Amazon Aurora is a MySQL and PostgreSQL-compatible relational database that combines the performance and availability of traditional enterprise databases with the simplicity and cost-effectiveness of open source databases. Amazon Aurora Serverless is an on-demand, auto-scaling configuration for Amazon Aurora (MySQL-compatible and PostgreSQL-compatible editions), where the database will automatically start up, shut down, and scale capacity up or down based on your application's needs. It enables you to run your database in the cloud without managing any database instances. It's a simple, cost-effective option for infrequent, intermittent, or unpredictable workloads.
In this tutorial, you will learn how to create a serverless message processing application with Amazon Aurora Serverless (PostgreSQL-compatible edition), Data API for Aurora Serverless, Amazon Lambda, and Amazon Simple Notification Service (SNS). The tutorial will provide step-by-step instructions to create an Aurora Serverless database, use Data API to connect it with an Amazon Lambda funtion that consumes messages from Amazon SNS, and stores the messages in Aurora Serverless database.
About this Tutorial | |
---|---|
Time | 10-20 minutes |
Cost | Less than $1 |
Use Case | Databases |
Products | Amazon Aurora, Amazon SNS, AWS Lambda |
Level | 100 |
Last Updated | September 24, 2019 |
Step 1: Create your Aurora serverless database
1.1 — Open a browser and navigate to Amazon RDS console. If you already have an AWS account, login to the console. Otherwise, create a new AWS account to get started.
Already have an account? Log in to your account
Review and create
After a quick review of all the fields in the form, you can proceed.
1.17 — Click on “Create database”.
While the instances are being created, you will see a banner explaining how to obtain your credentials. This is a good opportunity to save the credentials somewhere, as this is the only time you will be able to view this password.
1.27 — Create a table with this query:
CREATE TABLE sample_table(received_at TIMESTAMP, message VARCHAR(255));
By connecting to the database with the Query Editor, a Secret is created that you will use later on in your Lambda function. Leave this tab open, as you will need to run some queries at the end of the tutorial.
Copy the secret ARN
Open a new tab and head to the AWS Secrets Manager. Then follow the steps below to retrieve the Secret ARN.
1.28 — Find the secret containing the “RDS database postgres credentials for database-1”.
Step 2: Create an AWS Cloud9 environment
Open a new tab and head to the AWS Cloud9 console. Then follow the steps below to create a new environment.
2.8 — In the following screens, click “Next”, “Next”, then “Finish”.
Now the environment is ready and you will see a blueprint for a Lambda Function.
Before you start coding you need to configure some permissions as described in Step 3. Keep this environment open and use other tabs for completing the upcoming steps.
Step 3: Configure permissions
Open a new tab and head to the AWS IAM console. Then follow the steps below to assign permissions to your Lambda function.
Step 4: Create your AWS Lambda function
Go back to the Cloud9 environment you created in step 2, then follow the instructions below.
4.3 — Delete the code in the lambda_function file and in its place copy this sample code.
Step 5: Create an Amazon SNS topic
Your Lambda Function will process messages from Amazon Simple Notification Service (SNS), which offers pub/sub messaging for microservices and serverless applications.
In a new tab, visit the SNS Dashboard and follow these instructions:
Step 6: Subscribe AWS Lambda Function to Amazon SNS topic
Go to the AWS Lambda Management Console and follow these instructions:
Step 7: Publish test message
Go back to the SNS Dashboard and follow these instructions:
Verify database changes
7.5 — Go to the tab where you left the Query Editor open. If you closed it, visit the RDS Dashboard and connect to the Query Editor as explained in 1.20.
Step 8: Cleanup
To finish this tutorial, you will learn how to delete your Aurora DB cluster when it's not needed anymore, along with the Lambda Function, the Cloud9 environment, the Secret for connecting to the database, and any other leftovers.
Delete your Lambda Function
8.1 — Go to the AWS Lambda Management Console and select your Lambda Function.
Delete your SNS topic
8.3 — Visit the SNS Dashboard and click on “Topics” on the left panel.
Delete your Cloud9 environment
8.6 — Head to the AWS Cloud9 console and select the environment you created for this tutorial.
Delete your Aurora Serverless database
8.9 — Go to the Amazon RDS console and select the database you created for this tutorial.
Delete your IAM role
8.15 — Go to the AWS IAM console. Click “Roles” and search for a role starting with “cloud9-”.
Congratulations
You have created an Aurora Serverless database and connected it with an AWS Lambda Function via Aurora's Data API. You configured Amazon Simple Notification Service (SNS) as a trigger for your Lambda Function, and the messages you sent via SNS were processed and stored in your Aurora Serverless database.
Recommended next steps
Learn more about Amazon Aurora features
Find out more about the features of Amazon Aurora with the Amazon Aurora User Guide.
Best practices with Amazon Aurora
Learn about general best practices and options for using or migrating data to an Amazon Aurora DB cluster.
Learn more about Serverless
If you want to know more about serverless applications, check the AWS Lambda Documentation, as well as the User Guide for Aurora.