**Update: November 7 **- AWS has rolled out this feature to 8 more regions, including ap-southeast-2, Sydney. The other regions are N. California, Ireland, Paris, Mumbai, Singapore and San Paulo. This roll out brings up the number to 11 regions with Ohio, Frankfurt and Tokyo, which received the feature in late September. With this update, all AWS accounts will see the improvement for their Lambda functions with VPC networking in those regions.
AWS Lambda is the number one service that comes to mind to most of us when we think about serverless. In Lambda you can run code without worrying about provisioning any servers and you only pay for what you use.
Since Lambda was announced in 2014, it has had many new features added, making the service better and better. Faster execution times, 15 minutes executions, the list goes on and on. VPC networking was still not quite there… yet.
How VPC networking for Lambda was
If there was something that would give you a headache, and needed a better solution, it was AWS Lambda support for VPC. By default, Lambda functions are provisioned in a Service VPC, that is, a VPC managed by AWS over which you have no control. Any Lambda in this VPC would have Internet access, meaning your Lambda code could reach any public endpoint.
When accessing resources in your own VPC things got a little bit harder to manage. Configuring your Lambda function to access your VPC means an Elastic Network Interface, or ENI, is provisioned per instance of your Lambda function, and is created and attached during each cold start.
As your application scales, more instances of your Lambda functions are required to serve increasing requests, and more ENIs are provisioned. This causes several issues:
- When the Lambda Service scales your function, the first request that gets sent to the new instance suffers the cold start while the ENI is provisioned and attached. This happens even with the first ever invocation.
- Even if you configure your Lambda function across multiple subnets and AZs (for high availability), each instance of your Lambda function is attached through the ENI to 1 single subnet/AZ.
- As your application scales with demand the number of ENIs in your subnets grows. This has two side effects: you need to manage your ENI service limits (which is a per region limit, not per VPC, making it even harder if you have several VPCs with Lambda access in any given region) and each ENI consumes a private IP from your subnets range. This is not an easy limit to increase.
- When you delete a Lambda function you will have to wait until all ENIs have been deleted. This can take a while, and when you provision transient environments with automation it can be painful.
How VPC networking for Lambda will be
- On September the 3rd, AWS announced improved VPC networking for AWS Lambda. These improvements change the way Lambda provisions and now shares ENIs across Lambda functions and instances of those Lambda functions.
Now instead of the AWS Lambda service provisioning ENIs and attaching them to every new Lambda instance, it is basically doing the work upfront, and consolidating ENIs where possible. How?
- When you create a new Lambda function, the AWS Lambda service will create 1 ENI per subnet in your Lambda configuration and will be ready for the first request.
- When you create a Lambda function configured across a number of subnets, ideally across all AZs, and with a Security Group attached to it, the AWS Lambda service will determine if that combination is already in use by another Lambda function, and if so will reuse (share) the already existing ENIs. Zero provisioning and attachment time.
- All networking between your Lambda functions and your VPC is managed by the same technology used by the NLBs, which enables massive scaling capabilities without provisioning more ENIs.
- When you delete your Lambda function, if another function has the same subnet(s) and security group(s) combination, your Lambda function is deleted without waiting for ENIs to be deleted.
- You don’t have to change anything. If your Lambda functions are provisioned through the console, API or CloudFormation, they will continue to work.
Let’s compare old vs new
Now that 3 regions have the new feature available, we can run a comparison and see it firsthand.
Here is what we will do:
- Provision identical stacks in Sydney and Ohio regions.
- Each region will have a VPC across 3 AZs, with private and public subnets.
- A number of Lambda functions, with VPC access, will have different security group configurations: with no overlap, some overlap and full overlap.
- To provide a fair comparison a couple of Lambda functions will be provisioned to generate requests (and load) and avoid network differences (I’m in Sydney, Ohio is a bit far away from here)
What we are looking for is the difference in ENI provisioning
- When the Lambda function is provisioned
- When the Lambda function scales
- When the Lambda function is being deleted
You can clone this repo and provision the resources yourself using CloudFormation and Sceptre.
Once all resources are provisioned in Sydney and Ohio, let’s have a look at the Lambda functions and ENIs.