Skip to Content

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

Lambda in a VPC: a Lambda function configured with a VPC creates an elastic network interface in a private subnet, in a Lambda security group, so it can reach an RDS database in the RDS security group; for internet access it needs a NAT gateway in a public subnet Lambda in a VPC: a Lambda function configured with a VPC creates an elastic network interface in a private subnet, in a Lambda security group, so it can reach an RDS database in the RDS security group; for internet access it needs a NAT gateway in a public subnet

Lambda reaches private resources through an ENI in the customer's subnet.
  • 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

Lambda with RDS Proxy: many Lambda functions deployed in the VPC connect to RDS Proxy in a private subnet, which pools their connections to the RDS database; because the proxy is never public, the functions must be in the VPC Lambda with RDS Proxy: many Lambda functions deployed in the VPC connect to RDS Proxy in a private subnet, which pools their connections to the RDS database; because the proxy is never public, the functions must be in the VPC

Many functions, a few pooled database connections.

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

Invoking Lambda from RDS for PostgreSQL or Aurora MySQL: a user registers, the database inserts the row and invokes a Lambda function, which sends a welcome email through Amazon SES; the database needs an outbound network path and IAM permission to invoke the function Invoking Lambda from RDS for PostgreSQL or Aurora MySQL: a user registers, the database inserts the row and invokes a Lambda function, which sends a welcome email through Amazon SES; the database needs an outbound network path and IAM permission to invoke the function

The database itself calls a function.
  • 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_lambda extension) and Aurora MySQL (the lambda_sync and lambda_async functions).
  • 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: events about the DB instance itself, such as stopped or snapshot created, are sent to Amazon SNS, whose subscribers can be SQS queues or Lambda functions, or to EventBridge, whose rules can target Lambda RDS event notifications: events about the DB instance itself, such as stopped or snapshot created, are sent to Amazon SNS, whose subscribers can be SQS queues or Lambda functions, or to EventBridge, whose rules can target Lambda

Events about the database, not about its data.
  • 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:

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt