gpt4 book ai didi

amazon-web-services - 如何在AWS CDK中导入现有的VPC?

转载 作者:行者123 更新时间:2023-12-01 12:04:20 24 4
gpt4 key购买 nike

嗨,我在aws cdk上工作。我正在尝试获取现有的非默认vpc。我尝试了以下选项。

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,并且可以正常工作

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

最佳答案

看一看aws_cdk.aws_ec2 documentationCDK Runtime Context

If your VPC is created outside your CDK app, you can useVpc.fromLookup(). The CDK CLI will search for the specified VPC in thethe stack’s region and account, and import the subnet configuration.Looking up can be done by VPC ID, but more flexibly by searching for aspecific 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
)
使用 typescript 示例进行更新:
import ec2 = require('@aws-cdk/aws-ec2');
const getExistingVpc = ec2.Vpc.fromLookup(this, 'ImportVPC',{isDefault: false,vpcId: vpcId });
More info here.

关于amazon-web-services - 如何在AWS CDK中导入现有的VPC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59301265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com