AWS CDK: Place a Lambda function in specific subnets

I struggled for a while today to figure out how to place a lambda function inside of a given SubnetType of a VPC generated in the same CDK stack.

To specify subnets for a lambda function it needs a SubnetSelection object passed in. I expected to find a method of ec2.VPC() which would give me an output with the type SubnetSelection but this doesn’t exist.

After some reading of other people’s posts online I found an example which showed what I needed to do:

vpc_subnets=ec2.SubnetSelection(subnet_type= ec2.SubnetType.ISOLATED)

Turns out SubnetSelection isn’t a list of subnets, but a filter which generates a list of subnets. After looking over the SubnetSelection documentation again this became obvious.

Here is a more complete example of how to use SubnetSelection to specify the subnets for a Lambda function.

vpc = ec2.Vpc(...)

my_lambda = _lambda.Function(
    self, 'my_lambda',
    vpc_subnets=ec2.SubnetSelection(subnet_type= ec2.SubnetType.ISOLATED),
        'my_env_var_key': 'my_env_var_value',

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.