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.

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:

    zip pymysql_lambda_layer.zip pymysql_lambda.py
Create a Lambda Function
  1. Run the following command, giving your function a name and replacing <ROLE_ARN> with the ARN provided on the lab page:

    aws lambda create-function 
    --memory-size 512  
    --function-name <NAME_YOUR_FUNCTION> 
     --runtime python2.7 
    --handler pymysql_lambda.lambda_handler 
    --zip-file fileb://pymysql_lambda_layer.zip 
    --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/exercise_files/Section5-BackendLayer/pymysql_lambda_layer.zip

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, including the function name you provided when you created the function and replacing <LAYER_VERSION_ARN> with the ARN you just noted:

    aws lambda update-function-configuration --function-name <YOUR_FUNCTION_NAME> --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 Serverless database, providing the following values:
    • Select engine Aurora, and wait for serverless option to appear
    • DB Cluster Identifier (your unique DB Cluster name)
    • Master Username, Master User Password (read password instructions carefully!)
    • Scaling configuration (ACU) => Min Capacity = 1, Max Capacity = 2
    • Leave everything else at their default values
Modify Lambda Function to Add VPC and Subnet Access to It
  1. Navigate to Lambda.
  2. Select the Lambda function’s name you created via the CLI.
  3. Scroll down to the VPC settings, and change the VPC option from "No VPC" to the only VPC listed in the dropdown. Once you choose a VPC, you’ll be able to add subnets and security group.
  4. For Subnets, click and add the two available subnets.
  5. For Security groups, choose the default one.
  6. Click Save in the top right corner.
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, click Save in the top right corner.
Create Test Event for Lambda
  1. In the Lambda function properties, go to the top right corner, click on Configure test event, and create a new test event to test your Lambda function.
  2. Keep the Hello World template selected, give your test event a name, and replace the test event body with {}.
  3. Click Create.
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.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

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