aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

dotnet: Tagging an EC2 instance using L1 class doesn't work

Open ChintanRaval opened this issue 8 years ago • 1 comments

I'm trying to bring up an EC2 instance while tagging the same with a few key/value pairs and this seems to be not working in .NET.

I posted the issue with code samples in StackOverflow. Since I didn't get any traction on it, I reached out to the aws-cdk channel on Gitter and got help with confirmation that the same works when using typescript and therefore, I think this may be an issue at least with .NET.

Anyway, here's the code samples of what does and does not work.

Does work

Thanks to @moofish32 & @ccurrie-amzn!

Code

class InstanceStack extends cdk.Stack {

    constructor(parent: cdk.App, id: string, props: {}) {
        super(parent, id, props);
        new ec2.cloudformation.InstanceResource(this, 'Instance', {
            imageId: "ami-0f1155cc2cb6b0cfd", // Microsoft Windows Server 2016 Base
            instanceType: "t2.micro",
            tags: [{key: 'foo', value: 'bar'}]
        });

    }
}

Result CF template

 Resources:
    Instance:
        Type: 'AWS::EC2::Instance'
        Properties:
            ImageId: ami-0f1155cc2cb6b0cfd
            InstanceType: t2.micro
            Tags:
                -
                    Key: foo
                    Value: bar
    CDKMetadata:
        Type: 'AWS::CDK::Metadata'
        Properties:
            Modules: '@aws-cdk/aws-ec2=0.9.1,@aws-cdk/cdk=0.9.1,@aws-cdk/cx-api=0.9.1,js-base64=2.4.9,vpc-examples=0.1.0'

Does NOT work

Code

    public class TestStack : Amazon.CDK.Stack
    {
        public TestStack(Amazon.CDK.App parent, string name, Amazon.CDK.IStackProps props) : base(parent, name, props)
        {
            new Amazon.CDK.AWS.EC2.cloudformation.InstanceResource_(
                this, 
                "testInstance1", 
                new Amazon.CDK.AWS.EC2.cloudformation.InstanceResourceProps
                {
                    ImageId = "ami-0f1155cc2cb6b0cfd", // Microsoft Windows Server 2016 Base
                    InstanceType = "t2.micro",
                    Tags = new []
                    {
                        new Amazon.CDK.Tag { Key = "foo", Value = "bar" }
                    }
                });
        }
    }

Result CF template

Resources:
    testInstance1:
        Type: 'AWS::EC2::Instance'
        Properties:
            ImageId: ami-0f1155cc2cb6b0cfd
            InstanceType: t2.micro
    CDKMetadata:
        Type: 'AWS::CDK::Metadata'
        Properties:
            Modules: '@aws-cdk/aws-ec2=0.9.1,@aws-cdk/aws-iam=0.9.1,@aws-cdk/cdk=0.9.1,@aws-cdk/cx-api=0.9.1,js-base64=2.4.9'

ChintanRaval avatar Sep 20 '18 00:09 ChintanRaval

Is this still an issue? I wrote the following code:

using Amazon.CDK;
using Amazon.CDK.AWS.EC2.cloudformation;

namespace aws_cdk_746
{
    class TestStack : Stack
    {
        public TestStack(App parent, string name, IStackProps props) : base(parent, name, props)
        {
            new InstanceResource_(this, "testInstance1", new InstanceResourceProps
            {
                ImageId = "ami-0f1155cc2cb6b0cfd",
                InstanceType = "t2.micro",
                Tags = new[]
                {
                    new Tag {Key = "foo", Value = "bar"}
                }
            });
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var app = new App();
            new TestStack(app, "testStack", new StackProps());
            app.Run();
        }
    }
}

And got the following output:

Resources:
  testInstance1:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0f1155cc2cb6b0cfd
      InstanceType: t2.micro
      Tags:
        - Key: foo
          Value: bar
    Metadata:
      aws:cdk:path: testStack/testInstance1
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: aws-cdk=0.21.0,@aws-cdk/aws-ec2=0.21.0,@aws-cdk/aws-iam=0.21.0,@aws-cdk/cdk=0.21.0,@aws-cdk/cx-api=0.21.0,jsii-runtime=DotNet/4.0.30319.42000

I also tried reproducing with the new CfnInstance, but that was also successful.

I am using .NET Core 2.2 on CDK version 0.21.0

costleya avatar Dec 20 '18 21:12 costleya