How To Toggle AWS VPC Configuration Per Stage With Serverless Framework

This guy has asked the serverless forum for a solution to implement it. That is why I wanted to write this post to describe how to toggle VPC configuration per stage. I hope it can help you and save time 🙂

If you are working on the Serverless framework to build your application on AWS. You possibly wanted to run your Lambda functions under your private Vpc to protect your functions and hide them from the world. But sometimes, you might want to run your function without VPC such as for non-production environments.

AWS Lambda console: prod function with VPC set to No VPC

Vpc on AWS Lambda

Basically, we can disable Vpc configuration for the Lambda function by either:

  • Omit the vpc property in the provider configuration.
  • Or you can set both securityGroupIds and subnetIds as an empty array.

Based on the above logic, we can define a property in the custom prop as below:

custom:
  vpc:
    dev:
      # If you don't want to run Lambda under a Vpc, then just give it an empty array
      securityGroupIds: []
      subnetIds: []

    # You can add more stages here

    prod:
      securityGroupIds:
        Fn::Split:
          - ','
          - ${file(secrets.${self:provider.stage}.yml):VPC_SECURITY_GROUP_IDS}
      subnetIds:
        Fn::Split:
          - ','
          - ${file(secrets.${self:provider.stage}.yml):VPC_SUBNET_IDS}

Then you can update the provider config like this:

provider:
  name: aws
  ...
  vpc: ${self:custom.vpc.${self:provider.stage}}

With the above configurations, deploying to the dev stage will not add your lambda functions to a Vpc, but deploying to the prod stage will add your lambda functions to your desired Vpc.

You can refer to some plugins I added in the serverless.yml file, it can be used to create your Vpc automatically or discover from the AWS account.

If you want to have a try, you can check out our repo on GitHub.

Should you run into issues while practicing the steps outlined in this article, I encourage you to reach out to me. You can get in touch with me through my Twitter handle @hoangleitvn

Similar articles

Merge queue for AI coding agents

Merge Queue for AI Coding Agents: Lessons From 900 PRs

AI agents open pull requests faster than checks can run. How we built a merge queue with batching and bisect, what broke in 900 PRs, and the 9 rules we keep.

Software factory levels in 2026

Software Factory Levels in 2026: Six Axes Instead of One Ladder

A software factory has six axes: control, compute, context, trust, learning and runtime. Where the agents run is only one of them. Here is how we score ours, the rules we run, and which axis to raise first.

A modern development stack in 2026

What a Modern Development Stack Looks Like in 2026

Many posts explain how to choose a tech stack. Here is what we use in real projects in 2026, our preferences, where each default bends, and the five questions that decide when it should.