Certopolis

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 AWSLambdaVPCAccessExecutionRole opens 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 Inactive and 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.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A developer attaches an AWS Lambda function to private subnets in a VPC so that the function can query an Amazon RDS database. After the change, the database queries succeed but the function's calls to a third-party payment API on the public internet time out. The function must reach both the database and the public API. Which solution will resolve this issue?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: D is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Outbound internet access from private subnets requires a NAT gateway in a public subnet with a route from the private subnets to it.

    Written from docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html

  2. A company runs 30 AWS Lambda functions that all query one Amazon RDS DB instance in the private subnets of a VPC. Each function was attached to those subnets with a security group of its own. The team wants Lambda to create as few Hyperplane elastic network interfaces as it can. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: D is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    Giving all 30 functions one shared security group over the same subnets collapses 30 combinations into one, so the functions share one set of interfaces. The runner-up cuts each function down to a single subnet, which does reduce interfaces per function, but because every function still carries a security group of its own, each one remains a distinct combination and Lambda still creates an interface for each.

    Written from docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html

  3. A company keeps an AWS Lambda function that runs a database failover routine. The function is attached to two private subnets and a security group so that it can reach an Amazon Aurora cluster. An on-call engineer invokes it from a runbook only during an incident, a few times a year. In the most recent incident the first invocation returned an error, and a repeat of the same command later completed normally. The routine must finish within 60 seconds. Which solution will meet these requirements?

    Right. In the game that’s Renown for your Development with AWS Services building.

    Not that one: A is right. In the game your Development with AWS Services building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A routine invoked a few times a year crosses fourteen idle days before every incident, so the first invocation of each incident is the one that fails, and the second one works because the interface has been rebuilt in the meantime. The same page says a function "transitions to the Active state only when the Hyperplane ENI is ready, which can take several minutes", so riding out the recovery is not compatible with a 60-second budget. The fix has to keep the function from ever reaching fourteen idle days, and a schedule that fires every 7 days does that at a cost of one extra invocation a week.

    Written from docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html

Ask for something