如何创建可在堆栈之间共享的VPC? [英] How to create VPC that can be shared across stacks?

查看:101
本文介绍了如何创建可在堆栈之间共享的VPC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决如何创建可重复使用的VPC的问题,该VPC可使用AWS CDK在多个堆栈中使用.我希望能够为每个项目创建不同的堆栈,然后能够导入应分配给不同堆栈的VPC.我还想使用一个良好的结构来创建它,在这里我可以在不同的时间部署不同的堆栈(这意味着:我不想一次部署所有堆栈).

I am trying to wrap my head around how to create a reusable VPC that can be used across multiple stacks using AWS CDK. I want to be able to create different stack per project and then be able to import the VPC that should be assigned to the different stacks. I also want to create this using a good structure where I can deploy different stacks at different times (meaning: I do not want to deploy all stacks at once).

我尝试了以下方法,但是这将为每个堆栈创建一个新的VPC,这不是我想要实现的,而是我想一次创建我的VPC,然后如果它已经存在,它将简单地重用先前创建的VPC.

I have tried the following approach but this will create a new VPC per stack which is not what I want to achieve, instead I would like to create my VPC once and then if it already exists it will simply reuse the previous created.

app.ts

import cdk = require('@aws-cdk/core');
import { Stack1 } from '../lib/stack1';
import { Stack2 } from '../lib/stack2';

const app = new cdk.App();

new Stack1(app, "Stack1");
new Stack2(app, "Stack2");

stack1.ts

stack1.ts

import cdk = require('@aws-cdk/core');

import { Configurator } from './configurators/configurator'

export class Stack1 extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const configurator = new Configurator(scope, "Stack1");

    // later reuse vpc from configurator using configurator.vpc
  }
}

stack2.ts

stack2.ts

import cdk = require('@aws-cdk/core');

import { Configurator } from './configurators/configurator'

export class Stack2 extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const configurator = new Configurator(scope, "Stack2");

    // later reuse vpc from configurator using configurator.vpc
  }
}

configurator.ts

configurator.ts

import cdk = require('@aws-cdk/core');
import ec2 = require("@aws-cdk/aws-ec2");

export class Configurator {

  vpc: ec2.Vpc;

  constructor(scope: cdk.Construct, name: string) {
    this.vpc = new ec2.Vpc(scope, "MyVPC", {
      maxAzs: 3
    });
  }
}

完成后

cdk synth
cdk deploy Stack1
cdk deploy Stack2

这将创建2个VPC,并且不会像我想的那样重用1个VPC.我会将堆栈部署到相同的帐户和区域.

This will create 2 VPCs and not reusing 1 VPC as I would like. I will deploy the stacks to same account and region.

如何更改方法以实现所需的输出?我希望能够彼此独立地部署堆栈.

How can I change my approach in order to achieve the output I am looking for? I want to be able to deploy my stacks independently of each other.

推荐答案

如果您打算在不同的堆栈中重用VPC,我建议您将其放在单独的堆栈中,因为您的VPC堆栈的生命周期与您的VPC堆栈的生命周期不同应用程序堆栈.

If you intend to reuse the VPC in different stacks, I'd recommend placing it in a separate stack, since your VPC stack will have a different lifecycle than your application stacks.

这就是我要做的.我希望你不要介意Python:)

Here's what I'd do. I hope you don't mind a bit of Python :)

首先,在 VpcStack 中定义您的VPC:

First, define your VPC in VpcStack:

class VpcStack(core.Stack):
    def __init__(self, app: core.App, id: str, **kwargs) -> None:
        super().__init__(app, id, **kwargs)
        aws_ec2.Vpc(self, 'MyVPC', max_azs=3)

然后在另一个堆栈中查找它:

Then look it up in another stack:

class Stack1(core.Stack):
    def __init__(self, app: core.App, id: str, **kwargs) -> None:
        super().__init__(app, id, **kwargs)
        # Lookup the VPC named 'MyVPC' created in stack 'vpc-stack'
        my_vpc = aws_ec2.Vpc.from_lookup(self, 'MyVPC', vpc_name=f'vpc-stack/MyVPC')
        # You can now use the VPC in ECS cluster, etc.

这将是您的 cdk_app.py :

app = core.App()
vpc = VpcStack(app, 'vpc-stack')
stack1 = Stack1(app, 'stack1')

这篇关于如何创建可在堆栈之间共享的VPC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆