typescript - 通过 AWS CDK Typescript 进行 VPC 对等互连(2 个 VPCS 在 2 个不同的账户中)
问题描述
我正在尝试为 2 个单独帐户中的 2 个 vpc 之间的 vpc 对等创建 cdk 打字稿。相关代码如下。vpc 创建得很好,但是我无法从 vpc1 和 vpc2 中引用 vpcId 和 peerVpcId。任何人都可以帮忙吗?任何工作示例代码都会非常有帮助。谢谢。
//creation of vpc 1
export class vpc1 extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: EnvProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'vpc1',{
cidr: props.vpcCidr ,
enableDnsHostnames: true,
enableDnsSupport: true,
maxAzs: 3,
subnetConfiguration: [{
cidrMask: 24,
name: 'Public',
subnetType: ec2.SubnetType.PUBLIC,
},
{
cidrMask: 24,
name: 'Private',
subnetType: ec2.SubnetType.PRIVATE,
}],
natGateways: 1
}); }}
//creation of vpc 2
export class vpc2 extends cdk.Stack {
constructor(scope: cdk.App, id: string, props: EnvProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'vpc2',{
cidr: props.vpcCidr ,
enableDnsHostnames: true,
enableDnsSupport: true,
maxAzs: 3,
subnetConfiguration: [{
cidrMask: 24,
name: 'Public',
subnetType: ec2.SubnetType.PUBLIC,
},
{
cidrMask: 24,
name: 'Private',
subnetType: ec2.SubnetType.PRIVATE,
}],
natGateways: 1
});
}}
export class vpcPeeringfisHiDev extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc_peering = new ec2.CfnVPCPeeringConnection (this, 'vpcPeer',{
vpcId: vpc1.vpc.vpcId, //Error - Property 'vpc' does not exist on type 'typeof vpc1'.ts(2339)
peerVpcId: vpc2.vpc.vpcId //Error - Property 'vpc' does not exist on type 'typeof vpc2'.ts(2339)
}); }}
解决方案
vpc: ec2.Vpc
您必须在各自的堆栈中公开您正在创建的构造,以便vpcPeeringfisHiDev
可以使用它们:
export class vpc1 extends cdk.Stack {
readonly vpc: ec2.Vpc // <-- add this
constructor (...) {
// :: instead of `const vpc =`
this.vpc = new ec2.Vpc (...)
}
// ...
}
// :: do that for the vpc2 stack too
export interface VpcPeeringStackProps extends cdk.StackProps {
vpc1: ec2.Vpc,
vpc2: ec2.Vpc
}
export class VpcPeeringStack extends cdk.Stack {
constructor (scope: cdk.Construct, id: string, props: VpcPeeringStackProps) {
// ...
const peering = new ec2.CfnVPCPeeringConnection(this, '...', {
// :: now you can use them here
vpcId: props.vpc1.vpcId,
peerVpcId: props.vpc2.vpcId
})
}
}
然后在您的 CDK 应用程序定义文件中,您需要正确传递您的引用:
const vpc1stack = new vpc1(app, 'vpc1')
const vpc2stack = new vpc2(app, 'vpc2')
// :: the peering stack --- note how we're passing in the 2 previous VPCs
new VpcPeeringStack(app, 'peering', {
vpc1: vpc1stack.vpc,
vpc2: vpc2stack.vpc
})
推荐阅读
- css - Yii2 中的自定义 Bootstrap 4 css 和资产管理
- hyperledger-fabric - 如何将 Daml 智能合约部署到 Hyperledger Fabric 或 Sawtooth?
- matplotlib - 导入 matplotlib.animation 时,模块“matplotlib”没有属性“_get_executable_info”
- android - 使用 RxJava 进行双向数据绑定
- c# - 什么都不返回的异步方法的返回类型应该是什么
- r - glmnet:提取标准化系数
- asp.net-web-api2 - 在 Insights 中看不到任何跟踪或异常
- html - Angular 8 有什么方法可以将类型信息保存在多个异步管道中?
- apache - 如何使用核心 PHP 中的基本参数通过 Htaccess 重写 URL
- php - Magento 2 - 以编程方式删除产品的 Tierprices