question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[aws-ec2] Cannot get VPC from ID for Lambda deploy, results in multiple builds

See original GitHub issue

❓ General Issue

I have a Lambda function deploying and I am trying to attach it to a VPC.

I am migrating from Serverless Framework (SLS) to CDK. With SLS I specified a security group ID and a subnet ID, but it looks like CDK requires a VPC ID specified as well.

However although I could deploy the Lambda function successfully though the same user, with CDK it tells me I don’t have permissions:

const vpcLambdaDB = cdkEC2.Vpc.fromLookup(this, 'vpcLambdaDB', {
  vpcId: 'vpc-13851b74',
});
[Error at /my-stack] You are not authorized to perform this operation.

It doesn’t tell me what permissions I need, so I tried adding DescribeVpcs as it was the only one that appeared relevant, however this did not work. I would rather not add any extra permissions at all, since this worked with SLS so I know these permissions allow me to deploy a Lambda into a VPC. Is there a way to have it just accept the VPC ID I am supplying without requiring special permissions to look it up? Or is there a way to forget the VPC and just supply the security group ID and subnet ID as I did with SLS? The docs say these are ignored if VPC is not supplied in the CDK Lambda function’s options.

Also, since I added the above Vpc.fromLookup() call, my Lambda functions get built two, sometimes three times over:

Bundling asset my-stack/my-lambda/Code/Stage...
../../../asset-output/my-lambda.js    965.2 KB    356ms
Bundling asset my-stack/my-lambda/Code/Stage...
../../../asset-output/my-lambda.js    965.2 KB    363ms

This happens even if I don’t specify the VPC on the Lambda function - just having the lookup command and discarding the response causes multiple builds. This only seems to have happened since upgrading to CDK 1.73.0. When I was using 1.71.0 it was only being built once, but I upgraded to see if the VPC request would work without changing permissions and now everything is being built multiple times. If I comment out the Vpc.fromLookup() then things go back to being built only once.

Environment

  • CDK CLI Version: 1.73.0 (build eb6f3a9)
  • Module Version: 1.73.0
  • Node.js Version: v14.13.0
  • OS: Arch Linux
  • Language (Version): NodeJS

(as this is technically a support question if you would like me to open an AWS Support case and post the number here let me know and I will do so, if that would help)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
adam-nielsencommented, Apr 20, 2021

I’m afraid I can’t remember exactly but I thought the error message told me. I think they were EC2 permissions, either DescribeVpc* or possibly DescribeRouteTables.

0reactions
gwenael-hagenmullercommented, Mar 9, 2022

I hit the same issue (tested only with CDK 1.23.0 (build 01f326e)) and using AWS CloudTrail, I found the API calls made by the method and so the 3 required permissions:

  • DescribeVpnGateways
  • DescribeSubnets
  • DescribeRouteTables

AWS IAM Policy Simulator can then be used to check if those permissions are allowed or denied.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring a Lambda function to access resources in a VPC
Managing VPC connections. Multiple functions can share a network interface, if the functions share the same subnet and security group. Connecting additional ...
Read more >
How to import existing VPC in aws cdk? - Stack Overflow
I am trying to get existing non-default vpc. I tried below options. vpc = ec2.Vpc.from_lookup(self, id = "VPC", vpc_id='vpcid', vpc_name=' ...
Read more >
AWS Lambda Guide - Serverless.yml Reference
Configure the S3 bucket used by Serverless Framework to deploy code packages to Lambda. deploymentBucket: # Name of an existing bucket to use...
Read more >
Using AWS VPC | Crosswalk - Pulumi
Getting the Default VPC. Often resources like clusters, API gateways, lambdas, and more, will request a VPC object or ID. This ensures such...
Read more >
Cdk use existing vpc - Seba Online
The build and deployment of the application if fully automated using AWS CDK. ... I want to assign the lambda function to specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found