How to toggle Vpc configuration per stage?
Tags: serverless, aws, lambda, vpc
Published: 2019-11-05 03:57:59

This post is originally published on [our blog](https://innomizetech.com/blog/how-to-toggle-vpc-configuration-per-stage).

<!-- wp:paragraph -->
<p><a href="https://forum.serverless.com/t/exclude-vpc-config-for-different-enviornments/9756">This guy has asked the serverless forum</a> for a solution to implement it. That is why I wanted to write this post to describe how to toggle <strong>VPC</strong> configuration per stage. I hope it can help you and save your time :)</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:paragraph -->
<p>If you are working on the <a href="https://serverless.com/">Serverless</a> 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 <strong>VPC</strong> such as for non-production environments.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":310,"align":"center"} -->
<div class="wp-block-image"><figure class="aligncenter"><img src="https://i1.wp.com/blog.innomizetech.com/wp-content/uploads/2019/11/Screen-Shot-2019-11-05-at-10.41.12-AM.png?fit=640%2C291&amp;ssl=1" alt="" class="wp-image-310"/><figcaption>Vpc on AWS Lambda</figcaption></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Basically, we can disable <strong>Vpc</strong> configuration for Lambda function by either:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>Omit the <strong>vpc</strong> property in the <strong>provider</strong> configuration.</li><li>Or you can set both <strong>securityGroupIds</strong> and <strong>subnetIds</strong> as an empty array.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Based on the above logic, we can define a property in the <strong>custom</strong> prop as below:</p>
<!-- /wp:paragraph -->


{% gist https://gist.github.com/hoang-innomizetech/192192145f02c408cdb56f58e98bd4c6 %}

<!-- wp:paragraph -->
<p>With the above configurations, deploy to the <strong>dev</strong> stage will not add your lambda functions to a Vpc, but deploy to the <strong>prod</strong> stage will add your lambda functions to your desired Vpc. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can refer to some plugins I added in the <em>serverless.yml </em>file, it can be used to create your Vpc automatically or discover from the AWS account.</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a href="https://www.npmjs.com/package/serverless-vpc-plugin">serverless-vpc-plugin</a></li><li><a href="https://www.npmjs.com/package/serverless-vpc-discovery">serverless-vpc-discovery</a></li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>If you want to have a try, you can check out <a href="https://github.com/hoang-innomizetech/serverless-toggle-vpc-example">our repo</a> on Github.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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&nbsp;<a href="https://twitter.com/hoangleitvn">@hoangleitvn</a><br></p>
<!-- /wp:paragraph -->


Visit our [blog](https://innomizetech.com/blog) for more interesting articles. If you have any questions or need help you can contact me via [Twitter](https://twitter.com/hoangleitvn).