4
import pymysql from botocore.vendored import requests def lambda_handler(event,context): conn=pymysql.connect (host ="rootrestdatabase.cd6kbmibgfod.ap-south-1.rds.amazonaws.com", user="****" , passwd="*****",db="restawsdatabase") 

i want to connect rds in lambda . but i gor error like

"errorMessage": "(2003, \"Can't connect to MySQL server on 'rootrestdatabase.cd6kbmibgfod.ap-south-1.rds.amazonaws.com' (timed out)\")", "errorType": "OperationalError", "stackTrace": [ [ 

i have tried it in local machine it works fine but when i deploy code in lamba it didnt worked. also both lambda function and rds are in same region. what shoud i have to do?

1
  • in addition i have set time out of lambda funtion to 5 min still got same error Commented Feb 20, 2019 at 11:40

3 Answers 3

15

To communicate with RDS instances, lambda functions have to be in the same VPC - a network timeout error is a great indicator of that. However, if your RDS instance is publicly accessible, make sure the security groups, which you attached to the function, allow traffic that you need.

Sign up to request clarification or add additional context in comments.

1 Comment

this should be selected as the answer.
0

When you are connecting Lambda to My SQL DB make sure to follow the below steps:

  1. The Execution Role which is assigned to Lambda must have Permission to EC2 Full access.
  2. Go to VPC Tab in Lambda and make sure you have VPC there. Edit and add Required VPC Connection with proper Subnets and Security Groups.

Once this is done wait for 2 minutes and start testing your Lambda. It should connect to your required DB in VPC

Comments

0

Adding to Vladyslav Usenko answer.

If your RDS Instance is in a private subnet and you're adding your Lambda function to the same subnet and security group.

That means now both Lambda and RDS can interact with each other without any issue.

But here you might required a VPC endpoint if you're using some other service from your lambda function.

Scenario

I am using boto3 library in my lambda function to access Glue client, you might use this library for any another service like S3.

In that situation your lambda function will timeout because your VPC (in which the lambda is present) don't have a connection between VPC and another AWS service.

So here I created an GLUE endpoint and after that my issue was fixed. In your case another service endpoint might required.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.