title | excerpt | products | keywords | |||||||
---|---|---|---|---|---|---|---|---|---|---|
Integrate AWS Lambda with Timescale Cloud |
With AWS Lambda, you can run code without provisioning or managing servers, and scale automatically. Integrate AWS Lambda with Timescale Cloud and inject data into your service |
|
|
import IntegrationPrereqs from "versionContent/_partials/_integration-prereqs.mdx";
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS) that allows you to run code without provisioning or managing servers, scaling automatically as needed.
This page shows you how to integrate AWS Lambda with $SERVICE_LONG to process and store time-series data efficiently.
- Set up an AWS Account.
- Install and configure AWS CLI.
- Install NodeJS v18.x or later.
Create a table in $SERVICE_LONG to store time-series data.
-
Connect to your $SERVICE_LONG
For $CLOUD_LONG, open an SQL editor in $CONSOLE. For self-hosted, use
psql
. -
Create a table to store sensor data
CREATE TABLE sensor_data ( time TIMESTAMPTZ NOT NULL, sensor_id TEXT NOT NULL, value DOUBLE PRECISION NOT NULL );
-
For better performance and easier real-time analytics, convert the table to a hypertable
Hypertables are PostgreSQL tables that automatically partition your data by time. You interact with hypertables in the same way as regular PostgreSQL tables, but with extra features that makes managing your time-series data much easier.
SELECT create_hypertable('sensor_data', 'time');
Write an AWS Lambda function in a Node.js project that processes and inserts time-series data into a $SERVICE_LONG.
-
Initialize a new Node.js project to hold your Lambda function
mkdir lambda-timescale && cd lambda-timescale npm init -y
-
Install the PostgreSQL client library in your project
npm install pg
-
Write a Lambda Function that inserts data into your $SERVICE_LONG
Create a file named
index.js
, then add the following code:const { Client } = require('pg'); exports.handler = async (event) => { const client = new Client({ host: process.env.TIMESCALE_HOST, port: process.env.TIMESCALE_PORT, user: process.env.TIMESCALE_USER, password: process.env.TIMESCALE_PASSWORD, database: process.env.TIMESCALE_DB, }); try { await client.connect(); // const query = ` INSERT INTO sensor_data (time, sensor_id, value) VALUES ($1, $2, $3); `; const data = JSON.parse(event.body); const values = [new Date(), data.sensor_id, data.value]; await client.query(query, values); return { statusCode: 200, body: JSON.stringify({ message: 'Data inserted successfully!' }), }; } catch (error) { console.error('Error inserting data:', error); return { statusCode: 500, body: JSON.stringify({ error: 'Failed to insert data.' }), }; } finally { await client.end(); } };
To create an AWS Lambda function that injects data into your $SERVICE_LONG:
-
Compress your code into a
.zip
zip -r lambda-timescale.zip .
-
Deploy to AWS Lambda
In the following example, replace
<IAM_ROLE_ARN>
with your AWS IAM credentials, then use AWS CLI to create a Lambda function for your project:aws lambda create-function \ --function-name TimescaleIntegration \ --runtime nodejs14.x \ --role <IAM_ROLE_ARN> \ --handler index.handler \ --zip-file fileb://lambda-timescale.zip
-
Set up environment variables
In the following example, use your connection details to add your $SERVICE_LONG connection settings to your Lambda function:
aws lambda update-function-configuration \ --function-name TimescaleIntegration \ --environment "Variables={TIMESCALE_HOST=<host>,TIMESCALE_PORT=<port>, \ TIMESCALE_USER=<Username>,TIMESCALE_PASSWORD=<Password>, \ TIMESCALE_DB=<Database name>}"
-
Test your AWS Lambda function
-
Invoke the Lambda function and send some data to your $SERVICE_LONG:
aws lambda invoke \ --function-name TimescaleIntegration \ --payload '{"body": "{\"sensor_id\": \"sensor-123\", \"value\": 42.5}"}' \ --cli-binary-format raw-in-base64-out \ response.json
-
Verify that the data is in your $SERVICE_SHORT.
Open an SQL editor and check the
sensor_data
table:SELECT * FROM sensor_data;
You see something like:
time sensor_id value 2025-02-10 10:58:45.134912+00 sensor-123 42.5
-
You can now seamlessly ingest time-series data from AWS Lambda into $CLOUD_LONG.