我试图使用subnet_group_name属性在AWS中使用aws_ec2.Vpc的select_subnets方法选择私有子网,如下代码段所述:
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类这里可用的方法。
帮助感激!
发布于 2021-05-01 00:50:05
您应该将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)https://stackoverflow.com/questions/67281678
复制相似问题