Member-only story
Using AWS Lambda with Amazon Aurora MySQL

Amazon Aurora is a fully managed relational database engine that is compatible with MySQL and PostgreSQL. In this article, I will explain how you can invoke an AWS Lambda function from an Amazon Aurora MySQL cluster with the native function lambda_sync
or lambda_async
.
Simple Use Case
We have an Amazon Aurora MySQL database with a table (user) for saving user information. We have a requirement that we need to notify when the application or DB users delete records from the user table.
How it works

- We have an Aurora MySQL database with the user table.
- There is a MySQL trigger associated with the user table.
- This trigger will be activated when the DELETE action is executed on the table.
- Inside the trigger, we call the lambda_sync function to invoke the Lambda function.
- Then Lambda function sends an SNS notification.
Steps
- Create a VPC
- Create an AWS Lambda function
- Create an SNS topic
- Create an IAM role for the Aurora MySQL DB cluster
- Associate the IAM role with the Aurora MySQL DB cluster
- Create an Aurora MySQL database cluster
- Setup an EC2 instance for connecting to the database
- Create a database, table, and trigger
- Testing
Note that I use AWS CDK for creating all the resources.
Create a VPC
We deploy our Aurora MySQL DB cluster in a private subnet of a VPC. However, the Lambda function resides outside the VPC. Therefore, Aurora MySQL won’t be able to invoke the Lambda function. To fix this, we can deploy either a NAT gateway in a public subnet and set the routes or create a VPC interface endpoint between the Aurora MySQL cluster and AWS Lambda function. In this example, I deploy a NAT gateway.