簡體   English   中英

如何在 aws cdk 中導入現有 VPC?

[英]How to import existing VPC in aws cdk?

嗨,我正在研究 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 文檔CDK 運行時上下文

如果您的 VPC 是在 CDK 應用程序之外創建的,您可以使用 Vpc.fromLookup()。 CDK CLI 將在堆棧的區域和帳戶中搜索指定的 VPC,並導入子網配置。 可以通過 VPC ID 進行查找,但通過搜索 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 v2v1(latest) ,您可以使用:

// You can either use vpcId OR vpcName and fetch the desired vpc
const getExistingVpc = ec2.Vpc.fromLookup(this, 'ImportVPC',{
  vpcId: "VPC_ID",
  vpcName: "VPC_NAME"

});

這是一個簡單的例子

//get VPC Info form AWS account, FYI we are not rebuilding we are referencing 
const DefaultVpc = Vpc.fromVpcAttributes(this, 'vpcdev', {
    vpcId:'vpc-d0e0000b0',
    availabilityZones: core.Fn.getAzs(),
    privateSubnetIds: 'subnet-00a0de00',
    publicSubnetIds: 'subnet-00a0de00'
});

        const yourService = new lambda.Function(this, 'SomeName', {
        code: lambda.Code.fromAsset("lambda"),
        handler: 'handlers.your_handler',
        role: lambdaExecutionRole,
        securityGroup: lambdaSecurityGroup,
        vpc: DefaultVpc,
        runtime: lambda.Runtime.PYTHON_3_7,
        timeout: Duration.minutes(2),
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM