- Lab
- A Cloud Guru
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.**
Path Info
Table of Contents
-
Challenge
Zip Up Lambda Code to Create a Deployable File to Lambda
-
Change to the
exercise_files
directory:cd exercise_files/Section5-BackendLayer/
-
Zip the Lambda code to create a deployable file to Lambda:
zip pymysql_function.zip pymysql_lambda.py
-
-
Challenge
Create a Lambda Function
-
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 ``` -
-
Challenge
Create Lambda Layer with Provided Zipped Python Library
-
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. -
-
Challenge
Update Lambda Function Configuration to Add Lambda Layer to It
-
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>
-
-
Challenge
Create RDS Serverless DB
- Log in to the AWS Management Console with the credentials provided on the lab page.
- 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
-
Challenge
Modify Lambda Function to Add VPC and Subnet Access to It
- Navigate to Lambda.
- Select your Lambda function's name.
- In the Configuration section, click VPC and Edit.
- Select the only available VPC.
- Click the Subnets dropdown, and add the two available subnets.
- For Security groups, choose the default security group.
- Click Save.
-
Challenge
Get the RDS Serverless DB's Endpoint URL and Plug It into the Lambda Function Code
- Navigate to RDS.
- Get the RDS Serverless database endpoint URL. Once you click on the database name, it should be under Connectivity & security tab.
- 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.
- Once you're done, save your code and click Deploy.
-
Challenge
Create Test Event for Lambda
- In the Lambda code source properties, click the box to the right of Test and select Configure test event.
- Keep the Hello World template selected, give your test event a name, and replace the test event body with
{}
. - Click Save.
-
Challenge
Test Your Lambda and Aurora RDS Serverless DB Integration Using PyMySQL
- 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.
- 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.
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.