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.
Successfully complete this lab by achieving the following learning objectives:
- Zip Up Lambda Code to Create a Deployable File to Lambda
Change to the
Zip the Lambda code to create a deployable file to Lambda:
zip pymysql_function.zip pymysql_lambda.py
- Create a Lambda Function
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
Change to the
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.
- Update Lambda Function Configuration to Add Lambda Layer to It
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
- Log in to the AWS Management Console with the credentials provided on the lab page.
- 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
- Navigate to Lambda.
- Select the Lambda function’s name you created via the CLI.
- 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.
- For Subnets, click and add the two available subnets.
- For Security groups, choose the default one.
- Click Save in the top right corner.
- 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, click Save in the top right corner.
- Create Test Event for Lambda
- 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.
- Keep the Hello World template selected, give your test event a name, and replace the test event body with
- Click Create.
- 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.