最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon rds - Accessing AWS RDS PostgreSQL database from AWS Lambda - Stack Overflow

programmeradmin0浏览0评论

I am trying to write a sample AWS Lambda Node.js code that accesses an RDS PostgreSQL database. It fails with the error of the database does not exists.

Please help me troubleshoot.

The Lambda and RDS are in the same VPC I have a security group for each. One for the Lambda and one for the RDS. The RDS’ security group has an inbound rule that is PostgreSQL type, protocol TCP, port 5432, and the source is the Lambda’s security group. Similarly, the Lambda’s security group outbound rule has a destination which is the RDS’ security group.

I have the following Lamba code (index.js)


const { Client } = require("pg");

exports.handler = async (event) => {
    const client = new Client({
        user: process.env.DB_USER,
        host: process.env.DB_HOST,
        database: process.env.DB_NAME,
        password: process.env.DB_PASSWORD,
        port: 5432,
    });

    try {
        await client.connect();
        const res = await client.query("SELECT NOW()");
        await client.end();

        return {
            statusCode: 200,
            body: JSON.stringify({ message: "Connection successful!", time: res.rows[0] }),
        };
    } catch (error) {
        return {
            statusCode: 500,
            body: JSON.stringify({ error: error.message }),
        };
    } 


The index.js is part of the zip that also contains the pg. I installed locally the pg, and created a zip that contains everything needed. Finally, I uploaded the zip.

I set the 4 environment variables used in the code. The DB_NAME is the same as the name of the database I created in RDS.

What is wrong? How can I avoid the error message and get a successful database access?

Thank you for you help

发布评论

评论列表(0)

  1. 暂无评论