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

查看:26
本文介绍了如何在 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

推荐答案

查看 aws_cdk.aws_ec2 文档CDK 运行时上下文.

如果您的 VPC 是在 CDK 应用程序之外创建的,您可以使用Vpc.fromLookup().CDK CLI 将在堆栈的区域和帐户,并导入子网配置.可以通过 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
)

更新打字稿示例:

import ec2 = require('@aws-cdk/aws-ec2');
const getExistingVpc = ec2.Vpc.fromLookup(this, 'ImportVPC',{isDefault: false,vpcId: vpcId });

此处有更多信息.

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

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