如何在AWS CDK中导入现有的VPC? [英] How to import existing VPC in aws cdk?

查看:235
本文介绍了如何在AWS CDK中导入现有的VPC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在制作AWS CDK.我正在尝试获取现有的非默认vpc.我尝试了以下选项.

Hi I am working on aws cdk. I am trying to get existing non-default vpc. I tried below options.

vpc = ec2.Vpc.from_lookup(self, id = "VPC", vpc_id='vpcid', vpc_name='vpc-dev')

这将导致以下错误

[Error at /LocationCdkStack-cdkstack] Request has expired.
[Warning at /LocationCdkStack-cdkstack/TaskDef/mw-service] Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'.
Found errors

我尝试过的其他方法是

vpc = ec2.Vpc.from_vpc_attributes(self, 'VPC', vpc_id='vpc-839227e7', availability_zones=['ap-southeast-2a','ap-southeast-2b','ap-southeast-2c'])

这将导致

[Error at /LocationCdkStack-cdkstack] Request has expired.
[Warning at /LocationCdkStack-cdkstack/TaskDef/mw-service] Proper policies need to be attached before pulling from ECR repository, or use 'fromEcrRepository'.
Found errors

我尝试过的其他方法是

vpc = ec2.Vpc.from_lookup(self, id = "VPC", is_default=True)//这将获得默认的vpc,并且可以正常工作

vpc = ec2.Vpc.from_lookup(self, id = "VPC", is_default=True) // This will get default vpc and this will work

有人可以帮助我在AWS CDK中获得非默认VPC吗?任何帮助,将不胜感激.谢谢

Can someone help me to get non-default vpc in aws cdk? Any help would be appreciated. Thanks

推荐答案

看看

如果您的VPC是在CDK应用程序外部创建的,则可以使用 Vpc.fromLookup(). CDK CLI将在以下位置搜索指定的VPC: 堆栈的区域和帐户,然后导入子网配置. 可以通过VPC ID进行查找,但可以通过搜索 VPC上的特定标签.

If your VPC is created outside your CDK app, you can use Vpc.fromLookup(). The CDK CLI will search for the specified VPC in the the stack’s region and account, and import the subnet configuration. Looking up can be done by VPC ID, but more flexibly by searching for a specific tag on the VPC.

用法:

# Example automatically generated. See https://github.com/aws/jsii/issues/826
from aws_cdk.core import App, Stack, Environment
from aws_cdk import aws_ec2 as ec2

# Information from environment is used to get context information
# so it has to be defined for the stack
stack = MyStack(
    app, "MyStack", env=Environment(account="account_id", region="region")
)

# Retrieve VPC information
vpc = ec2.Vpc.from_lookup(stack, "VPC",
    # This imports the default VPC but you can also
    # specify a 'vpcName' or 'tags'.
    is_default=True
)

使用相关示例进行更新:

vpc = ec2.Vpc.from_lookup(stack, "VPC",
    vpc_id = VPC_ID
)

此处有更多信息.

这篇关于如何在AWS CDK中导入现有的VPC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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