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 Timescale Cloud service to process and store time-series data efficiently.

Before integrating:

Create a table in Timescale Cloud service to store time-series data.

  1. Connect to your Timescale Cloud service

    For Timescale Cloud, open an SQL editor in Timescale Console. For self-hosted, use psql.

  2. 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
    );
  3. 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 Timescale Cloud service.

  1. Initialize a new Node.js project to hold your Lambda function

    mkdir lambda-timescale && cd lambda-timescale
    npm init -y
  2. Install the PostgreSQL client library in your project

    npm install pg
  3. Write a Lambda Function that inserts data into your Timescale Cloud service

    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 Timescale Cloud service:

  1. Compress your code into a .zip

    zip -r lambda-timescale.zip .
  2. 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
  3. Set up environment variables

    In the following example, use your connection details to add your Timescale Cloud service 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>}"
  4. Test your AWS Lambda function

    1. Invoke the Lambda function and send some data to your Timescale Cloud service:

      aws lambda invoke \
      --function-name TimescaleIntegration \
      --payload '{"body": "{\"sensor_id\": \"sensor-123\", \"value\": 42.5}"}' \
      --cli-binary-format raw-in-base64-out \
      response.json
    2. Verify that the data is in your service.

      Open an SQL editor and check the sensor_data table:

      SELECT * FROM sensor_data;

      You see something like:

      timesensor_idvalue
      2025-02-10 10:58:45.134912+00sensor-12342.5

You can now seamlessly ingest time-series data from AWS Lambda into Timescale Cloud.

Keywords

Found an issue on this page?Report an issue or Edit this page in GitHub.