简体   繁体   English

.NET-使用AWS CDK为.NET的低级CloudFormation资源构造标记EC2实例

[英].NET - Tagging an EC2 Instance using AWS CDK for .NET's low-level CloudFormation Resource constructs

While we all wait for a higher-level / hand-written Construct to be available for creating an EC2 instance using AWS CDK, I'm trying to use the auto-generated low-level CloudFormation Resource available at Amazon.CDK.AWS.EC2.cloudformation.InstanceResource_ in the Amazon.CDK.AWS.EC2 NuGet package . 虽然我们都等待上级/手写构建是可用于创建使用AWS CDK EC2实例,我试图使用可用的自动生成的低级别的CloudFormation资源在Amazon.CDK.AWS.EC2.cloudformation.InstanceResource_ NuGet包中的Amazon.CDK.AWS.EC2.cloudformation.InstanceResource_

My test app is a .NET Core Console App with the Amazon.CDK.AWS.EC2 NuGet package as the only other dependency. 我的测试应用程序是一个.NET Core控制台应用程序,其中Amazon.CDK.AWS.EC2 NuGet程序包是唯一的其他依赖项。 Here's the code: 这是代码:

using System;
using System.Linq;

namespace AWSCDKEval
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new app. The first argument is used to display a usage message for this app.
            var appArgs = new [] { $"dotnet ${nameof(AWSCDKEval)}" }
                .Concat(args)
                .ToArray();
            var app = new Amazon.CDK.App(appArgs);

            new TestStack(app, "test-aws-cdk-stack-1", new Amazon.CDK.StackProps());

            // Your app must write the return value of app.Run() to standard output. The `cdk init`
            // and `cdk synth` commands require this output.
            Console.WriteLine(app.Run());
        }
    }

    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",
                    KeyName = "my_key",
                    Tags = new []
                    {
                        new Amazon.CDK.Tag { Key = "Name", Value = "test-instance-1" },
                        new Amazon.CDK.Tag { Key = "foo", Value = "bar" }
                    }
                });
        }
    }
}

You then compile the project and use the CDK to synth it into CloudFormation templates and all settings but the tags make it. 然后,您编译项目并使用CDK将其synth到CloudFormation模板中,除tags所有设置都将其synth

dotnet build <folder-with-code>
cdk synth --output <folder-where-to-write-cf-templates>

And the output: 并输出:

Resources:
    testInstance1:
        Type: 'AWS::EC2::Instance'
        Properties:
            ImageId: ami-0f1155cc2cb6b0cfd
            InstanceType: t2.micro
            KeyName: my_key
    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'

Now, I totally understand that the CDK is in very early stages and not recommended for any dev work, but the best part is the availability of all native resources under the cloudformation namespace which I plan to use while the CDK grows. 现在,我完全理解CDK处于起步阶段,不建议任何开发工作使用,但是最好的部分是我计划在CDK增长时使用的cloudformation命名空间下所有本机资源的可用性。

Any help in the right direction would be appreciated! 任何在正确方向上的帮助将不胜感激!

Turns out, this is currently a bug in AWS CDK and does work if you use typescript instead of any C# .NET . 事实证明,这当前是AWS CDK中的一个错误,如果您使用typescript而不是任何C# .NET,它确实可以工作 I've logged an issue for the same. 我已经记录了同样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM