aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Unable to reference subnet group name tag on an imported VPC

Open dassowmd opened this issue 6 years ago • 1 comments

I am not able to reference a custom subnet group name on an imported VPC. I have tried to reference both the default tag (aws-cdk:subnet-name) and also by passing the a custom tag key to the vpc.from_lookup() method. Both to no avail unless I manually prebuild my cdk.context.json and commit to my repo.

This issue seems related to https://github.com/aws/aws-cdk/issues/3650 which is marked as resolved, but does not appear to be functioning as expected.

Reproduction Steps

` vpc = aws_ec2.Vpc.from_lookup(self , "VPC" , vpc_name='VPC1' , subnet_group_name_tag='custom-subnet-group-tag-name' )

    host = aws_ec2.Instance(<my_stack>
                            ,windows_powerbi_gateway_server_name,
                            instance_type=ec2.InstanceType(
                            instance_type_identifier='t2.large'),
                            instance_name='some_name',
                            machine_image=<windows_ami>,
                            vpc=vpc,
                            key_name='some_key',
                            vpc_subnets=ec2.SubnetSelection(subnet_group_name="application_layer")
                       )

`

Error Log

If I run the above code with an empty context I get the following error: vpc_subnets=ec2.SubnetSelection(subnet_group_name="application") File "C:\Users\<username>\AppData\Local\Programs\Python\Python37\lib\site-packages\jsii\_runtime.py", line 66, in __call__ inst = super().__call__(*args, **kwargs) File "C:\Users\<username>\AppData\Local\Programs\Python\Python37\lib\site-packages\aws_cdk\aws_ec2\__init__.py", line 22158, in __init__ jsii.create(Instance, self, [scope, id, props]) File "C:\Users\<username>\AppData\Local\Programs\Python\Python37\lib\site-packages\jsii\_kernel\__init__.py", line 229, in create interfaces=[iface.__jsii_type__ for iface in getattr(klass, "__jsii_ifaces__", [])], File "C:\Users\<username>\AppData\Local\Programs\Python\Python37\lib\site-packages\jsii\_kernel\providers\process.py", line 333, in create return self._process.send(request, CreateResponse) File "C:\Users\<username>\AppData\Local\Programs\Python\Python37\lib\site-packages\jsii\_kernel\providers\process.py", line 318, in send raise JSIIError(resp.error) from JavaScriptError(resp.stack) jsii.errors.JSIIError: Cannot read property 'subnetId' of undefined Subprocess exited with error 1

If I prebuild my cdk.context.json and rerun it successfully builds. While I recognize this work around fixes the immediate issue, it also creates technical debt because now any time I deploy the stack to a new account or region I have to update my cdk.context.json manually.

Environment

  • **CLI Version :1.33.0
  • **Framework Version :
  • **OS : Windows 10
  • **Language :Python 3.7.6

Other

I believe that the issue is that the CDK is relying on the dummy vpc instead of making the call to get the context before trying to make the aws_ec2.Instance() call. If it were to instead fetch the context first I believe it would fix the issue.


This is :bug: Bug Report

dassowmd avatar Apr 23 '20 20:04 dassowmd

I am experiencing the same issue with a Python CDK app. We would like to use the Vpc.from_lookup() with a specified subnet group name tag since we have multiple PRIVATE subnet tiers/groups within our VPC.

ec2.Vpc.from_lookup(
    self, "VPC", vpc_name="vpc-name", subnet_group_name_tag="custom-tag-name"
)

We then try to use the ec2.SubnetSelection() reference with a valid subnet_group_name:

vpc_subnets=ec2.SubnetSelection(subnet_group_name="app-tier")

This does not work for a synth if the context does not yet exist but will work just fine if the context is already populated from an earlier successful Vpc.from_lookup().

This seems to be a problem only for the first synth when trying to use the subnet selection based on a supplied group name. As long as we have a context and do not clear it going forward, the subnet selection reference will work as expected.

jeremywitte avatar Apr 23 '20 20:04 jeremywitte