Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Integrating Aurora Serverless with Lambda Functions Using Python and PyMySQL

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.**

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 45m
Published
Clock icon Nov 01, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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
      
  2. Challenge

    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://pymysql_lambda.zip
    --role "<ROLE_ARN>"
    --region us-east-1 ```

  3. Challenge

    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/pymysql_lambda_layer.zip
      

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

  4. Challenge

    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>
      
  5. Challenge

    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
  6. Challenge

    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.
  7. Challenge

    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.
  8. Challenge

    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.
  9. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans