Skip to main content
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 to process and store time-series data efficiently.

Prerequisites

To follow the steps on this page:
  • Create a target with the Real-time analytics capability enabled.

    You need your connection details. This procedure also works for .

Prepare your Tiger Cloud service to ingest data from AWS Lambda

Create a table in to store time-series data.
  1. Connect to your For , open an SQL editor in . For , use psql.
  2. Create a to store sensor data s are tables that automatically partition your data by time. You interact with s in the same way as regular tables, but with extra features that make managing your time-series data much easier.
    CREATE TABLE sensor_data (
      time TIMESTAMPTZ NOT NULL,
      sensor_id TEXT NOT NULL,
      value DOUBLE PRECISION NOT NULL
    ) WITH (
      tsdb.hypertable
    );
    
    When you create a using CREATE TABLE … WITH …, the default partitioning column is automatically the first column with a timestamp data type. Also, creates a columnstore policy that automatically converts your data to the , after an interval equal to the value of the chunk_interval, defined through compress_after in the policy. This columnar format enables fast scanning and aggregation, optimizing performance for analytical workloads while also saving significant storage space. In the conversion, chunks are compressed by up to 98%, and organized for efficient, large-scale queries. You can customize this policy later using alter_job. However, to change after or created_before, the compression settings, or the the policy is acting on, you must remove the columnstore policy and add a new one. You can also manually convert chunks in a to the .

Create the code to inject data into a Tiger Cloud service

Write an AWS Lambda function in a Node.js project that processes and inserts time-series data into a .
  1. Initialize a new Node.js project to hold your Lambda function
    mkdir lambda-timescale && cd lambda-timescale
    npm init -y
    
  2. Install the client library in your project
    npm install pg
    
  3. Write a Lambda Function that inserts data into your 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();
        }
    
    };
    

Deploy your Node project to AWS Lambda

To create an AWS Lambda function that injects data into your :
  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 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 :
      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 . 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 .