嗨,我在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 documentation和CDK Runtime Context。
用法:
# 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/