npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ttoss/lambda-query

v0.1.0

Published

Create a Lambda function that queries a PostgreSQL database.

Downloads

7

Readme

@ttoss/lambda-postgres-query

This package creates an AWS Lambda function that can query an RDS Postgres database inside a private subnet of a VPC.

The goal of this package is to provide a way to query a Postgres database from a Lambda function without exposing the database to the internet. If your project needs to query a Postgres database and access to the internet, you can follow two approaches:

  1. Create a Lambda function inside a VPC and use a NAT Gateway to access the internet. This approach is expensive because you need to pay for the NAT Gateway.

  2. Decompose your architecture into multiple Lambdas—some inside the VPC and some outside the VPC. The Lambda inside the VPC can query the database, and the Lambda outside the VPC can query the Lambda inside the VPC. On this approach, Lambdas outside the VPC invoke Lambdas inside the VPC using the AWS SDK to query the database. This approach is complex and requires more effort to maintain.

Check this StackOverflow question for more information: Why can't an AWS lambda function inside a public subnet in a VPC connect to the internet?

Installation

To install this package, you need to run the following command:

pnpm install @ttoss/lambda-postgres-query

Usage

CloudFormation

Create a src/cloudformation.ts file with the following content:

import { createLambdaQueryTemplate } from '@ttoss/lambda-postgres-query';

Create a src/handler.ts file with the following content:

export { handler } from '@ttoss/lambda-postgres-query';

Provide the following environment variables in the .env file:

DATABASE_NAME=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_HOST=
DATABASE_HOST_READ_ONLY=
DATABASE_PORT=
SECURITY_GROUP_IDS=
SUBNET_IDS=

Add the deploy script to the package.json file:

{
  "scripts": {
    "deploy": "carlin deploy"
  }
}

Deploy them using the following command:

pnpm deploy

It'll create the necessary resources to query the Postgres database and display the name of the Lambda function created.

Querying the database

To query the database from Lambdas outside the VPC, do the following:

import { query } from '@ttoss/lambda-postgres-query';
import type { Handler } from 'aws-lambda';

export const handler: Handler = async (event) => {
  const text = 'SELECT * FROM table_name';
  const result = await query({ text });
  return result.rows;
};

API

createLambdaQueryTemplate

This function creates a CloudFormation template to deploy the Lambda function that queries the Postgres database.

query

This function queries the Postgres database using the environment variables provided. It uses the pg package to connect to the database.

Parameters

It accepts all the QueryConfig object from the pg package with the following additional properties:

  • readOnly: A boolean that indicates if the query should be executed on the read-only database, in case you provided the DATABASE_HOST_READ_ONLY value. Default is true.
  • lambdaPostgresQueryFunction: The name of the Lambda function that queries the database. Default is the value of the LAMBDA_POSTGRES_QUERY_FUNCTION environment variable.