DEV Community

Hoang Le
Hoang Le

Posted on • Edited on

5 1

How to toggle Vpc configuration per stage?

This post is originally published on our blog.

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 your 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 it from the world. But sometimes, you might want to run your function without VPC such as for non-production environments.

Vpc on AWS Lambda

Basically, we can disable Vpc configuration for 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:

With the above configurations, deploy to the dev stage will not add your lambda functions to a Vpc, but deploy 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

Visit our blog for more interesting articles. If you have any questions or need help you can contact me via Twitter.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay