Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless Framework add Lambda to an Existing VPC and Subnet

Is it possible to create a Serverless Framework Lambda deployment where the Lambda is deployed into an existing VPC's SecurityGroup? I don't want the service deployment or it's stack to own an of the network artifacts?

like image 844
Ryan Fisch Avatar asked May 23 '18 22:05

Ryan Fisch


People also ask

Can a Lambda call another Lambda in a VPC?

If your Lambda function is VPC attached, it needs to be able to communicate via your VPC to the AWS API. Lambdas do not talk to other Lambdas over the network, they initiate requests with the AWS API or an API Gateway, which passes the request on to the Lambda Function.

Can you invoke a Lambda in a VPC?

You can call any of the Lambda API operations from your VPC. For example, you can invoke the Lambda function by calling the Invoke API from within your VPC. For the full list of Lambda APIs, see Actions in the Lambda API reference.

How do I make Lambda available in multiple VPCs?

This is not possible with Lambda. Lambda functions can provide access only to one single VPC. If there are multiple subnets and are specified, then they must all be in the same VPC. You then can connect to the other VPCs by peering your VPCs.


3 Answers

Yes it is. The vpc configuration in serverless.yml just needs to reference existing subnets and security groups. Something like this:

vpc:
    securityGroupIds:
      - securityGroupId1
      - securityGroupId2
    subnetIds:
      - subnetId1
      - subnetId2

Take a look at https://serverless.com/framework/docs/providers/aws/guide/functions/#vpc-configuration

like image 152
Brian Winant Avatar answered Oct 16 '22 02:10

Brian Winant


The following setup worked perfectly for me in Serverless version 1.51.0. I included staging variables, since my environments use different subnets and security groups for logical isolation. My network setup is an already existing VPC with subnets and security groups.

provider:
  name: aws
  ....
  ....
  vpc:
    securityGroupIds:
      - ${self:custom.securityGroupId.${self:provider.stage}}
    subnetIds:
      - ${self:custom.subnetId.${self:provider.stage}}

custom:
  stages:
    - tst
    - dev
    - prd
  securityGroupId:
    local: sg-local
    tst: sg-tst
    dev: sg-dev
    prd: sg-prd
  subnetId:
    local: subnet-local
    tst: subnet-tst
    dev: subnet-dev
    prd: subnet-prd


plugins:
  - serverless-stage-manager
like image 21
Nebulastic Avatar answered Oct 16 '22 01:10

Nebulastic


An extension to the answer provided by @Nebulastic.

This is when you want to configure your VPC Lambda's to execute from more than one subnet for various Stages.

provider:
  name: aws
  vpc:
    securityGroupIds:
      - ${self:custom.securityGroupId.${self:provider.stage}}
    subnetIds:
      - ${self:custom.subnetId1.${self:provider.stage}}
      - ${self:custom.subnetId2.${self:provider.stage}}
      - ${self:custom.subnetId3.${self:provider.stage}}

custom:
  stage: ${opt:stage, self:provider.stage}

  securityGroupId:
    prod: sgId-prod
    test: sgId-test
    dev: sgId-dev
  subnetId1:
    prod: subnetId1-prod
    test: subnetId1-test
    dev: subnetId1-dev
  subnetId2:
    prod: subnetId2-prod
    test: subnetId2-test
    dev: subnetId2-dev
  subnetId2:
    prod: subnetId3-prod
    test: subnetId3-test
    dev: subnetId3-dev
like image 33
kiran01bm Avatar answered Oct 16 '22 02:10

kiran01bm