Lambda networking and RDS
Where Lambda runs by default, connecting Lambda to a VPC, using RDS Proxy with Lambda, invoking Lambda from RDS and Aurora, and RDS event notifications.
Lambda by default
By default, a Lambda function runs outside the customer's VPC, in a VPC owned by AWS.
- It can reach the public internet and public AWS services such as DynamoDB, S3 and SQS.
- It can't reach resources inside the customer's VPC: a private RDS database, ElastiCache, an internal load balancer.
Lambda in a VPC
- The function is configured with the VPC ID, the subnets and the security groups.
- Lambda creates an ENI (elastic network interface) in those subnets, and uses it to reach resources such as an RDS database.
- The database's security group must allow traffic from the Lambda security group.
- The function's role needs permission to manage those network interfaces (the managed policy
AWSLambdaVPCAccessExecutionRole). - Internet access: a function in a VPC doesn't get a public IP, even in a public subnet. To reach the internet, put it in private subnets with a route to a NAT gateway in a public subnet. To reach AWS services privately, use VPC endpoints. See NAT gateways and VPC endpoints.
Lambda with RDS Proxy
If Lambda functions access a database directly, they may open too many connections under high load: each concurrent function opens its own. RDS Proxy:
- improves scalability by pooling and sharing database connections;
- improves availability, cutting failover time by up to 66% and keeping connections open;
- improves security by enforcing IAM authentication and storing credentials in Secrets Manager.
The Lambda function must be deployed in the VPC, because RDS Proxy is never publicly accessible.
Invoking Lambda from RDS and Aurora
- Invoke Lambda functions from within the DB instance, to process data events in the database (for example, a new row in the users table sends a welcome email).
- Supported for RDS for PostgreSQL (the
aws_lambdaextension) and Aurora MySQL (thelambda_syncandlambda_asyncfunctions). - Outbound traffic must be allowed from the DB instance to Lambda: through a public route, a NAT gateway or VPC endpoints.
- The DB instance needs permission to invoke the function: an IAM role on the database (and the function's resource-based policy where needed).
RDS event notifications
- RDS event notifications report on the DB instance itself: created, stopped, started, failed over…
- They give no information about the data in it. (For data events, invoke Lambda from the database as above, or use DynamoDB Streams with DynamoDB.)
- Event categories: DB instance, DB snapshot, DB parameter group, DB security group, RDS Proxy, custom engine version.
- Near real time (up to 5 minutes).
- Send notifications to SNS, or subscribe to the events with EventBridge.
Real-world examples
- An order API on Lambda reads and writes a private Aurora database through RDS Proxy, so a burst of 2,000 concurrent requests doesn't exhaust connections.
- A function in a VPC that calls a third-party payment API reaches the internet through a NAT gateway.
- A DBA team gets an SNS email whenever a production database fails over or a snapshot fails.
Exam tips
- "Lambda can't connect to a private RDS database" → configure the function with the VPC, subnets and security groups.
- "Lambda in a VPC can't reach the internet" → private subnet + NAT gateway (or VPC endpoints for AWS services).
- "Too many database connections from Lambda" → RDS Proxy (and the function must be in the VPC).
- "Run code when a row is inserted in RDS PostgreSQL or Aurora MySQL" → invoke Lambda from the database.
- "Be notified when a DB instance is stopped or a snapshot completes" → RDS event notifications to SNS or EventBridge.
Common traps
- "Put the Lambda function in a public subnet to give it internet access." It gets no public IP; use a NAT gateway.
- "RDS event notifications tell you when data changes." They're about the instance, not the data.
- "Lambda outside the VPC can use RDS Proxy." The proxy is only reachable inside the VPC.
Check yourself
A VPC lab needs a NAT gateway (about $0.045 an hour) and a database, so instead:
Keep the function in the private subnets with a security group the database allows, and route 0.0.0.0/0 to a NAT gateway in a public subnet for the weather API.
Put RDS Proxy in front of the database and point the functions (in the VPC) at the proxy endpoint, so connections are pooled.
AWS documentation
Lambda limits and concurrency
The Lambda limits to know, concurrency and throttling, reserved concurrency, asynchronous retries, cold starts, provisioned concurrency and SnapStart.
Amazon API Gateway
API Gateway as the serverless front door for APIs, REST vs HTTP APIs, integrations with Lambda, HTTP and AWS services, endpoint types, security, throttling, caching and limits.