aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.59k stars 3.89k forks source link

[Lambda Function] subnet selection tries to evaluate subnet AZ even if AZ filtering is not specified #8301

Closed pergardebrink closed 4 years ago

pergardebrink commented 4 years ago

When I try to use the Subnet.FromSubnetId method to construct a SubnetSelection, I get an error stating that I must add the availabilityZone. This does not really make any sense since I am specifying the subnet and not trying to filter anything?

Reproduction Steps

new Function(this, "function", new FunctionProps {
    // function properties... 
    VpcSubnets = new SubnetSelection {
        Subnets = new[] { Subnet.FromSubnetId(this, "Subnet", "subnet-id-I-want-to-use") }
    }
}

Error Log

Unhandled exception. Amazon.JSII.Runtime.JsiiException: You cannot reference a Subnet's availability zone if it was not supplied. Add the availabilityZone when importing using Subnet.fromSubnetAttributes()
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.Create(CreateRequest request)
   at Amazon.JSII.Runtime.Services.Client.Create(String fullyQualifiedName, Object[] arguments, Override[] overrides, String[] interfaces)
   at Amazon.JSII.Runtime.Deputy.DeputyBase..ctor(DeputyProps props)
   at Constructs.Construct..ctor(DeputyProps props)
   at Amazon.CDK.Construct..ctor(DeputyProps props)
   at Amazon.CDK.Resource..ctor(DeputyProps props)
   at Amazon.CDK.AWS.Lambda.FunctionBase..ctor(DeputyProps props)
   at Amazon.CDK.AWS.Lambda.Function..ctor(Construct scope, String id, IFunctionProps props)

Environment

Other

If I use the following code, it works, but it feels a bit ugly and hacky

new Function(this, "function", new FunctionProps {
    // function properties... 
    VpcSubnets = new SubnetSelection {
        Subnets = new[] { 
            Subnet.FromSubnetAttributes(this, "Subnet", new subnetAttributes {
                AvailabilityZone = "dummy", // This works as long as it's non-empty
                SubnetId =  "subnet-id-I-want-to-use"
            }) 
        }
    }
}

This is :bug: Bug Report

PerArneng commented 4 years ago

    private static ISubnet getSubnetFromId(Construct scope, String id) {
        return Subnet.fromSubnetAttributes(scope, "subnet_" + id,
                  SubnetAttributes.builder()
                          .subnetId(id)
                          .availabilityZone("dummy") // https://github.com/aws/aws-cdk/issues/8301
                          .build()
        );
    } 

that code works for me as well. thanks for the workaround.

flemjame-at-amazon commented 4 years ago

This is the bare minimum code to reproduce the issue:

vpc.selectSubnets(SubnetSelection.builder()
    .subnets(Arrays.asList(
        Subnet.fromSubnetId(this, "S", "s-d135fa9b")
    ))
    .build());