Link to home
Start Free TrialLog in
Avatar of Manikandan Thiagarajan
Manikandan ThiagarajanFlag for India

asked on

how could i connect to lambda to rds

how could i connect to lambda to rds
Avatar of ste5an
ste5an
Flag of Germany image

If you mean by lambda AWS Lambda and by rds Remote Desktops Services, then the answer you cannot.

AWS Lambda is an event-driven, serverless computing platform. RDS is a user interface.

So, please rephrase your question, provide some context.
Hi,
check this official document by aws.
you need to create a  aws role that can access RDS instance via vpc through lamda.
https://docs.aws.amazon.com/lambda/latest/dg/services-rds-tutorial.html
Create an IAM Role which has access to RDS
Use that role in Lambda function to access RDS
There are actually 2 steps to do.
  1. Allow the Lambda to access RDS by attaching a proper policy to the lambda execution role. 
  2. Bundle your Lambda with the modules that support RDS access. 
Consider the following Lambda python code example:
import pymysql
import sys

db_username = "someuser"
db_password = "somepassword"
db_name = "mydatabase"
db_host="mydatabase.mydomain.co"
db_port = 3306

def main(event=None, context=None):
    try:
        conn = pymysql.connect(
            db_host, 
            user=db_name, 
            passwd=db_password, 
            db=db_name, 
            connect_timeout=25, 
            autocommit=True
         )
    except:
        print("ERROR: Unexpected error: Could not connect to MySql instance.")
        sys.exit()

    print("SUCCESS: Connection to RDS mysql instance succeeded")

if __name__ == '__main__':
    main()

Open in new window

The pymysql module is NOT part of the python runtime, so you have to either bundle it with your lambda function, or place it as a layer.

Bundling is easy. Start a shell/command line in the directory where your code is, and execute a command that will place all of the requirements in this directory. In python it will be
pip install pymysql -t .

Open in new window

then just zip your lambda with all the subdirectories and upload as a zip file.

Creating a layer is a bit more complex, read about it here.
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.