I'm trying to connect to a msql database on AWS using the following lambda function
const dotenv = require('dotenv');
dotenv.config();
var mysql = require('mysql');
var pool = mysql.createPool({
host : process.env.DBHOST,
user : process.env.DBUSER,
password : process.env.DBPASSWORD,
database : process.env.DBNAME
});
exports.handler = (event, context, callback) => {
//prevent timeout from waiting event loop
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
// Use the connection
connection.query('select * from products', function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) callback(error);
else callback(null,results[0].emp_name);
});
});
};
When executed it gives the following error:
Response:
{
"errorMessage": "2020-12-05T16:51:55.273Z 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Task timed out after 3.00 seconds"
}
Request ID:
"281776f1-ee7f-4c8c-8862-a7371d1a8f37"
Function logs:
START RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Version: $LATEST
END RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37
REPORT RequestId: 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Duration: 3003.58 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 75 MB Init Duration: 202.41 ms
2020-12-05T16:51:55.273Z 281776f1-ee7f-4c8c-8862-a7371d1a8f37 Task timed out after 3.00 seconds
How do I fix this?
This seems like a communication issue.
Make sure that the lambda can reach the RDS. Either they are deployed on same VPC, or there is a path from the lambda to the RDS.
Make sure that the RDS has a security group with an inbound rule to allow connection from the lambda (protocol, port, ip range or security group)
Also, verify that the lambda has the appropriate privileges (i.e. AWS IAM Role) to support your desired action.
Related
I am trying to connect AWS RDS Mysql from Lamda function written in Node.js. Initially I was getting "timed out" error, then I made below configuration.
-The Lambda execution role has full access to VPC.
-The Lambda function and the RDS instances are now in the same VPC.
-The Lambda function and the RDS instances are in same subnets.
-The Lambda function and the RDS instances shares a security group.
-All inbound traffic permitted.
Now I says that the DB I am trying to connect is unknown.Giving below error:
Response:
{
"errorType": "Error",
"errorMessage": "ER_BAD_DB_ERROR: Unknown database 'empdb'",
"trace": [
"Error: ER_BAD_DB_ERROR: Unknown database 'empdb'",
I am copying the code snippet as well.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : process.env.RDS_HOSTNAME,
user : process.env.RDS_USERNAME,
password : process.env.RDS_PASSWORD,
port : process.env.RDS_PORT,
database : process.env.RDS_DATABASE
});
connection.connect();
exports.handler = (event, context, callback) => {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
const sql = "insert into MESSAGE values('Testing1');";
connection.query(sql, (err, res) => {
if (err) {
throw err
}
callback(null, '1 records inserted.');
})
};
I tried both Create and Insert statements. I am new to AWS. Please advise.
My web app uses Lambda using NodeJS and backend is RDS(MySQL). I'm using serverless-mysql to make db calls.
For some reason, the db call times out intermittently. I tried the following:
Enabled flow logs to see if there are any errors (but couldn't find any reject statuses).
Tried making the database publicly available and took lambda out of VPC (to see if it is an issue with VPC configuration). But still, it was failing intermittently. So VPC is out of the equation.
RDS is not having any unusual spikes and connection exhaustion as monitoring shows a peak of only up to 3 connections. Lambda is always kept warm. I tried increasing the time out to up to 25 seconds. Still no luck.
Below is the code I use:
export async function get(event, context, callback) {
if (await warmer(event)) return 'warmed';
context.callbackWaitsForEmptyEventLoop = false;
try {
const userId = getUserIdFromIdentityId(event);
const query = "select * from UserProfile where UserId = ?";
const result = await mysql.query(query, [userId]);
console.log(result);
console.log('getting user account');
mysql.quit();
return success({
profileSettings: result.length > 0 ? result[0] : null,
});
} catch(e) {
console.log(e);
return failure();
}
}
Success function basically returns a json object like below:
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify(body)
};
mysql is initialized as below:
export const mysql = AWSXray.captureMySQL(require('serverless-mysql')({
config: {
host: process.env.dbHost,
user: process.env.dbUsername,
password: process.env.dbPassword,
database: process.env.database,
}
}));
The only error I can see in Cloudwatch logs is:
Task timed out after 10.01 seconds.
I've created a simple mySQL database that I'm trying to access data from via an AWS Lambda function.
This is a version of the code that runs fine locally:
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool({
host : config.dbhost,
user : config.dbuser,
password : config.dbpassword,
database : config.dbname
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query("SELECT username FROM ClimbingDB.users WHERE email = 'testemail1'", function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) throw error;
console.log(results);
process.exit();
});
});
This is that code converted to work with AWS Lambda:
var mysql = require('mysql');
var config = require('./config.json');
var pool = mysql.createPool({
host : config.dbhost,
user : config.dbuser,
password : config.dbpassword,
database : config.dbname
});
exports.handler = (event, context, callback) => {
//prevent timeout from waiting event loop
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
if (err) return callback(err)
// Use the connection
connection.query("SELECT username FROM ClimbingDB.users WHERE email = 'testemail1'", function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) return callback(error);
else return callback(null,results);
});
});
};
Which times out with this error message:
{
"errorMessage": "2019-07-19T17:49:04.110Z 2f3e208c-62a6-4e90-b8ec-29398780a2a6 Task timed out after 3.00 seconds"
}
I'm not sure why it doesnt seem to be able to connect. I tried adding the function to a vpc and a security group that has access to RDB's, neither of which do anything. I'm not sure what I'm doing wrong here.
You will need:
The Amazon RDS instance in the same VPC as the AWS Lambda function
A security group on the Lambda function (Lambda-SG)
A security group on the RDS instance (DB-SG) that permits inbound connections on port 3306 from Lambda-SG
That is, DB-SG should specifically reference Lambda-SG (it will turn into a security group ID in the format sg-1234).
You might also want to increase the timeout of the Lambda function to give it a bit more time to run.
I am trying to learn how to connect MySQL using lambda functions in AWS. I have followed a couple of instructions online and basically ended up with this code:
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 1000,
connectTimeout : 60 * 60 * 1000,
acquireTimeout : 60 * 60 * 1000,
timeout : 60 * 60 * 1000,
host: "foo-bar-123.us-east-2.rds.amazonaws.com",
user: "root",
password: "pass123",
database: "sample_db",
});
exports.handler = (event, context, callback) => {
// prevent timeout from waiting event loop
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
if (err) throw err;
// Use the connection
connection.query('SELECT id FROM customer limit 10;', function (error, results, fields) {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) callback(error);
else callback(null,results);
});
});
};
This is working on my local but when I zip this code and uploaded it as a lambda function, this returns
Response:
{
"errorMessage": "2018-11-13T02:16:10.339Z 0562b432-e6ea-11e8-81ef-bd64aa1af0a4 Task timed out after 30.03 seconds"
}
It times out no matter how many seconds I set it to.
I have pretty much set everything at default since I am new to all of these but I have added AmazonRDSFullAccess to the role of lambda function.
Does anyone have any idea what may be wrong or missing in my setup?
Thanks.
After doing some trial and errors, I was able to make it work and what I was missing is that I did not allow All TCP in the inbound of my RDS Security group. After that, I set it as my lambda function to No VPC, and it was able to query properly.
This link: https://dzone.com/articles/querying-rds-mysql-db-with-nodejs-lambda-function and the stack overflow link posted in there (which is this: AWS Lambda RDS connection timeout) helped me a lot in figuring out what was wrong with my code/setup.
Here is the final code that I ended up using.
const mysql = require('mysql');
const pool = mysql.createPool({
host: "foo-bar-123.us-east-2.rds.amazonaws.com",
user: "root",
password: "pass123",
database: "sample_db"
});
exports.handler = (event, context, callback) => {
//prevent timeout from waiting event loop
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection((err, connection) => {
if(err) throw err;
// Use the connection
connection.query('SELECT id FROM customer limit 10;', (error, results, fields) => {
// And done with the connection.
connection.release();
// Handle error after the release.
if (error) callback(error);
else callback(null,results);
});
});
};
Thanks!
I am trying to process bounces sent from Amazon's Simple Email Service via their Simple Notification Service vi a Lambda on AWS.
I'm running the following script:
var aws = require('aws-sdk');
var mysql = require('mysql');
Processor = {};
Processor.initializeConnection = function() {
console.log('Connecting to database');
Processor.connection = mysql.createConnection({
host : 'MYHOST',
user : 'MYUSER',
password : 'PASSWORD',
database : 'DATABASE'
});
console.log('Connection configured');
Processor.connection.connect(function(err) {
console.log('****');
console.log(err);
if (err != null) {
console.log('Could not connect to database');
return false;
} else {
console.log('Successfully connected to database');
return true;
}
});
console.log('Should not get here');
};
exports.handler = function(event,context){
console.log('Received event:');
var message = event.Records[0].Sns.Message;
// Get the object from the event and show its content type
if(Processor.initializeConnection()) {
context.fail('Database connection failed');
return;
}
context.succeed(message);
};
I upload this script as index.js along with node_modules containing the node mysql module all as a zip file.
I get the following output from Amazon when this is run:
START RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d
2015-07-23T00:46:13.159Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Received event:
2015-07-23T00:46:13.160Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Connecting to database
2015-07-23T00:46:14.035Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Connection configured
2015-07-23T00:46:14.095Z 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Should not get here
END RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d
REPORT RequestId: 378b8a8c-30d4-11e5-9db4-9b9537e3f53d Duration: 937.51 ms Billed Duration: 1000 ms Memory Size: 128 MB Max Memory Used: 14 MB
None of the code inside the connect fallback is run. I'm expecting it to report a connection failure as I'm not using valid credentials.
If I run a version of the code locally under nodejs the connect callback does fire. It just doesn't fire under Lambda.
Due to the asynchronous nature of node.js, your code might be exiting as a result of context.succeed() before all of your functions are executed.
See:
Async AWS Lambda not executed if caller returns too early
Why is this HTTP request not working on AWS Lambda?