简体   繁体   English

如何将默认 VPC 导入 CloudFormation 堆栈并使用 AWS CDK 重新创建它?

[英]How to import default VPC into CloudFormation stack and recreate it with AWS CDK?

I want to create an AWS CloudFormation stack with all of my network resources.我想用我的所有网络资源创建一个 AWS CloudFormation 堆栈。 So I also want to include my default VPC.所以我还想包括我的默认 VPC。 I created my stack with AWS CDK and want to configure all of my resources with CDK.我使用 AWS CDK 创建了我的堆栈,并希望使用 CDK 配置我的所有资源。

To import my default VPC I used the management console and the stack action "Import resources into stack".为了导入我的默认 VPC,我使用了管理控制台和堆栈操作“将资源导入堆栈”。 I used this template to import my default VPC:我使用此模板导入我的默认 VPC:

Resources:
  VPC:
    Type: AWS::EC2::VPC
    DeletionPolicy: Retain
    Properties:
      CidrBlock: 172.31.0.0/16
      EnableDnsSupport: 'true'
      EnableDnsHostnames: 'true'
      InstanceTenancy: default

The import wizard called for the VPC ID and I used my default VPC ID.导入向导调用了 VPC ID,我使用了我的默认 VPC ID。 After successful import I got a CloudFormation template of the stack with the same content as above.成功导入后,我得到了一个与上面内容相同的堆栈的 CloudFormation 模板。

Now I wanted to recreate my default VCP with AWS CDK.现在我想用 AWS CDK 重新创建我的默认 VCP。 I don't want to use Vpc.fromLookup or fromVpcAttributes .我不想使用Vpc.fromLookupfromVpcAttributes I want to create a new VPC with AWS CDK which is my default VPC.我想用 AWS CDK 创建一个新的 VPC,这是我的默认 VPC。 So I wrote:所以我写道:

const vpc = new ec2.Vpc(this, 'VPC', {
  cidr: "172.31.0.0/16",
  enableDnsSupport: true,
  enableDnsHostnames: true,
  defaultInstanceTenancy: DefaultInstanceTenancy.DEFAULT
});

But when I call cdk diff it shows:但是当我调用cdk diff它显示:

[-] AWS::EC2::VPC VPC orphan
[+] AWS::EC2::VPC VPC VPCXYZ12345

So it wants to create a new VPC and my imported default VPC is orphaned.所以它想创建一个新的 VPC,而我导入的默认 VPC 是孤立的。

I also tried to override the logical ID.我还尝试覆盖逻辑 ID。 So it matches with the ID of my default VPC:所以它与我的默认 VPC 的 ID 匹配:

const cfnVpc = vpc.node.defaultChild as cdk.CfnResource;
cfnVpc.overrideLogicalId('vpc-abcd1234');

But the output of cdk diff is like above but now with the VPC ID of my default VPC.但是cdk diff的 output 就像上面一样,但现在使用的是我的默认 VPC 的 VPC ID。

Is it even possible to import the default VPC into a CloudFormation stack and recreate it with AWS CDK?是否可以将默认 VPC 导入 CloudFormation 堆栈并使用 AWS CDK 重新创建它?

Though you are keeping the stack name same, the logical id of the vpc resource in imported cloudformation and the cdk generated cloudformation are different.尽管您保持堆栈名称相同,但导入的 cloudformation 中的 vpc 资源的逻辑 id 和 cdk 生成的 cloudformation 是不同的。 VPC vs VPCXYZ12345. VPC 与 VPCXYZ12345。

This is the reason why cdk diff is showing deleting and recreation.这就是 cdk diff 显示删除和重新创建的原因。

Here is what we can ideally do:以下是我们理想情况下可以做的:

  • Delete your existing cloudformation删除您现有的 cloudformation
  • Create cloudformation cdk --no-version-reporting synth创建 cloudformation cdk --no-version-reporting synth
  • Grab the template from cdk.out从 cdk.out 获取模板
  • Import this in cloudformation console, just like your regular template.将其导入 cloudformation 控制台,就像您的常规模板一样。

This process should help you import your existing vpc into cdk.这个过程应该可以帮助您将现有的 vpc 导入 cdk。 Here is an similar answer for importing DynamoDB table in cdk.是在 cdk 中导入 DynamoDB 表的类似答案。

You can't mark non-default VPC as default, and you can't yourself reconstruct a default VPC.您不能将非默认 VPC 标记为默认,也不能自己重建默认 VPC。 From docs :来自文档

You cannot restore a previous default VPC that you deleted, and you cannot mark an existing nondefault VPC as a default VPC.无法恢复之前删除的默认 VPC,也无法将现有的非默认 VPC 标记为默认 VPC。

You have to use AWS Console, SDK or CLI dedicated call to create a default VPC:您必须使用 AWS 控制台、SDK 或 CLI专用调用来创建默认 VPC:

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

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