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

22 Comments



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 – https://youtu.be/Ng_zi11N4_c

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

🎉 SUPPORT BE A BETTER DEV 🎉
Become a Patron: https://www.patreon.com/beabetterdev

📚 MY RECOMMENDED READING LIST FOR SOFTWARE DEVELOPERS📚
Clean Code – https://amzn.to/37T7xdP
Clean Architecture – https://amzn.to/3sCEGCe
Head First Design Patterns – https://amzn.to/37WXAMy
Domain Driver Design – https://amzn.to/3aWSW2W
Code Complete – https://amzn.to/3ksQDrB
The Pragmatic Programmer – https://amzn.to/3uH4kaQ
Algorithms – https://amzn.to/3syvyP5
Working Effectively with Legacy Code – https://amzn.to/3kvMza7
Refactoring – https://amzn.to/3r6FQ8U

🎙 MY RECORDING EQUIPMENT 🎙
Shure SM58 Microphone – https://amzn.to/3r5Hrf9
Behringer UM2 Audio Interface – https://amzn.to/2MuEllM
XLR Cable – https://amzn.to/3uGyZFx
Acoustic Sound Absorbing Foam Panels – https://amzn.to/3ktIrY6
Desk Microphone Mount – https://amzn.to/3qXMVIO
Logitech C920s Webcam – https://amzn.to/303zGu9
Fujilm XS10 Camera – https://amzn.to/3uGa30E
Fujifilm XF 35mm F2 Lens – https://amzn.to/3rentPe
Neewer 2 Piece Studio Lights – https://amzn.to/3uyoa8p

💻 MY DESKTOP EQUIPMENT 💻
Dell 34 inch Ultrawide Monitor – https://amzn.to/2NJwph6
Autonomous ErgoChair 2 – https://bit.ly/2YzomEm
Autonomous SmartDesk 2 Standing Desk – https://bit.ly/2YzomEm
MX Master 3 Productivity Mouse – https://amzn.to/3aYwKVZ
Das Keyboard Prime 13 MX Brown Mechanical- https://amzn.to/3uH6VBF
Veikk A15 Drawing Tablet – https://amzn.to/3uBRWsN

📚 References:
Part 1 – How to connect to AWS RDS MySQL: https://youtu.be/Ng_zi11N4_c
Getting started with AWS: https://youtu.be/lTyqzyk86f8
Code: https://pastebin.com/X0Yq9t0S

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

🌎 Find me here:
Twitter – https://twitter.com/BeABetterDevv
Instagram – https://www.instagram.com/beabetterdevv/
Patreon – Donations help fund additional content – https://www.patreon.com/beabetterdev

#AWS
#Serverless
#RDS
#MySQL

source

22 Comments
    • 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
      TheDoud33
      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
      marcin2x4
      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 "handler.py", line 1, in <module>
      import pymysql
      File "/Users/zhangyiwan/code/python/pythonRdsQuery/pymysql/__init__.py", line 59, in <module>
      from . import connections # noqa: E402
      File "/Users/zhangyiwan/code/python/pythonRdsQuery/pymysql/connections.py", 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
      yTek
      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
      Georgia-PRO
      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
      tpol45
      May 13, 2022 07:11 am Reply

      This is great and really well explained. Thanks!

Leave us a comment