Serverless computing is a powerful way to build cloud-native applications without managing infrastructure. And when you’re working with IoT devices—streaming temperature, location, or telemetry data—AWS Lambda and TimescaleDB make a natural pairing for IoT.

In this tutorial, you’ll learn how to:

  • Set up Timescale Cloud for time-series data

  • Connect an AWS Lambda function to TimescaleDB

  • Insert IoT sensor data from Lambda into your PostgreSQL database

  • Apply best practices for security, monitoring, and scaling

Whether you're building a proof-of-concept or a production-grade pipeline, this guide will help you get started quickly with a modern, serverless data architecture.


What Is AWS Lambda?

AWS Lambda is a serverless platform that runs code in response to events. You don’t manage servers or provision instances—just deploy a function and configure when it should run (e.g., on an API call, S3 upload, or IoT message). It supports multiple languages (Python, Node.js, Go, Java) and scales automatically based on traffic.

Why Use TimescaleDB With AWS Lambda for IoT Workloads

TimescaleDB extends PostgreSQL with features designed for time-series, event, and analytics data. You can run it on your own infrastructure or use Timescale Cloud if you’d rather not manage things like backups, scaling, or availability yourself. In this guide, you’ll see how to connect AWS Lambda to either setup so you can start capturing and analyzing IoT data with minimal overhead.

IoT data is inherently time-stamped, high-ingest, and append-only—exactly what TimescaleDB is optimized for. By combining it with AWS Lambda, you get:

  • Automatic ingestion from device events

  • SQL-powered analysis and reporting

  • A modern stack with minimal operational overhead


Setting Up Timescale Cloud for IoT Data Storage

Timescale Cloud, with TimescaleDB at its core, provides a fully managed, scalable solution for handling IoT data. Below are the steps to set it up.

1. Create a Timescale Cloud account

Start by creating an account at Timescale Cloud. The platform offers a 30-day free trial for new users.

2. Create a new service

After logging in, follow these steps to create a new service for your IoT project:

  • Select service type: Choose PostgreSQL with TimescaleDB.

  • Select compute size: For development purposes, the smallest option (0.5 CPU / 2 GiB Memory) is sufficient. You can scale up as needed.

  • Choose environment: Choose Development if you are still in the testing phase. For production systems, select Production.

  • Select region: For instance, US West (Oregon) if it’s closest to your user base.

3. Retrieve connection information

Image description

Once the service is created, you will receive connection credentials, including a connection string (starting with postgres://). Download the credentials file for future reference.

Example connection string:

postgres://tsdbadmin:p3ohiff5fq9cglmg@l6f30gq1h6.uclw19gco.tsdb.cloud.timescale.com:31949/tsdb?sslmode=require

4. Create a hypertable

TimescaleDB uses hypertables, which automatically partition your data, speeding up your queries. After your service is up, follow these steps:

Select Create hypertable to partition your IoT data based on time, improving query performance.

5. Integrating AWS Lambda with Timescale Cloud

Image description

Now that your Timescale Cloud service is ready, integrate it with your AWS Lambda function. Here’s an example of how to modify your existing Lambda function to connect to Timescale Cloud:

import os
import psycopg2

def lambda_handler(event, context):
    # Timescale Cloud connection details
    db_conn_string = os.environ['DB_CONN_STRING']

    # Establish connection to Timescale Cloud
    conn = psycopg2.connect(db_conn_string)
    cursor = conn.cursor()

    # Insert IoT data
    sensor_data = json.loads(event['body'])
    cursor.execute("INSERT INTO iot_data (device_id, temperature, humidity, timestamp) VALUES (%s, %s, %s, %s)",
                   (sensor_data['device_id'], sensor_data['temperature'], sensor_data['humidity'], sensor_data['timestamp']))

    conn.commit()
    cursor.close()
    conn.close()

    return {
        'statusCode': 200,
        'body': json.dumps({'message': 'Data inserted successfully'})
    }

This Lambda function connects to your Timescale Cloud instance using the provided connection string and inserts IoT data into a table.

Setting Up AWS Lambda to Access TimescaleDB

If you're just getting started with IoT data, you may opt to self-host TimescaleDB. To integrate AWS Lambda with TimescaleDB, follow these steps:

Create a Lambda function: Start by creating a Lambda function using the AWS Management Console. You can choose your preferred runtime environment, such as Python or Node.js.

Image description

  • Install necessary libraries: Depending on the programming language, you'll need to install the appropriate PostgreSQL client library. For example, if you're using Python, you can use the psycopg2 library to interact with TimescaleDB. If you're unsure whether to use psycopg2 or psycopg3, we benchmarked both so you can make an informed decision.

  • Use Lambda layers: Lambda layers allow you to package and share libraries and dependencies across multiple Lambda functions. This feature is particularly useful when your function relies on external dependencies. For instance, you can package the psycopg2 library into a Lambda layer and attach it to your function, simplifying dependency management.

Image description

  • Set environment variables: Use Lambda's environment variables to store sensitive information, such as database connection details. This practice not only enhances security but also makes it easier to update these values without modifying your code.

  • Connect to TimescaleDB: In your Lambda function, establish a connection to the TimescaleDB instance using the client library. Ensure that your database credentials and other connection parameters are securely stored and retrieved from the environment variables.

Example: Inserting IoT data into TimescaleDB

Here’s a simple example of a Lambda function written in Python that inserts IoT data into a TimescaleDB table:

import json
import os
import psycopg2

def lambda_handler(event, context):
    # Retrieve environment variables
    db_name = os.environ['DB_NAME']
    db_user = os.environ['DB_USER']
    db_host = os.environ['DB_HOST']
    db_port = os.environ['DB_PORT']
    db_pass = os.environ['DB_PASS']

    # Establish connection to TimescaleDB
    conn = psycopg2.connect(
        dbname=db_name,
        user=db_user,
        password=db_pass,
        host=db_host,
        port=db_port
    )

    cursor = conn.cursor()

    # Insert data into TimescaleDB
    sensor_data = json.loads(event['body'])
    cursor.execute("INSERT INTO iot_data (device_id, temperature, humidity, timestamp) VALUES (%s, %s, %s, %s)",
                   (sensor_data['device_id'], sensor_data['temperature'], sensor_data['humidity'], sensor_data['timestamp']))

    conn.commit()
    cursor.close()
    conn.close()

    return {
        'statusCode': 200,
        'body': json.dumps({'message': 'Data inserted successfully'})
    }

Best Practices and Tips

  • Use efficient data processing techniques: When handling large volumes of data, optimize your code to minimize memory usage and execution time. Consider batch processing and parallelism to improve performance.

  • Monitor and log: Utilize AWS CloudWatch for monitoring and logging. Set up alerts for key metrics, such as function execution duration and errors, to ensure timely responses to any issues.

Image description

  • Security considerations: Secure your Lambda function by using identity and access management (IAM) roles with the minimum required permissions. Encrypt and store sensitive data, such as database credentials.

What’s Next

Integrating AWS Lambda with Timescale Cloud provides a powerful and scalable solution for managing IoT data pipelines. By combining the strengths of serverless architecture and a powerful time-series database like TimescaleDB, developers can build efficient and resilient systems capable of handling vast amounts of data.

Whether you're developing an application for real-time analytics or long-term data storage, this integration offers a flexible and cost-effective approach.

  • If you want to take it to the next level and build a time-series (or IoT) application using Lambda functions in Python, check out this tutorial.

  • To learn how to build IoT pipelines for faster analytics, we recommend another integration tutorial with AWS, this time using IoT Core.

Haven't tried Timescale yet? Install TimescaleDB on your machine or simply skip all the installation steps and create a Timescale Cloud account to experience a reliable, production-ready, and cost-efficient PostgreSQL cloud platform. You can try it for free for 30 days.