How to Query RDS MySQL From AWS Lambda in Python | Step by Step Tutorial


Trying to connect to RDS from a Lambda function but having Trouble? This is the video for you.

Part 1 – Create a RDS MySQL Database –

02:11 Installing Pymysql
03:00 Writing our Lambda Handler Function with Python
07:45 Preparing your handler and dependencies for Lambda
08:26 Creating our Lambda Function
10:43 Testing our Function

Become a Patron:

Clean Code –
Clean Architecture –
Head First Design Patterns –
Domain Driver Design –
Code Complete –
The Pragmatic Programmer –
Algorithms –
Working Effectively with Legacy Code –
Refactoring –

Shure SM58 Microphone –
Behringer UM2 Audio Interface –
XLR Cable –
Acoustic Sound Absorbing Foam Panels –
Desk Microphone Mount –
Logitech C920s Webcam –
Fujilm XS10 Camera –
Fujifilm XF 35mm F2 Lens –
Neewer 2 Piece Studio Lights –

Dell 34 inch Ultrawide Monitor –
Autonomous ErgoChair 2 –
Autonomous SmartDesk 2 Standing Desk –
MX Master 3 Productivity Mouse –
Das Keyboard Prime 13 MX Brown Mechanical-
Veikk A15 Drawing Tablet –

📚 References:
Part 1 – How to connect to AWS RDS MySQL:
Getting started with AWS:

☁Topics covered include:
RDS MySQL Connection
Lambda RDS IAM Permissions
Pymysql for Querying RDS MySQL

🌎 Find me here:
Twitter –
Instagram –
Patreon – Donations help fund additional content –



    • blank
      jumboliah13 Kipfer
      May 13, 2022 07:11 am Reply

      Oh, I've been searching for this one. THANKS!!

    • blank
      manikanta alapati
      May 13, 2022 07:11 am Reply

      "errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",

      "errorType": "Runtime.ImportModuleError",

      "requestId": "a0df413f-957b-42ca-a37a-ef4788c347b1",

      "stackTrace": []
      I am getting this error , can anyone tell me what to do?

    • blank
      Venkatesh Polisetty
      May 13, 2022 07:11 am Reply

      It's fantastic that you have explained it very well. I want to know that is it possible to create a table in aws RDS Instance itself without configuring with local mysql db?

    • blank
      May 13, 2022 07:11 am Reply

      Thanks a lot for this video 🙂
      Using SAM, I deployed a lambda function, and it's supposed to connect to a postgre DB (it is working locally).
      Next step, I created an Aurora postgre serverless RDS (not using dataAPI).
      VPC, subnets and security group were created during RDS creation.
      And Lambda was deployed on same VPC and security group. IAM role seems ok.
      But I'm getting a timeout error when trying to access the DB (through the API gateway).
      Is there some network routing or anything else to set so that my lambda can connect the RDS ?

      Thanks a lot (again)

    • blank
      Max Moor
      May 13, 2022 07:11 am Reply

      Hi, excellent video, is it possible that you make a video to connect to SQL Server RDS from Lambda function? I will buy you donuts whenever we meet 😃thanks

    • blank
      May 13, 2022 07:11 am Reply

      I'm getting error: `No module named pymssql._pymssql`…

    • blank
      Evan Erickson
      May 13, 2022 07:11 am Reply

      Any idea what this means?

      Traceback (most recent call last):
      File "", line 1, in <module>
      import pymysql
      File "/Users/zhangyiwan/code/python/pythonRdsQuery/pymysql/", line 59, in <module>
      from . import connections # noqa: E402
      File "/Users/zhangyiwan/code/python/pythonRdsQuery/pymysql/", line 167
      SyntaxError: invalid syntax

    • blank
      mustafa aydın
      May 13, 2022 07:11 am Reply

      Thanks for the great tutorial. I hava a question. What if we have a code including many libraries (many imports) and also some compiled functions in it via numba,cpython etc? I am asking this since in every api trigger, the imports and compilations will be also recalled which creates unnecessary overheads. How can it be overcome?

    • blank
      Ghanshyam Khatri
      May 13, 2022 07:11 am Reply

      great video !!

    • blank
      May 13, 2022 07:11 am Reply

      Helpful. Thanks.

    • blank
      Satya K
      May 13, 2022 07:11 am Reply

      So each lambda will have its own db connection object ? Is it there any benefit with common util function that can provide connection object to these lambda?

    • blank
      Wai Yan Leung
      May 13, 2022 07:11 am Reply

      How should I modify your lambda function to connect with API gateway?

    • blank
      High Volume
      May 13, 2022 07:11 am Reply

      If anyone can help: I am trying to pass the queryStringParameters to pymysql query to get the desired result for user selection
      param = event['queryStringParameters']['param']
      query = "select from xxxx where param = %s",(param,)
      but the value i am passing is turning to be null, any suggestion or solution approach

    • blank
      LM SM
      May 13, 2022 07:11 am Reply

      thanks a lot for your videos !

    • blank
      Md Nababuddin
      May 13, 2022 07:11 am Reply

      Hi Can you explain how to connect Private RDS using Lambda?

    • blank
      Alex Ma
      May 13, 2022 07:11 am Reply

      may I know is it possible to create a "GET" api to query MySQL through lambda function?

    • blank
      Abhay Nayak
      May 13, 2022 07:11 am Reply

      Hey this is great place to begin with AWS db and lambda, Could you also make a similar play through video on using database proxy, environment variables and other best practices for production? Would love to understand the best practices you follow in prod.

    • blank
      Ruth Kiruki
      May 13, 2022 07:11 am Reply

      Jus t a quick question: Do you need to attach the lambda and RDS to the same VPC for this to work?

    • blank
      Anand Kumar
      May 13, 2022 07:11 am Reply

      22,020 views•Apr 20, 2020
      nice number

    • blank
      May 13, 2022 07:11 am Reply

      hello , thank you for the video , how do you do if you want to setup an api and a json formatted return ?

    • blank
      Akash Narvekar
      May 13, 2022 07:11 am Reply

      This is an awesome Tutorial .. Short and Simple. Very Nicely explained.. Keep making these kind of videos .. Its gonna help lots of People to learn AWS without going through the lengthy tutorials we see these days.. Liked and Subscribed .. 🙂

    • blank
      May 13, 2022 07:11 am Reply

      This is great and really well explained. Thanks!

Leave us a comment