Serverless Vpc Plugin

homepage icon
Follow @smoketurner


NPM Downloads Last Month
Watch Star Fork Issue Download License NPM Build Status Coverage Status Contributors

Repo README Contents:


serverless npm version MIT licensed npm downloads

Automatically creates an AWS Virtual Private Cloud (VPC) using all available Availability Zones (AZ) in a region.

This plugin provisions the following resources:

If the VPC is allocated a /16 subnet, each availability zone within the region will be allocated a /20 subnet. Within each availability zone, this plugin will further divide the subnets:

The subnetting layout was heavily inspired by the now shutdown Skyliner platform. 😞

Optionally, this plugin can also create AWS::EC2::NatGateway instances in each availability zone which requires provisioning AWS::EC2::EIP resources (AWS limits you to 5 per VPC, so if you want to provision your VPC across all 6 us-east availability zones, you’ll need to request an VPC EIP limit increase from AWS).

Instead of using the managed AWS::EC2::NatGateway instances, this plugin can also provision a single t2.micro NAT instance in PublicSubnet1 which will allow HTTP/HTTPS traffic from the “Application” subnets to reach the Internet.

Lambda functions will execute within the “Application” subnet and only be able to access:

If your Lambda functions need to access the internet, then you MUST provision NatGateway resources or a NAT instance.

By default, AWS::EC2::VPCEndpoint “Gateway” endpoints for S3 and DynamoDB will be provisioned within each availability zone to provide internal access to these services (there is no additional charge for using Gateway Type VPC endpoints). You can selectively control which AWS::EC2::VPCEndpoint “Interface” endpoints are available within your VPC using the services configuration option below. Not all AWS services are available in every region, so the plugin will query AWS to validate the services you have selected and notify you if any changes are required (there is an additional charge for using Interface Type VPC endpoints).

If you specify more then one availability zone, this plugin will also provision the following database-related resources (controlled using the subnetGroups plugin option):

to make it easier to create these resources across all of the availability zones.


$ npx sls plugin install -n serverless-vpc-plugin


# add in your serverless.yml

  - serverless-vpc-plugin

  # you do not need to provide the "vpc" section as this plugin will populate it automatically
      -  # plugin will add LambdaExecutionSecurityGroup to this list
      -  # plugin will add the "Application" subnets to this list

    # Whether plugin is enabled. Can be used to selectively disable plugin
    # on certain stages or configurations. Defaults to true.
    enabled: true

    cidrBlock: ''

    # if createNatGateway is a boolean "true", a NAT Gateway and EIP will be provisioned in each zone
    # if createNatGateway is a number, that number of NAT Gateways will be provisioned
    createNatGateway: 2

    # When enabled, the DB subnet will only be accessible from the Application subnet
    # Both the Public and Application subnets will be accessible from
    createNetworkAcl: false

    # Whether to create the DB subnet
    createDbSubnet: true

    # Whether to enable VPC flow logging to an S3 bucket
    createFlowLogs: false

    # Whether to create a bastion host
    createBastionHost: false
    bastionHostKeyName: MyKey # required if creating a bastion host

    # Whether to create a NAT instance
    createNatInstance: false

    # Whether to create AWS Systems Manager (SSM) Parameters
    createParameters: false

    # Optionally specify AZs (defaults to auto-discover all availabile AZs)
      - us-east-1a
      - us-east-1b
      - us-east-1c

    # By default, S3 and DynamoDB endpoints will be available within the VPC
    # see
    # for a list of available service endpoints to provision within the VPC
    # (varies per region)
      - kms
      - secretsmanager

    # Optionally specify subnet groups to create. If not provided, subnet groups
    # for RDS, Redshift, ElasticCache and DAX will be provisioned.
      - rds

    # Whether to export stack outputs so it may be consumed by other stacks
    exportOutputs: false

CloudFormation Outputs

After executing serverless deploy, the following CloudFormation Stack Outputs will be provided:

Exporting CloudFormation Outputs

Setting exportOutputs: true will export stack outputs. The name of the exported value will be prefixed by the cloud formation stack name (AWS::StackName). For example, the value of the VPC output of a stack named foo-prod will be exported as foo-prod-VPC.

SSM Parameters

Setting createParameters: true will create the below parameters in the AWS Systems Manager (SSM) Parameter Store:

As an example, if the stack name you want to reference is new-service-dev, you can then use Serverless’ built-in support for reading from SSM:

    - ${ssm:/SLS/new-service-dev/AppSecurityGroup}
  subnetIds: ${ssm:/SLS/new-service-dev/AppSubnets~split}

(Note the usage of ~split to split the SSM StringList parameter type and return an array of values)