> Be assumption that almost restrictions are resolved from AWS support, your AWS Lambda function was ready to get tips and implementation.
> Add Layers to Lambda function
> To create custom layer for Node.js in Lambda function, you need to determine which dependencies was built and use in Lambda function.
const { Pool, Client } = require('pg')
Note: As we observe ('pg') was used in code implementation, we need to create folder as structure below.
+ nodejs
+ nodejs/node_modules
+ nodejs/package.json
+ nodejs/package-lock.json
Here is details of package.json
{
"name": "pg",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"pg": "^8.7.1"
}
}
Running command npm install to generate automatically package-lock.json and node_modules. If we are still lack of dependencies, then you can totally update in package.json and run the command again. After that, let move and update in Layers of Lambda dashboard.
Note: Use generated folder and wrap as zip format to upload.
> Try to make an connection to Amazon RDS
exports.handler = async (event, context) => {
var dbConfig = {
user: "flagtickpostgres",
password: "qOi828&^oNLI",
database: "powerbi",
host: "powerbi.bbioeyff8icc.us-west-2.rds.amazonaws.com",
port: '5432'
};
const pool = new pg.Pool(dbConfig);
let client = await pool.connect();
let queryExample = "SELECT * FROM \"users\"";
let res = await new Promise((resolve) => {
client.query(queryExample, (err, res) => {
if (err !== null) {
resolve(err);
} else {
resolve(res);
}
client.end();
});
});
const response = {
statusCode: 200,
body: JSON.stringify(res),
};
return response;
};
> You can refer link here to get more instructions about pg.