简体   繁体   English

在不同 AWS CDK 堆栈之间共享资源(如 VPC、安全规则)的最佳和推荐方式

[英]Best & Recommended way to share resources like VPC, Security Rules between different AWS CDK Stack

I want to setup a infrastructure for a client product and I need to share resources between different ECS Services like VPC, Security Group, RDS so I can avoid recreating EC2 instance and assign a global accelerator to that ec2 instance for static ip我想为客户端产品设置基础设施,我需要在不同的 ECS 服务(如 VPC、安全组、RDS)之间共享资源,这样我就可以避免重新创建 EC2 实例并为该 ec2 实例分配一个全局加速器 static ip

There's no particular barrier to sharing resources across stacks in the CDK.在 CDK 中跨堆栈共享资源没有特别的障碍。 Just access the objects like any other object.只需像访问任何其他 object 一样访问对象。

Create your shared resources in one stack, then simply access them from your other stacks.在一个堆栈中创建您的共享资源,然后只需从其他堆栈访问它们。 The CDK will automatically create necessary cloudformation exports and imports. CDK 将自动创建必要的 cloudformation 导出和导入。

For example, suppose you create a stack infraStack which contains your vpc, security groups, etc as properties.例如,假设您创建了一个堆栈infraStack ,其中包含您的 vpc、安全组等作为属性。 You can simply pass your infraStack object to the constructor for your other stacks and access the resources in the other stack then use the objects like accessing any object like infraStack.vpc .您可以简单地将您的infraStack object 传递给其他堆栈的构造函数并访问其他堆栈中的资源,然后像访问任何 object 一样使用对象infraStack.vpc

Alternatively, you can explicitly create the exports/imports.或者,您可以显式创建导出/导入。 For example, you can use CfnOutput to output the id for a VPC.例如,您可以使用CfnOutput将 output 作为 VPC 的 ID。 Then you can import this in another stack using importValue fromFn and use that with ec2.Vpc.fromLookup -- or if you don't care about hard-coding the VPC id, just call ec2.Vpc.fromLookup and hard-code the VPC ID.然后,您可以使用Fn中的importValue将其导入另一个堆栈并将其与ec2.Vpc.fromLookup一起使用——或者如果您不关心对 VPC id 进行硬编码,只需调用ec2.Vpc.fromLookup并对专有网络 ID。

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

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