[英]Unable to find select_subnets function in aws CDK
我正在尝试 select 私有子网,使用 AWS CDK 中 aws_ec2.Vpc 的 select_subnets 方法使用 subnet_group_name 属性,如下面的代码片段所述:
from aws_cdk import core as cdk
from aws_cdk import aws_ec2 as ec2
from aws_cdk import core
class SimpleCdkStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
subnets = ec2.Vpc.select_subnets(self,
subnet_group_name="private-subnet"
)
print("Subnet Ids: " + subnets.subnet_ids)
上述错误在执行过程中会产生以下错误:
$ cdk diff
jsii.errors.JavaScriptError:
Error: Class @aws-cdk/core.Stack doesn't have a method 'selectSubnets'
at Kernel._typeInfoForMethod (/tmp/tmphu1erjw6/lib/program.js:8420:27)
at Kernel._findInvokeTarget (/tmp/tmphu1erjw6/lib/program.js:8340:33)
at Kernel.invoke (/tmp/tmphu1erjw6/lib/program.js:7966:44)
at KernelHost.processRequest (/tmp/tmphu1erjw6/lib/program.js:9479:36)
at KernelHost.run (/tmp/tmphu1erjw6/lib/program.js:9442:22)
at Immediate._onImmediate (/tmp/tmphu1erjw6/lib/program.js:9443:46)
at processImmediate (internal/timers.js:461:21)
我已经使用以下命令安装了所需的软件包
$ pip install aws_cdk.aws_ec2
不知道我哪里错了。
AWS 在这里明确提到了适用于 aws_ec2.Vpc class 的方法
帮助表示赞赏!
您应该将 Vpc 引用作为第一个参数传递给 select_subnet 调用,但实际上您已经传递了self
,它是一个 CDK 堆栈。
例子
from aws_cdk import aws_ec2 as ec2
from aws_cdk import core as cdk
class SimpleCdkStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
subnet_ids = ec2.Vpc.from_lookup(
vpc_id='your vpc id'
).select_subnets(
subnet_group_name="private-subnet"
).subnet_ids
for subnet_id in subnet_ids:
print("Subnet Ids: " + subnet_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.