---
title: Integrate AWS Lambda with Tiger Cloud | Tiger Data Docs
description: Run serverless code to process and store your data without managing infrastructure
---

[AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 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 Tiger Cloud service to process and store time-series data efficiently.

## Prerequisites

To follow the procedure on this page you need to:

- Create a [target Tiger Cloud service](/docs/get-started/quickstart/create-service/index.md).

  This procedure also works for [self-hosted TimescaleDB](/docs/get-started/choose-your-path/install-timescaledb/index.md).

* Set up an [AWS Account](https://signin.aws.amazon.com/signup?request_type=register).
* Install and configure [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
* Install [NodeJS v18.x or later](https://nodejs.org/en/download).

## Prepare your service to ingest data from AWS Lambda

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

1. **Connect to your Tiger Cloud service**

   For Tiger Cloud, open an [SQL editor](/docs/build/data-management/run-queries-from-tiger-console/index.md) in [Tiger Console](https://console.cloud.tigerdata.com/dashboard/services). For self-hosted TimescaleDB, use [`psql`](/docs/integrate/query-administration/psql/index.md).

2. **Create a hypertable to store sensor data**

   [Hypertables](/docs/learn/hypertables/understand-hypertables/index.md) 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 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 hypertable using [CREATE TABLE … WITH …](/docs/reference/timescaledb/hypertables/create_table/index.md), the default partitioning column is automatically the first column with a timestamp data type. Also, TimescaleDB creates a [columnstore policy](/docs/reference/timescaledb/hypercore/add_columnstore_policy/index.md) that automatically converts your data to the columnstore, after an interval equal to the value of the [chunk\_interval](/docs/reference/timescaledb/hypertables/set_chunk_time_interval/index.md), defined through `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 columnstore conversion, hypertable chunks are compressed by up to 98%, and organized for efficient, large-scale queries.

   You can customize this policy later using [alter\_job](/docs/reference/timescaledb/jobs-automation/alter_job/index.md). However, to change `after` or `created_before`, the compression settings, or the hypertable the policy is acting on, you must [remove the columnstore policy](/docs/reference/timescaledb/hypercore/remove_columnstore_policy/index.md) and [add a new one](/docs/reference/timescaledb/hypercore/add_columnstore_policy/index.md).

   You can also manually [convert chunks](/docs/reference/timescaledb/hypercore/convert_to_columnstore/index.md) in a hypertable to the columnstore.

## Create the code to inject data into a service

Write an AWS Lambda function in a Node.js project that processes and inserts time-series data into a Tiger Cloud service.

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

   Terminal window

   ```
   mkdir lambda-timescale && cd lambda-timescale
   npm init -y
   ```

2. **Install the PostgreSQL client library in your project**

   Terminal window

   ```
   npm install pg
   ```

3. **Write a Lambda Function that inserts data into your Tiger 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();
       }


   };
   ```

## Deploy your Node project to AWS Lambda

To create an AWS Lambda function that injects data into your Tiger Cloud service:

1. **Compress your code into a `.zip`**

   Terminal window

   ```
   zip -r lambda-timescale.zip .
   ```

2. **Deploy to AWS Lambda**

   In the following example, replace `<IAM_ROLE_ARN>` with your [AWS IAM credentials](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-keys-admin-managed.html#admin-list-access-key), then use AWS CLI to create a Lambda function for your project:

   Terminal window

   ```
   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](/docs/integrate/find-connection-details/index.md) to add your Tiger Cloud service connection settings to your Lambda function:

   Terminal window

   ```
   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 Tiger Cloud service:

      Terminal window

      ```
      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](/docs/build/data-management/run-queries-from-tiger-console/index.md) 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 Tiger Cloud.
