Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda can't connect to RDS instance, but I can locally?

Tags:

I am trying to connect to my RDS instance from a lambda. I wrote the lambda locally and tested locally, and everything worked peachy. I deploy to lambda, and suddenly it doesn't work. Below is the code I'm running, and if it helps, I'm invoking the lambda via a kinesis stream.

'use strict';  exports.handler = (event, context, handlerCallback) => {     console.log('Recieved request for kinesis events!');     console.log(event);     console.log(context);      const connectionDetails = {         host:     RDS_HOST,         port:     5432,         database: RDS_DATABASE,         user:     RDS_USER,         password: RDS_PASSWORD     };      const db = require('pg-promise')({promiseLib: require('bluebird')})(connectionDetails);      db             .tx(function () {                 console.log('Beginning query');                  return this.query("SELECT 'foobar'")                            .then(console.log)                            .catch(console.log)                            .finally(console.log);             })             .finally(() => handlerCallback()); }; 

Here is the logs from cloud watch if it helps:

START RequestId: *********-****-****-****-********* Version: $LATEST  2016-05-31T20:58:25.086Z    *********-****-****-****-*********  Recieved request for kinesis events!  2016-05-31T20:58:25.087Z    *********-****-****-****-*********  { Records:  [ { kinesis: [Object], eventSource: 'aws:kinesis', eventVersion: '1.0', eventID: 'shardId-000000000000:**********************************', eventName: 'aws:kinesis:record', invokeIdentityArn: 'arn:aws:iam::******************:role/lambda_kinesis_role', awsRegion: 'us-east-1', eventSourceARN: 'arn:aws:kinesis:us-east-1:****************:stream/route-registry' } ] }  2016-05-31T20:58:25.283Z    *********-****-****-****-*********  { callbackWaitsForEmptyEventLoop: [Getter/Setter], done: [Function], succeed: [Function], fail: [Function], logGroupName: '/aws/lambda/apiGatewayRouteRegistry-development', logStreamName: '2016/05/31/[$LATEST]******************', functionName: 'apiGatewayRouteRegistry-development', memoryLimitInMB: '128', functionVersion: '$LATEST', getRemainingTimeInMillis: [Function], invokeid: '*********-****-****-****-*********', awsRequestId: '*********-****-****-****-*********', invokedFunctionArn: 'arn:aws:lambda:us-east-1:*************:function:apiGatewayRouteRegistry-development' }  END RequestId: *********-****-****-****-*********  REPORT RequestId: *********-****-****-****-*********    Duration: 20003.70 ms   Billed Duration: 20000 ms Memory Size: 128 MB   Max Memory Used: 22 MB    2016-05-31T20:58:45.088Z *********-****-****-****-********* Task timed out after 20.00 seconds 
like image 850
LordZardeck Avatar asked May 31 '16 21:05

LordZardeck


People also ask

How do I allow Lambda to connect to RDS?

To connect a Lambda function to an RDS instance, the networking configurations on each must be set to allow the connection. There are different configuration settings for each of the following connection types: A Lambda function and RDS instance in the same VPC. A Lambda function and RDS instance in different VPCs.

Can't connect to AWS RDS?

Troubleshoot database level issuesBe sure that you're using the correct user name and password to access the instance from your DB client. Be sure that the user has the database permissions to connect to the DB instance. Check for any resource throttling in Amazon RDS, such as CPU or memory contention.

Can you use Lambda with RDS?

Lambda can work seamlessly with RDS instances, as long as you remember the specific requirements for this particular setup. Since RDS instances are running in your VPC and Lambda by default does not have access to those resources, you'll need to configure the VPC connection when creating a Lambda function.

Can't connect to MySQL server on AWS Lambda?

When you are connecting Lambda to My SQL DB make sure to follow the below steps: The Execution Role which is assigned to Lambda must have Permission to EC2 Full access. 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.


1 Answers

@MarkB @Michael-sqlbot were correct in the comments, it was a security group issue.

I finally got AWS support response to point out that the RDS security group was indeed private to a specific IP. This doesn't make sense as I never configured that, and I could access the database from my local machine and elastic beanstalk. I added 0.0.0.0/0 to the security group and now the lambda can connect. Thanks for your help guys!

like image 122
LordZardeck Avatar answered Oct 19 '22 12:10

LordZardeck