Private networking with VPC
Amazon Virtual Private Cloud (Amazon VPC) is a virtual network in the Amazon cloud, dedicated to your Amazon account. You can use Amazon VPC to create a private network for resources such as databases, cache instances, or internal services. For more information about Amazon VPC, see What is Amazon VPC?
A Lambda function always runs inside a VPC owned by the Lambda service. Lambda applies network access and security rules to this VPC and Lambda maintains and monitors the VPC automatically. If your Lambda function needs to access the resources in your account VPC, configure the function to access the VPC. Lambda provides managed resources named Hyperplane ENIs, which your Lambda function uses to connect from the Lambda VPC to an ENI (Elastic network interface) in your account VPC.
There's no additional charge for using a VPC or a Hyperplane ENI. There are charges for some VPC components,
such as NAT gateways. For more information, see Amazon VPC Pricing
Topics
VPC network elements
Amazon VPC networks includes the following network elements:
-
Elastic network interface – elastic network interface is a logical networking component in a VPC that represents a virtual network card.
-
Subnet – A range of IP addresses in your VPC. You can add Amazon resources to a specified subnet. Use a public subnet for resources that must connect to the internet, and a private subnet for resources that don't connect to the internet.
-
Security group – use security groups to control access to the Amazon resources in each subnet.
-
Access control list (ACL) – use a network ACL to provide additional security in a subnet. The default subnet ACL allows all inbound and outbound traffic.
-
Route table – contains a set of routes that Amazon uses to direct the network traffic for your VPC. You can explicitly associate a subnet with a particular route table. By default, the subnet is associated with the main route table.
-
Route – each route in a route table specifies a range of IP addresses and the destination where Lambda sends the traffic for that range. The route also specifies a target, which is the gateway, network interface, or connection through which to send the traffic.
-
NAT gateway – An Amazon Network Address Translation (NAT) service that controls access from a private VPC private subnet to the Internet.
-
VPC endpoints – You can use an Amazon VPC endpoint to create private connectivity to services hosted in Amazon, without requiring access over the internet or through a NAT device, VPN connection, or Amazon Direct Connect connection. For more information, see Amazon PrivateLink and VPC endpoints
.
Tip
To configure your Lambda function to access a VPC and subnet, you can use the Lambda Console or the API.
Refer to the VpcConfig
section in CreateFunction to configure your function. See Configuring VPC access (console) and Configuring VPC access (API) for detailed steps.
For more information about Amazon VPC networking definitions, see How Amazon VPC works in the Amazon VPC Developer Guide and the
Amazon VPC FAQs.
Connecting Lambda functions to your VPC
A Lambda function always runs inside a VPC owned by the Lambda service. By default, a Lambda function isn't connected to VPCs in your account. When you connect a function to a VPC in your account, the function can't access the internet unless your VPC provides access.
Lambda accesses resources in your VPC using a Hyperplane ENI. Hyperplane ENIs provide NAT capabilities from the Lambda VPC to your account VPC using VPC-to-VPC NAT (V2N). V2N provides connectivity from the Lambda VPC to your account VPC, but not in the other direction.
When you create a Lambda function (or update its VPC settings), Lambda allocates a Hyperplane ENI for each subnet in your function's VPC configuration. Multiple Lambda functions can share a network interface, if the functions share the same subnet and security group.
To connect to another Amazon service, you can use VPC endpoints
To give your function access to the internet, route outbound traffic to a NAT gateway in a public subnet. The NAT gateway has a public IP address and can connect to the internet through the VPC's internet gateway.
For information about how to configure Lambda VPC networking, see Connecting outbound networking to resources in a VPC and Connecting inbound interface VPC endpoints for Lambda.
Shared subnets
VPC sharing allows multiple Amazon accounts to create their application resources, such as Amazon EC2 instances and Lambda functions, in shared, centrally-managed virtual private clouds (VPCs). In this model, the account that owns the VPC (owner) shares one or more subnets with other accounts (participants) that belong to the same Amazon Organization.
To access private resources, connect your function to a private shared subnet in your VPC. The subnet owner must share a subnet with you before you can connect a function to it. The subnet owner can also unshare the subnet a later time, thereby removing connectivity. For details on how to share, unshare, and manage VPC resources in shared subnets, see How to share your VPC with other accounts
Lambda Hyperplane ENIs
The Hyperplane ENI is a managed network resource that the Lambda service creates and manages. Multiple
execution environments in the Lambda VPC can use a Hyperplane ENI to securely access resources inside of VPCs in
your account. Hyperplane ENIs provide NAT capabilities from the Lambda VPC to your account VPC. For more
information about Hyperplane ENIs, see Improved VPC networking for Amazon Lambda
functions
Each unique security group and subnet combination in your account requires a different network interface. Functions in the account that share the same security group and subnet combination use the same network interfaces.
Because the functions in your account share the ENI resources, the ENI lifecycle is more complex than other Lambda resources. The following sections describe the ENI lifecycle.
ENI lifecycle
Creating ENIs
Lambda may create Hyperplane ENI resources for a newly created VPC-enabled function or for a VPC configuration change to an existing function. The function remains in pending state while Lambda creates the required resources. When the Hyperplane ENI is ready, the function transitions to active state and the ENI becomes available for use. Lambda can require several minutes to create a Hyperplane ENI.
For a newly created VPC-enabled function, any invocations or other API actions that operate on the function fail until the function state transitions to active.
For a VPC configuration change to an existing function, any function invocations continue to use the Hyperplane ENI associated with the old subnet and security group configuration until the function state transitions to active.
If a Lambda function remains idle for consecutive weeks, Lambda reclaims the unused Hyperplane ENIs and sets the function state to idle. The next invocation causes Lambda to reactivate the idle function. The invocation fails, and the function enters pending state until Lambda completes the creation or allocation of a Hyperplane ENI.
For more information about function states, see Lambda function states.
Managing ENIs
Lambda uses permissions in your function's execution role to create and manage network interfaces. Lambda creates a Hyperplane ENI when you define a unique subnet plus security group combination for a VPC-enabled function in an account. Lambda reuses the Hyperplane ENI for other VPC-enabled functions in your account that use the same subnet and security group combination.
There is no quota on the number of Lambda functions that can use the same Hyperplane ENI. However, each Hyperplane ENI supports up to 65,000 connections/ports. If the number of connections exceeds 65,000, Lambda creates a new Hyperplane ENI to provide additional connections.
When you update your function configuration to access a different VPC, Lambda terminates connectivity to the Hyperplane ENI in the previous VPC. The process to update the connectivity to a new VPC can take several minutes. During this time, invocations to the function continue to use the previous VPC. After the update is complete, new invocations start using the Hyperplane ENI in the new VPC. At this point, the Lambda function is no longer connected to the previous VPC.
Deleting ENIs
When you update a function to remove its VPC configuration, Lambda requires up to 20 minutes to delete the attached Hyperplane ENI. Lambda only deletes the ENI if no other function (or published function version) is using that Hyperplane ENI.
Lambda relies on permissions in the function execution role to delete the Hyperplane ENI. If you delete the execution role before Lambda deletes the Hyperplane ENI, Lambda won't be able to delete the Hyperplane ENI. You can manually perform the deletion.
Lambda doesn't delete network interfaces that are in use by functions or function versions in your account.
You can use the Lambda ENI Finder
Connections
Lambda supports two types of connections: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).
When you create a VPC, Lambda automatically creates a set of DHCP options and associates them with the VPC. You
can configure your own DHCP options set for your VPC. For more details, refer to Amazon VPC DHCP options
Amazon provides a DNS server (the Amazon Route 53 resolver) for your VPC. For more information, see DNS support for your VPC
Security
Amazon provides security groups
For general security best practices, see VPC security best
practices
You can use Lambda-specific condition keys for VPC settings to provide additional permission controls for your Lambda functions. For more information about VPC condition keys, see Using IAM condition keys for VPC settings.
Note
Lambda functions can be invoked from the public internet or Amazon PrivateLink
Observability
You can use VPC Flow Logs
Note: when you attach a function to a VPC, the CloudWatch log messages do not use the VPC routes. Lambda sends them using the regular routing for logs.