Manikandan Thiagarajan
asked on
how could i connect to lambda to rds
how could i connect to lambda to rds
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
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
Use that role in Lambda function to access RDS
There are actually 2 steps to do.
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
Creating a layer is a bit more complex, read about it here.
https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
- Allow the Lambda to access RDS by attaching a proper policy to the lambda execution role.
- Bundle your Lambda with the modules that support RDS access.
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()
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 .
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 TRIALMembers 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.
AWS Lambda is an event-driven, serverless computing platform. RDS is a user interface.
So, please rephrase your question, provide some context.