Unleashing the Power of AWS Aurora Serverless V2: Architecture, and Setup Guide with sample use case

Natarajan Mariyappan
7 min readFeb 2, 2023

--

The Aurora Serverless V2 database engine from Amazon Web Services (AWS) is a fully managed relational database engine that expands compute and storage capacity automatically, letting you concentrate on your applications rather than maintaining your database infrastructure. In this article, we’ll examine the use cases for AWS Aurora Serverless V2, delve into its architecture, and provide you with a step-by-step tutorial for setting up your own Aurora serverless database.

Aurora Sefverless Usecases

1. Varying Workloads: Aurora Serverless V2 is perfect for applications with fluctuating workloads since it can scale up or down fast to handle traffic changes.

2. Development and Testing: Aurora Serverless V2 is a great option for development and testing environments since it can scale down to zero and pause during times of inactivity. In these settings, usage can be erratic.

3. Data Warehousing: Aurora Serverless V2 is a good option for business intelligence and data warehousing applications due to its parallel query capabilities.

4. Multi-tenant applications: Aurora Serverless V2 support for multi-master clustering and fine-grained access control enables Aurora Serverless V2 to provide the performance and isolation required for multi-tenant applications.

Advanced Architecture

Aurora Serverless V2 is built on the foundations of Amazon Aurora, which itself is based on MySQL and PostgreSQL compatibility. The key components of its advanced architecture include:

  1. Storage Auto-scaling: Aurora Serverless V2 automatically scales storage capacity without any downtime, growing in 10 GB increments up to 128 TB. This feature eliminates the need for manual storage provisioning, reducing operational overhead.
  2. Capacity Auto-scaling: Compute capacity is scaled automatically based on the actual workload, with instant scaling for both read and write operations. Aurora Serverless V2 can scale from 0.5 ACU (Aurora Capacity Units) to 488 ACUs, allowing it to adapt to changing workloads with minimal latency.
  3. Multi-master Clustering: Aurora Serverless V2 supports multi-master clustering, which enables multiple writer nodes to operate concurrently. This architecture enhances fault tolerance and ensures high availability, as any node can take over if another becomes unavailable.
  4. Parallel Query: The parallel query feature improves query performance by distributing the processing of large datasets across multiple nodes. This results in faster response times for analytical queries and reduces the need for complex ETL processes.
  5. Data Security: Aurora Serverless V2 provides built-in security features, such as encryption at rest, encryption in transit, and fine-grained access control. These features ensure that your data is protected and accessible only to authorized users.

Setup Guide

Follow these steps to create an Aurora Serverless V2 database:

  1. Sign in to the AWS Management Console and navigate to the RDS service.
  2. Click “Create database” and select “Amazon Aurora” as the engine type. Choose either “Amazon Aurora with MySQL compatibility” or “Amazon Aurora with PostgreSQL compatibility” based on your requirements.
  3. In the “Capacity type” section,
  4. select “Serverless” to create an Aurora Serverless V2 database.
  5. Configure the settings for your database, such as the database identifier, master username, and master password.
  6. Under the “Advanced settings” section, select a VPC, subnet group, and security group for your database. It’s recommended to create a new security group that allows inbound traffic from your application’s IP address or security group.
  7. Configure additional settings, such as database port, parameter groups, and backup retention period, as per your requirements.
  8. Click “Create Database” to initiate the creation of your Aurora Serverless V2 database. The process may take several minutes to complete.
  9. Once the database is available, you can connect to it using your preferred database management tool or through your application. Make sure to use the provided endpoint for Aurora Serverless V2, which will be in the format <DBClusterIdentifier>.cluster-<RandomString>.<Region>.rds.amazonaws.com.

Sample Code

Here’s a sample Python code snippet that connects to an Aurora Serverless V2 database using the ‘mysql-connector-python’ library:

import mysql.connector
# Replace these values with your database credentials
DB_ENDPOINT = "your_database_endpoint"
DB_USERNAME = "your_database_username"
DB_PASSWORD = "your_database_password"
DB_NAME = "your_database_name"
# Connect to the Aurora Serverless V2 database
connection = mysql.connector.connect(
host=DB_ENDPOINT,
user=DB_USERNAME,
password=DB_PASSWORD,
database=DB_NAME
)
# Perform a sample query
cursor = connection.cursor()
cursor.execute("SELECT * FROM your_table_name;")
rows = cursor.fetchall()
# Print the results
for row in rows:
print(row)

Close the connection:

For PostgreSQL compatibility, you can use the 'psycopg2' library:
```python
import psycopg2
# Replace these values with your database credentials
DB_ENDPOINT = "your_database_endpoint"
DB_USERNAME = "your_database_username"
DB_PASSWORD = "your_database_password"
DB_NAME = "your_database_name"
# Connect to the Aurora Serverless V2 database
connection = psycopg2.connect(
host=DB_ENDPOINT,
user=DB_USERNAME,
password=DB_PASSWORD,
dbname=DB_NAME
)
# Perform a sample query
cursor = connection.cursor()
cursor.execute("SELECT * FROM your_table_name;")
rows = cursor.fetchall()
# Print the results
for row in rows:
print(row)
# Close the connection
cursor.close()
connection.close()

Sample Usecase to address flash sales e-commerce website with Aurora Serverless 2: You must make provisions for the peak load you anticipate when using a typical database to perform such marketing initiatives on a regular basis. Or, if you only run them sometimes, you need to configure your database to accommodate the anticipated boost in traffic during the sale. You are only as big as the capacity you think you need in both situations. If sales are higher than you anticipated, what happens? There could be a drop in service if your database is unable to handle the load. Alternatively, what happens if your marketing strategy doesn’t result in the anticipated sales? You are paying for the capability you don’t require that you don’t need.

In this Sample use case, the transactional database is Aurora Serverless v2. In order to call the Amazon Lambda function, To build a serverless architecture on AWS to handle a flash sale with Amazon Aurora Serverless v2, you can follow these steps:

1. Set up AWS Aurora Serverless V2 and other AWS resources

  • Create an Amazon Aurora Serverless v2 cluster with MySQL compatibility
  • Create an AWS Lambda function to handle incoming requests
  • Create an Amazon API Gateway to expose your Lambda function as a REST API
  • Set up Amazon S3 for static website hosting (optional)

2. Connect your e-commerce site to your new AWS resources

Here’s a high-level architecture diagram:

Aurora Serverless Architecture for the sample use case

Here’s a basic example of an AWS Lambda function to handle incoming purchase requests:

import json
import boto3
import os
from botocore.exceptions import ClientError
rds = boto3.client('rds-data')CLUSTER_ARN = os.environ['CLUSTER_ARN']
SECRET_ARN = os.environ['SECRET_ARN']
DATABASE_NAME = os.environ['DATABASE_NAME']
def lambda_handler(event, context):
try:
product_id = event['pathParameters']['product_id']
user_id = event['requestContext']['identity']['cognitoIdentityId']
stock_sql = f"SELECT stock FROM products WHERE id = '{product_id}';"
stock_response = execute_statement(stock_sql)
stock = stock_response['records'][0][0]['longValue']
if stock <= 0:
return {
'statusCode': 200,
'body': json.dumps({'message': 'Product is out of stock'})
}
update_stock_sql = f"UPDATE products SET stock = stock - 1 WHERE id = '{product_id}';"
execute_statement(update_stock_sql)
insert_order_sql = f"INSERT INTO orders (user_id, product_id) VALUES ('{user_id}', '{product_id}');"
execute_statement(insert_order_sql)
return {
'statusCode': 200,
'body': json.dumps({'message': 'Purchase successful'})
}
except ClientError as e:
print(e.response['Error']['Message'])
return {
'statusCode': 500,
'body': json.dumps({'message': 'An error occurred'})
}
def execute_statement(sql):
return rds.execute_statement(
resourceArn=CLUSTER_ARN,
secretArn=SECRET_ARN,
database=DATABASE_NAME,
sql=sql
)

To set up your infrastructure, follow these steps:

  1. Create an Amazon Aurora Serverless v2 cluster with MySQL compatibility:
  • Navigate to the Amazon RDS console and create a new Aurora Serverless v2 database cluster.
  • Choose MySQL 5.7 or 8.0 compatibility.
  • Configure the required settings, including VPC, security groups, and database names.

2. Create the necessary database tables:

Connect to your new Aurora Serverless v2 cluster using your favorite SQL client and create the required tables.

CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT NOT NULL
);
CREATE TABLE orders (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id VARCHAR(255) NOT NULL,
product_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

3. Create an AWS Lambda function:

  • Navigate to the AWS Lambda console and create a new function using the provided Python code.
  • Set the required environment variables: CLUSTER_ARN, SECRET_ARN, and DATABASE_NAME.
  • Add the required IAM permissions to access the Aurora Serverless v2 cluster.

4. Create an Amazon API Gateway:

  • Navigate to the Amazon API Gateway console and create a new REST API.
  • Create a new resource with a path parameter (e.g., /products/{product_id}/purchase).
  • Add a POST method to the resource and integrate it with your Lambda function.

5. Set up Amazon S3 for static website hosting (optional):

  • Create a new S3 bucket for your static files (HTML, CSS, and JavaScript).
  • Enable static website hosting for the bucket and set the index document.
  • Upload your e-commerce site’s static files to the bucket.
  • If you want to use a custom domain, set up Route 53 and CloudFront for your site.

Connect your e-commerce site to your new AWS resources:

  • Update your site’s code to send purchase requests to the API Gateway endpoint.
  • Replace the sample endpoint with the actual one generated by the API Gateway.

Now your architecture is set up to handle flash sales using Amazon Aurora Serverless v2, AWS Lambda, and Amazon API Gateway. The infrastructure will automatically scale based on demand during the sale, ensuring a seamless experience for your customers.

Conclusion:

With support for a range of use cases, including development and testing, multi-tenant applications, and data warehousing, Amazon Aurora Serverless V2 delivers a scalable, economical, and fully managed solution for relational databases. It is a fantastic option for companies trying to improve their database infrastructure because of its cutting-edge architecture, auto-scaling capabilities, and built-in security measures. Use the setup instructions and example code and Sample use case in this blog to begin utilizing Aurora Serverless V2’s capabilities.

--

--