虽然我们都在等待更高级别/手写的构造可用于使用 AWS CDK 创建 EC2 实例,但我正在尝试使用 Amazon.CDK.AWS 中提供的自动生成的低级别Amazon.CDK.AWS.EC2.cloudformation.InstanceResource_
CloudFormation资源.EC2 NuGet 包。
我的测试应用程序是一个 .NET Core 控制台应用程序,其中Amazon.CDK.AWS.EC2 NuGet 包作为唯一的其他依赖项。这是代码:
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
make 之外的所有设置。
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 增长时使用的命名空间下的所有本机资源的可用性。
任何正确方向的帮助将不胜感激!