Integrating Aurora Serverless with Lambda Functions Using Python and PyMySQL

1.75 hours
  • 9 Learning Objectives

About this Hands-on Lab

There’s more than one way to connect with Aurora RDS Serverless databases.
AWS already offers the RDS Data service API, which is an HTTP endpoint for connecting and querying your serverless RDS database instances , however there are limits to how much rows and data SQL query select calls can return through the RDS Data API(it’s 1000 rows or 1MB of data).
An alternative to using RDS Data API is to use the independent MySQL Python client called PyMySQL, which offers you the flexibility to establish connection, query and obtain larger quantity of operation on MySQL db, in our case Serverless RDS DB.
It establishes direct connection with the Serverless RDS Database unlike using an HTTP endpoint like RDS Data API.
**Note: The lab has been updated from Python2 to Python3, refer to lab guide for newest lab steps.**

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Zip Up Lambda Code to Create a Deployable File to Lambda
  1. Change to the exercise_files directory:

    cd exercise_files/Section5-BackendLayer/
  2. Zip the Lambda code to create a deployable file to Lambda:

Create a Lambda Function
  1. Run the following command, replacing <ROLE_ARN> with the ARN provided on the lab page:

    aws lambda create-function 
    --memory-size 512  
    --function-name pymysql-function 
     --runtime python3.7 
    --handler pymysql_lambda.lambda_handler 
    --zip-file fileb:// 
    --role "<ROLE_ARN>" 
    --region us-east-1
Create Lambda Layer with Provided Zipped Python Library
  1. Create the Lambda layer with the provided zipped Python library:

    aws lambda publish-layer-version --layer-name pymysql-layer --zip-file fileb:///home/cloud_user/

After successful command execution, note the LayerVersionArn, as you’ll need it in the next command.

Update Lambda Function Configuration to Add Lambda Layer to It
  1. Run the following command, replacing <LAYER_VERSION_ARN> with the ARN you just noted:

    aws lambda update-function-configuration --function-name pymysql-function --layers <LAYER_VERSION_ARN>
Create RDS Serverless DB
  1. Log in to the AWS Management Console with the credentials provided on the lab page.
  2. Create an RDS database, providing the following values:
    • With Standard create selected, set the Engine type to Aurora (MySQL Compatible)
    • Under Instance configuration > DB instance class, select Burstable classes (includes t classes) and db.t3.small
    • DB Cluster Identifier (your unique DB Cluster name)
    • Master Username, Master User Password (read password instructions carefully!)
    • Leave everything else at their default values
Modify Lambda Function to Add VPC and Subnet Access to It
  1. Navigate to Lambda.
  2. Select your Lambda function’s name.
  3. In the Configuration section, click VPC and Edit.
  4. Select the only available VPC.
  5. Click the Subnets dropdown, and add the two available subnets.
  6. For Security groups, choose the default security group.
  7. Click Save.
Get the RDS Serverless DB’s Endpoint URL and Plug It into the Lambda Function Code
  1. Navigate to RDS.
  2. Get the RDS Serverless database endpoint URL. Once you click on the database name, it should be under Connectivity & security tab.
  3. Head over to the Lambda function and replace the DB host URL, username, and password with the actual values you set up when creating the RDS Serverless DB.
  4. Once you’re done, save your code and click Deploy.
Create Test Event for Lambda
  1. In the Lambda code source properties, click the box to the right of Test and select Configure test event.
  2. Keep the Hello World template selected, give your test event a name, and replace the test event body with {}.
  3. Click Save.
Test Your Lambda and Aurora RDS Serverless DB Integration Using PyMySQL
  1. Ensure your RDS Serverless database is in the available state. You can do that by heading to RDS and checking the status against your DB.
  2. On the Lambda properties page for your function, make sure your test event in the top right corner is selected and hit Test. You should get a successful execution log and output of the SQL query embedded within the code.

Additional Resources

You work for an online furniture store and the back end of your company website is supported by MySQL. The company has recently shifted over to AWS and is leaning toward using the RDS Serverless offering to get its cost benefits during erratic traffic. Your department (DevOps) has been asked to provide a demonstration of integration of Lambda with the serverless DB, and since your team is already proficient with Python, you choose to go with Python's PyMySQL library to showcase the integration.

To get started, open a terminal session and log in to the provided EC2 instance via SSH using the credentials listed on the lab page:

ssh cloud_user@<PUBLIC IP>

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?