AWS Certified Developer – Associate
Lambda VPC access to private resources for DVA-C03
The Lambda Developer Guide says you give a function access to resources in a VPC by attaching it to the private subnets that contain them, with a security group. Lambda creates Hyperplane ENIs using permissions in the execution role. Once attached, the function reaches the internet only if the VPC provides a route, such as a NAT gateway.
How does a Lambda function reach a private resource?
A function that must query an Amazon RDS instance or an ElastiCache cluster with no public endpoint is attached to the VPC through the private subnets that hold the resource. You choose the subnets and one or more security groups in the function's VPC configuration. AWS recommends subnets in at least two Availability Zones.
Lambda needs permission to create and manage network interfaces. The AWS managed policy AWSLambdaVPCAccessExecutionRole on the execution role grants it. A custom policy must allow ec2:CreateNetworkInterface, ec2:DescribeNetworkInterfaces, ec2:DescribeSubnets, ec2:DeleteNetworkInterface, ec2:AssignPrivateIpAddresses and ec2:UnassignPrivateIpAddresses on all resources.
These permissions are used to create the interfaces, not to invoke the function. They are also granted to the function's code, so AWS suggests a deny statement with the lambda:SourceFunctionArn condition key to stop the code calling those EC2 APIs.
What are Hyperplane ENIs?
A Hyperplane ENI is the managed network interface between the function and the VPC. Lambda creates one the first time a function is attached with a particular subnet and security group combination. Other functions in the account that use the same combination share it, so reusing the same subnets and security groups keeps the number of interfaces down. Each ENI supports up to 65,000 connections.
While the ENI is being created a new function is in the Pending state and cannot be invoked. It becomes Active when the ENI is ready, which can take several minutes.
If a function is idle for 14 days, Lambda reclaims unused Hyperplane ENIs and sets the function to Inactive. The next invocation fails, and the function goes back to Pending until an ENI is available again.
Internet access, NAT gateways and VPC endpoints
By default a function has access to the public internet. After you attach it to a VPC, it can reach only what that VPC can reach. To call a public API, attach the function to private subnets whose route table sends internet-bound traffic to a NAT gateway in a public subnet. Connecting a function to a public subnet does not give it internet access.
The same applies to AWS service APIs, because the default service endpoints are public. The AWS PrivateLink guide describes the alternative: create a VPC endpoint so that traffic to a supported AWS service stays private and does not need an internet gateway.
IAM condition keys lambda:VpcIds, lambda:SubnetIds and lambda:SecurityGroupIds let an administrator require or restrict VPC settings on CreateFunction and UpdateFunctionConfiguration.
Where the exam catches people
- Believing a function in a public subnet gets internet access: it does not. Use private subnets with a route to a NAT gateway in a public subnet.
- Believing
AWSLambdaVPCAccessExecutionRoleopens outbound network paths: it only lets Lambda create and manage the network interfaces. - Believing every function gets its own network interface: functions that use the same subnet and security group combination share a Hyperplane ENI.
- Believing a VPC-attached function is always ready to run: after 14 idle days it becomes
Inactiveand the next invocation fails while the ENI is rebuilt. - Believing a VPC endpoint gives access to a third-party API on the internet: VPC endpoints connect to supported AWS services, and public internet calls need a NAT gateway.