Connect from AWS Lambda
Learn how to set up a Neon database and connect from an AWS Lambda function
AWS Lambda is a serverless, event-driven compute service that allows you to run code without provisioning or managing servers. It is a convenient and cost-effective solution for running various types of workloads, including those that require a database.
This guide describes how to set up a Neon database and connect to it from an AWS Lambda function using Node.js as the runtime environment. It covers:
- Creating a Lambda function using the Serverless Framework, which is a serverless application lifecycle management framework.
- Connecting your Lambda function to a Neon database.
- Deploying the Lambda function to AWS.
Prerequisites
- A Neon account. If you do not have one, see Sign up for instructions.
- An AWS account. You can create a free AWS account at AWS Free Tier. An IAM User and Access Key are required to programmatically interact with your AWS account. You must provide these credentials when deploying the Serverless Framework project.
- A Service Framework account. You can sign up at Serverless Framework.
Create a Neon project
If you do not have one already, create a Neon project:
- Navigate to the Projects page in the Neon Console.
- Click New Project.
- Specify your project settings and click Create Project.
Create a table in Neon
To create a table, navigate to the SQL Editor in the Neon Console:
In the SQL Editor, run the following queries to create a users
table and insert some data:
Create a Lambda function
Create the Lambda function using the Serverless Framework:
-
Install the Serverless Framework by running the following command:
-
Create a
my-lambda
project directory and navigate to it. -
Run the serverless command to create a serverless project.
Follow the prompts, as demonstrated below. You will be required to provide your AWS account credentials. The process creates an
aws-node-project
directory. -
Navigate to the
aws-node-project
directory created by the previous step and install thenode-postgres
package, which you will use to connect to the database.After installing the
node-postgres
package, the following dependency should be defined in yourpackage.json
file: -
In the
aws-node-project
directory, add ausers.js
file, and add the following code to it:The code in the
users.js
file exports thegetAllUsers
function, which retrieves all rows from theusers
table and returns them as aJSON
object in theHTTP
response body.This function uses the
pg
library to connect to the Neon database. It creates a newClient
instance and passes the database connection string, which is defined in theDATABASE_URL
environment variable. It then callsconnect()
to establish a connection to the database. Finally, it uses thequery()
method to execute aSELECT
statement that retrieves all rows from theusers
table.The query method returns a
Promise
that resolves to an object containing the rows retrieved by theSELECT
statement, which the function parses to retrieve therows
property. Finally, the function returns anHTTP
response with a status code of 200 and a body that contains aJSON
object with a singledata
property, which is set to the value of the rows variable. -
Add the
DATABASE_URL
environment variable and the function definition to theserverless.yml
file, which is located in youraws-node-project
directory.note
Environment variables can also be added to a
.env
file and loaded automatically with the help of the dotenv package. For more information, see Resolution of environment variables.You can copy the connection string from Connection Details widget the Neon Console. Add the
DATABASE_URL
underenvironment
, and addsslmode=require
to the end of the connection string to enable SSL. Thesslmode=require
option tells Postgres to use SSL encryption and verify the server's certificate. -
Deploy the serverless function using the following command:
The
serverless deploy
command generates an API endpoint using API Gateway. The output of the command appears similar to the following: -
Test the generated endpoint by running a cURL command. For example:
The response returns the following data:
Enabling CORS
If you make API calls to the Lambda function from your app, you will likely need to configure Cross-Origin Resource Sharing (CORS). Visit the AWS documentation for information about how to enable CORS in API Gateway.
You can run the following command to enable CORS to your local development environment:
You can find your api-id
on the API Gateway dashboard:
Conclusion
In this guide, you have learned how to set up a Postgres database using Neon and connect to it from an AWS Lambda function using Node.js as the runtime environment. You have also learned how to use Serverless Framework to create and deploy the Lambda function, and how to use the pg
library to perform a basic database read operations.
Last updated on