虽然我们都在等待更高级别/手写的Construct可用于使用AWS CDK创建EC2实例,但我正在尝试使用自动生成的低级CloudFormation资源,网址为Amazon.CDK.AWS.EC2 NuGet package中的Amazon.CDK.AWS.EC2.cloudformation.InstanceResource_
。
我的测试应用程序是。Amazon.CDK.AWS.EC2 NuGet package作为唯一其他依赖项的.NET Core控制台应用程序。这是代码:
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" }
}
});
}
}
}
然后,您编译项目并使用CDK将其synth
放入CloudFormation模板中,除tags
之外的所有设置都将其放入。
dotnet build <folder-with-code>
cdk synth --output <folder-where-to-write-cf-templates>
输出:
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'
现在,我完全理解CDK处于起步阶段,不建议用于任何开发工作,但最好的部分是cloudformation
命名空间下所有本机资源的可用性,我计划在CDK增长。
在正确方向上的任何帮助将不胜感激!
答案 0 :(得分:1)
结果是,如果您使用typescript
而不是任何C# .NET
,则为currently a bug in AWS CDK and does work。我已经为此登录了issue。