具有 Python 的 AWS CDK - 创建 VPC 的基础知识
AWS CDK with Python - basics of creating VPCs
我已经开始学习 AWS CDK,但从 Python 的 CDK 参考页面复制的以下代码示例有问题:https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2.README.html#vpc。这是我第一次尝试写一些比仅仅复制教程更好的东西,我找不到我在哪里犯了错误。非常感谢你帮助我。
配置:
source .env/bin/activate
pip3 install -r requirements.txt
pip3 install aws_cdk.aws_ec2
我的代码:
from aws_cdk import (
aws_ec2 as ec2,
core
)
class CdkStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
vpc = ec2.Vpc(self, "VPC",
cidr="10.100.0.0/16",
max_azs=3,
subnet_configuration=[{
"subnet_type": ec2.SubnetType.PUBLIC,
"name": "public1",
"cidr_mask": 24
}, {
"cidr_mask": 24,
"name": "private1",
"subnet_type": ec2.SubnetType.PRIVATE
}
]
)
"cdk synth" 命令抛出的错误消息:
(.env) [robert@fedora1 cdk]$ cdk synth
jsii.errors.JavaScriptError:
Error: Missing required properties for @aws-cdk/aws-ec2.SubnetConfiguration: subnetType
at validateRequiredProps (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7161:15)
at Object.deserialize (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6835:21)
at Kernel._toSandbox (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8111:61)
at /home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6731:40
at Array.map (<anonymous>)
at Object.deserialize (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6731:26)
at Kernel._toSandbox (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8111:61)
at /home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6841:29
at mapValues (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7101:27)
at Kernel._wrapSandboxCode (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8205:19)
at Kernel._create (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7749:26)
at Kernel.create (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7503:21)
at KernelHost.processRequest (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7293:28)
at KernelHost.run (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7233:14)
at Immediate._onImmediate (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7236:37)
at processImmediate (internal/timers.js:439:21)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 9, in <module>
CdkStack(app, "cdk")
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/robert/Documents/local/cdk/cdk/cdk_stack.py", line 22, in __init__
"subnet_type": ec2.SubnetType.PRIVATE
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/aws_cdk/aws_ec2/__init__.py", line 23226, in __init__
jsii.create(Vpc, self, [scope, id, props])
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_kernel/__init__.py", line 223, in create
interfaces=[iface.__jsii_type__ for iface in getattr(klass, "__jsii_ifaces__", [])],
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_kernel/providers/process.py", line 333, in create
return self._process.send(request, CreateResponse)
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_kernel/providers/process.py", line 318, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Missing required properties for @aws-cdk/aws-ec2.SubnetConfiguration: subnetType
Subprocess exited with error 1
您提供了一个字典列表,但它需要一个 SubnetConfiguration 对象列表。
subnet_configuration (Optional[List[SubnetConfiguration]])
subnet_configuration=[
ec2.SubnetConfiguration(
cidr_mask=24,
name='public1',
subnet_type=ec2.SubnetType.PUBLIC
),
ec2.SubnetConfiguration(
cidr_mask=24,
name='private1',
subnet_type=ec2.SubnetType.PRIVATE
)
]
我已经开始学习 AWS CDK,但从 Python 的 CDK 参考页面复制的以下代码示例有问题:https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2.README.html#vpc。这是我第一次尝试写一些比仅仅复制教程更好的东西,我找不到我在哪里犯了错误。非常感谢你帮助我。
配置:
source .env/bin/activate
pip3 install -r requirements.txt
pip3 install aws_cdk.aws_ec2
我的代码:
from aws_cdk import (
aws_ec2 as ec2,
core
)
class CdkStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
vpc = ec2.Vpc(self, "VPC",
cidr="10.100.0.0/16",
max_azs=3,
subnet_configuration=[{
"subnet_type": ec2.SubnetType.PUBLIC,
"name": "public1",
"cidr_mask": 24
}, {
"cidr_mask": 24,
"name": "private1",
"subnet_type": ec2.SubnetType.PRIVATE
}
]
)
"cdk synth" 命令抛出的错误消息:
(.env) [robert@fedora1 cdk]$ cdk synth
jsii.errors.JavaScriptError:
Error: Missing required properties for @aws-cdk/aws-ec2.SubnetConfiguration: subnetType
at validateRequiredProps (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7161:15)
at Object.deserialize (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6835:21)
at Kernel._toSandbox (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8111:61)
at /home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6731:40
at Array.map (<anonymous>)
at Object.deserialize (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6731:26)
at Kernel._toSandbox (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8111:61)
at /home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:6841:29
at mapValues (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7101:27)
at Kernel._wrapSandboxCode (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:8205:19)
at Kernel._create (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7749:26)
at Kernel.create (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7503:21)
at KernelHost.processRequest (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7293:28)
at KernelHost.run (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7233:14)
at Immediate._onImmediate (/home/robert/Documents/local/cdk/.env/lib/python3.7/site-packages/jsii/_embedded/jsii/jsii-runtime.js:7236:37)
at processImmediate (internal/timers.js:439:21)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 9, in <module>
CdkStack(app, "cdk")
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/robert/Documents/local/cdk/cdk/cdk_stack.py", line 22, in __init__
"subnet_type": ec2.SubnetType.PRIVATE
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_runtime.py", line 66, in __call__
inst = super().__call__(*args, **kwargs)
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/aws_cdk/aws_ec2/__init__.py", line 23226, in __init__
jsii.create(Vpc, self, [scope, id, props])
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_kernel/__init__.py", line 223, in create
interfaces=[iface.__jsii_type__ for iface in getattr(klass, "__jsii_ifaces__", [])],
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_kernel/providers/process.py", line 333, in create
return self._process.send(request, CreateResponse)
File "/home/robert/Documents/local/cdk/.env/lib64/python3.7/site-packages/jsii/_kernel/providers/process.py", line 318, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Missing required properties for @aws-cdk/aws-ec2.SubnetConfiguration: subnetType
Subprocess exited with error 1
您提供了一个字典列表,但它需要一个 SubnetConfiguration 对象列表。
subnet_configuration (Optional[List[SubnetConfiguration]])
subnet_configuration=[
ec2.SubnetConfiguration(
cidr_mask=24,
name='public1',
subnet_type=ec2.SubnetType.PUBLIC
),
ec2.SubnetConfiguration(
cidr_mask=24,
name='private1',
subnet_type=ec2.SubnetType.PRIVATE
)
]