-2

I need to add my lambda function to VPC but using javascript code - no from the aws console.

I'm creating my lambda in AWS CDK stack like that:

const myLambda = new lambda.Function(this, lambda-id, {
    code: code,
    handler: handler,
    runtime: runtime,
    ...
    **vpc**: 
})

I think I need to pass VPC as an argument to this function. Is there any way to fetch this VPC by its id and then pass it as argument to this function?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • 2
    https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html#vpc and https://stackoverflow.com/a/59302032/2442804 – luk2302 Jul 17 '21 at 11:41

1 Answers1

1

You can import an existing VPC by ID and provide it as an attribute on your lambda like:

const vpc = ec2.Vpc.fromLookup(this, 'VPC', {
  vpcId: 'your vpc id'
})

const myLambda = new lambda.Function(this, 'your-lambda', {
  code,
  handler,
  runtime,
  ...,
  vpc
})
Ryan
  • 1,151
  • 5
  • 7