- 最新
- 最多得票
- 最多評論
The solution was very tricky and not well documented in my opinion.
Since I was creating the subnet in the same stack as I used to look up the VPC, I had to use the ec2.Vpc.from_vpc_attributes()
class method instead of the ec2.Vpc.from_lookup()
class method.
Does not work:
# app.py vpc = ec2.Vpc.from_lookup(self, "IVpc", region="us-east-1") # ...[create public subnet ref CfnVpc] # ...[create instance in public subnet]
Error Logs
jsii.errors.JavaScriptError: Error: To set 'associatePublicIpAddress: true' you must select Public subnets (vpcSubnets: { subnetType: SubnetType.PUBLIC }) at new Instance (/tmp/jsii-kernel-wHeWcm/node_modules/aws-cdk-lib/aws-ec2/lib/instance.js:1:5169) at Kernel._Kernel_create (/tmp/tmpxiyze867/lib/program.js:10108:25) at Kernel.create (/tmp/tmpxiyze867/lib/program.js:9779:93) at KernelHost.processRequest (/tmp/tmpxiyze867/lib/program.js:11696:36) at KernelHost.run (/tmp/tmpxiyze867/lib/program.js:11656:22) at Immediate._onImmediate (/tmp/tmpxiyze867/lib/program.js:11657:46) at process.processImmediate (node:internal/timers:478:21) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/home/ubuntu/acg_nlb_lab/infra/app.py", line 80, in <module> MyInfra(app, STACK, env=env) File "/home/ubuntu/.pyenv/versions/cdk-3.10/lib/python3.10/site-packages/jsii/_runtime.py", line 118, in __call__ inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs) File "/home/ubuntu/acg_nlb_lab/infra/app.py", line 55, in __init__ instanceB = ec2.Instance( File "/home/ubuntu/.pyenv/versions/cdk-3.10/lib/python3.10/site-packages/jsii/_runtime.py", line 118, in __call__ inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs) File "/home/ubuntu/.pyenv/versions/cdk-3.10/lib/python3.10/site-packages/aws_cdk/aws_ec2/__init__.py", line 71951, in __init__ jsii.create(self.__class__, self, [scope, id, props]) File "/home/ubuntu/.pyenv/versions/cdk-3.10/lib/python3.10/site-packages/jsii/_kernel/__init__.py", line 334, in create response = self.provider.create( File "/home/ubuntu/.pyenv/versions/cdk-3.10/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 365, in create return self._process.send(request, CreateResponse) File "/home/ubuntu/.pyenv/versions/cdk-3.10/lib/python3.10/site-packages/jsii/_kernel/providers/process.py", line 342, in send raise RuntimeError(resp.error) from JavaScriptError(resp.stack) RuntimeError: To set 'associatePublicIpAddress: true' you must select Public subnets (vpcSubnets: { subnetType: SubnetType.PUBLIC })
Works:
# app.py # ...[create public subnet ref CfnVpc] # ...[create instance in public subnet] vpc = ec2.Vpc.from_vpc_attributes( self, "IVpc", vpc_id=self.vpc.ref, availability_zones=["us-east-1a", "us-east-1b"], public_subnet_ids=[self.publicSubnet.ref, publicSubnetB.subnet_id], public_subnet_route_table_ids=[ self.publicRouteTable.ref, self.publicRouteTable.ref, ], ) # ...[able to use L2 IVpc and L2 instance]
It's all about dependencies... You have an issue with the timing of resource creation when using AWS CDK. AWS CDK provisions resources in the order they are defined within the construct. However, when you use ec2.Vpc.from_lookup() to obtain a VPC, CDK might not wait for the lookup to complete before moving on to create other resources, resulting in the failure to find the public subnet in availability zone us-east-1b.
Try to use core.Construct to encapsulate the lookup logic and create a custom resource that waits for the lookup to finish.
相關內容
- AWS 官方已更新 1 年前
Hmm... I'm trying to understand this. So if I had something like this?
In my mind this should work - create the subnet first, then launch the instance into it. But I'm still getting the same
RuntimeError: To set 'associatePublicIpAddress: true' you must select Public subnets (vpcSubnets: { subnetType: SubnetType.PUBLIC })
error.