> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate AWS Lambda with Tiger Cloud

> Run serverless code to process and store your data without managing infrastructure

export const SERVICE_SHORT = 'service';

export const PG = 'Postgres';

export const CONSOLE = 'Tiger Console';

export const CLOUD_LONG = 'Tiger Cloud';

export const SERVICE_LONG = 'Tiger Cloud service';

export const SELF_LONG = 'self-hosted TimescaleDB';

export const CHUNK = 'chunk';

export const HYPERTABLE = 'hypertable';

export const COLUMNSTORE = 'columnstore';

export const TIMESCALE_DB = 'TimescaleDB';

[AWS Lambda][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.

## Prerequisites

To follow the steps on this page:

* Create a target [{SERVICE_LONG}][create-service] with Real-time analytics enabled.<p />

  You need [your connection details][connection-info]. This procedure also
  works for [{SELF_LONG}][enable-timescaledb].

[create-service]: /deploy-and-operate/tiger-cloud/get-started/create-services

[enable-timescaledb]: /deploy-and-operate/self-hosted/install-and-update/install-self-hosted

[connection-info]: /integrations/find-connection-details

* Set up an [AWS Account][aws-sign-up].
* Install and configure [AWS CLI][install-aws-cli].
* Install [NodeJS v18.x or later][install-nodejs].

## Prepare your service to ingest data from AWS Lambda

Create a table in {SERVICE_LONG} to store time-series data.

1. **Connect to your {SERVICE_LONG}**

   For {CLOUD_LONG}, open an [SQL editor][in-console-editors] in [{CONSOLE}][services-portal]. For {SELF_LONG}, use [`psql`][psql].

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

   [Hypertables][hypertables-section] are {PG} tables that automatically partition your data by time. You interact
   with hypertables in the same way as regular {PG} tables, but with extra features that make managing your
   time-series data much easier.

   ```sql theme={"dark"}
   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 ...][hypertable-create-table], the default partitioning
   column is automatically the first column with a timestamp data type. Also, {TIMESCALE_DB} creates a
   [columnstore policy][add_columnstore_policy] that automatically converts your data to the {COLUMNSTORE}, after an
   interval equal to the value of the [chunk\_interval][create_table_arguments], 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
   {COLUMNSTORE} conversion, {HYPERTABLE} {CHUNK}s are compressed by up to 98%, and organized for efficient, large-scale queries.

   You can customize this policy later using [alter\_job][alter_job_samples]. However, to change `after` or
   `created_before`, the compression settings, or the {HYPERTABLE} the policy is acting on, you must
   [remove the columnstore policy][remove_columnstore_policy] and [add a new one][add_columnstore_policy].

   You can also manually [convert {CHUNK}s][convert_to_columnstore] in a {HYPERTABLE} to the {COLUMNSTORE}.

   [add_columnstore_policy]: /api-reference/timescaledb/hypercore/add_columnstore_policy

   [alter_job_samples]: /api-reference/timescaledb/jobs-automation/alter_job#samples

   [convert_to_columnstore]: /api-reference/timescaledb/hypercore/convert_to_columnstore

   [create_table_arguments]: /api-reference/timescaledb/hypertables/create_table#arguments

   [hypertable-create-table]: /api-reference/timescaledb/hypertables/create_table

   [remove_columnstore_policy]: /api-reference/timescaledb/hypercore/remove_columnstore_policy

## 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 {SERVICE_LONG}.

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

   ```shell theme={"dark"}
   mkdir lambda-timescale && cd lambda-timescale
   npm init -y
   ```

2. **Install the {PG} client library in your project**

   ```shell theme={"dark"}
   npm install pg
   ```

3. **Write a Lambda Function that inserts data into your {SERVICE_LONG}**

   Create a file named `index.js`, then add the following code:

   ```javascript theme={"dark"}
   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 {SERVICE_LONG}:

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

   ```shell theme={"dark"}
   zip -r lambda-timescale.zip .
   ```

2. **Deploy to AWS Lambda**

   In the following example, replace `<IAM_ROLE_ARN>` with your [AWS IAM credentials][aws-iam-role], then use
   AWS CLI to create a Lambda function for your project:

   ```shell theme={"dark"}
   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][connection-info] to add your {SERVICE_LONG} connection settings to your Lambda function:

   ```shell theme={"dark"}
   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 {SERVICE_LONG}:

      ```shell theme={"dark"}
      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_SHORT}.

      Open an [SQL editor][in-console-editors] and check the `sensor_data` table:

      ```sql theme={"dark"}
      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}.

[aws-iam-role]: https://docs.aws.amazon.com/IAM/latest/UserGuide/access-keys-admin-managed.html#admin-list-access-key

[aws-lambda]: https://docs.aws.amazon.com/lambda/latest/dg/welcome.html

[aws-sign-up]: https://signin.aws.amazon.com/signup?request_type=register

[connection-info]: /integrations/find-connection-details

[hypertables-section]: /manage-data/capabilities/hypertables/understand-hypertables

[in-console-editors]: /deploy-and-operate/tiger-cloud/get-started/run-queries-from-console

[install-aws-cli]: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

[install-nodejs]: https://nodejs.org/en/download

[psql]: /integrations/query-administration/psql

[services-portal]: https://console.cloud.timescale.com/dashboard/services
