python - 在 aws CDK 中找不到 select_subnets 函数
问题描述
我正在尝试使用 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 类的方法
帮助表示赞赏!
解决方案
您应该将 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)
推荐阅读
- python - pypi神器的神器过期
- java - 有什么比在 Java Spring Boot 中使用自定义查询更好的方法?
- swagger - Swagger 编辑器显示路径参数的“架构错误:不应有其他属性”错误
- javascript - jQuery 立即验证
- react-native - 来自基于 SideMenu 的应用程序的 ViroARSceneNavigator/ViroVRSceneNavigator
- wordpress - post_per_page 使用自定义值
- azure-devops - 从特定工件触发器触发特定环境
- css - 我将图像设置为绝对定位,使用百分比设置,但是在调整窗口大小时一张图像仍然没有保留
- javascript - 当用户输入新消息时,将特定的新消息 div 滚动到容器 div 的顶部
- go - 如何使用 gonum/go 制作包含复数条目的矩阵?