Skip to main content

Integrating Aurora Serverless with Lambda Functions Using Python and PyMySQL

Hands-On Lab

 

Photo of Moosa Khalid

Moosa Khalid

AWS Training Architect II

Length

01:45:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Integrating Aurora Serverless with Lambda Functions Using Python and PyMySQL

Introduction

There's more than one way to connect with Aurora RDS Serverless databases. It's possible to connecte to it using the RDS Data API, but if there are some permissions constraints within your organization making you unable to use the RDS Data API, the Python Library PyMySQL can be used as an independently available library to integrate with our serverless RDS DB.

Solution

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>

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_function.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_function.zip 
    --role "<ROLE_ARN>"

Create Lambda Layer with Provided Zipped Python Library

  1. Change to the home directory:

    cd /home/cloud_user
  2. 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/pymysql_lambda_layer.zip
  3. In the resulting output, copy the LayerVersionArn and paste it into a text file, as we'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 copied:

    aws lambda update-function-configuration --function-name <YOUR_FUNCTION_NAME> --layers <LAYER_VERSION_ARN>

Create RDS Serverless DB

  1. In the browser, log in to the AWS Management Console using the credentials provided on the lab page.
  2. Navigate to RDS.
  3. Click Databases in the left-hand menu.
  4. Click Create database.
  5. With Standard Create selected, set the Engine type to Amazon Aurora.
  6. Set Edition to Amazon Aurora with MySQL compatibility.
  7. Set Database features to Serverless.
  8. In the Settings section, set the following values:
    • DB instance identifier: linuxacademy-testdb
    • Master username: admin
    • Master password: Enter a password you'll be able to remember later
  9. In the Capacity settings section, set Maximum Aurora capacity unit to 2.
  10. Click Create database.

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 Network section, click the dropdown (where the default is No VPC) and select the only VPC listed in the dropdown. When the VPC is selected, the subnets and security group options will appear.
  4. Click the Subnets dropdown, and add the two available subnets.
  5. For Security groups, choose the default security group.
  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. In a new browser tab, navigate to RDS.
  2. Click our listed database name, and then click the Connectivity & security tab.
  3. Copy the listed endpoint URL.
  4. Back in the Lambda tab, scroll to the Function code section.
  5. In the function code, update the following values:
    • host: Paste the endpoint URL
    • user: admin
    • password: The password you set up when creating the RDS Serverless DB
  6. Click Save in the top right corner.

Create Test Event for Lambda

  1. In the Lambda function properties, click the box to the left of Test and select Configure test event.
  2. Give it an Event name of "test1".
  3. Replace the test event body with {}.
  4. Leave the other defaults, and click Create.

Test Your Lambda and Aurora RDS Serverless DB Integration Using PyMySQL

  1. With our test1 selected in the top right corner, click Test. You should get a successful execution log and output of the SQL query embedded within the code.

Conclusion

Congratulations on successfully completing this hands-on lab!