TigerData logo
TigerData logo
  • Product

    Product

    Tiger Cloud

    Robust elastic cloud platform for startups and enterprises

    TimescaleDB Enterprise

    Self-managed TimescaleDB for on-prem, edge and private cloud

    Open source

    TimescaleDB

    Time-series, real-time analytics and events on Postgres

    Search

    Vector and keyword search on Postgres

  • Industry

    Data Centers

    Energy & Utilities

    Oilfield Services

    Smart Manufacturing

    Crypto

  • Docs
  • Pricing
  • Developer Hub

    Changelog

    Benchmarks

    Blog

    Community

    Customer Stories

    Events

    Support

    Integrations

    Launch Hub

  • Company

    About

    TigerData logo

    Timescale

    Partners

    Security

    Careers

View on GitHub (opens in a new tab)

Contact usStart a free trial
Tiger Data

Products

  • TimescaleDB
  • Tiger Cloud
  • TimescaleDB Enterprise
  • Postgres Search Stack

Industry

  • Data Centers
  • Energy & Utilities
  • Oilfield Services
  • Smart Manufacturing
  • Crypto

Support

  • Cloud Status
  • Support
  • Security
  • Terms of Service
  • Code Of Conduct

Learn

  • Documentation
  • Blog
  • Tutorials
  • Changelog
  • Success Stories

Company

  • About
  • Contact Us
  • Careers
  • Newsroom
  • Brand
  • Events

Products

  • TimescaleDB
  • Tiger Cloud
  • TimescaleDB Enterprise
  • Postgres Search Stack

Industry

  • Data Centers
  • Energy & Utilities
  • Oilfield Services
  • Smart Manufacturing
  • Crypto

Support

  • Cloud Status
  • Support
  • Security
  • Terms of Service
  • Code Of Conduct

Learn

  • Documentation
  • Blog
  • Tutorials
  • Changelog
  • Success Stories

Company

  • About
  • Contact Us
  • Careers
  • Newsroom
  • Brand
  • Events
Privacy preferencesLegalPrivacySitemap
Strategic Partner with Inductive Automation — Ignition

2026 (c) Timescale, Inc., d/b/a Tiger Data.
All rights reserved.

Tiger Data

2026 (c) Timescale, Inc., d/b/a Tiger Data.
All rights reserved.

Privacy preferencesLegalPrivacySitemap

Using AWS Lambda with TimescaleDB for IoT Data Integration

J

By Juan Jose

October 18th, 2024

6 min

Share

J

By Juan Jose

October 18th, 2024

6 min

Share

Copy as HTML

Open in ChatGPT

Open in Claude

Open in v0

AWS Lambda

IoT

Tutorials

Table of contents

  1. 01 What Is AWS Lambda?
  2. 02 Why Use TimescaleDB With AWS Lambda?
  3. 03 Setting Up Timescale Cloud for IoT Data Storage
  4. 04 Best Practices and Tips
  5. 05 What’s Next
Get started for free
The Timescale and AWS logos: Using AWS Lambda with TimescaleDB for IoT Data Integration

Serverless architectures have gained significant popularity in the field of cloud computing due to their scalability, cost efficiency, and ease of management. One of the key players in this domain is AWS Lambda, which enables developers to run code without provisioning or managing servers.

In this article, we explore the integration of AWS Lambda with self-hosted TimescaleDB, a database built on PostgreSQL for time series, events, analytics, and vector data, and its mature cloud platform, Timescale Cloud, which offers additional benefits in terms of reliability and scalability.

These setups are particularly useful for capturing, processing, and storing data from IoT devices and sensors.

🤔
Wondering how to choose an IoT database?

What Is AWS Lambda?

AWS Lambda is a serverless computing service provided by Amazon Web Services. It allows developers to run code in response to events, such as HTTP requests or changes in data, without the need for provisioning servers.

Lambda supports various programming languages, including Python, Node.js, Java, and Go, making it a versatile choice for a wide range of applications. Lambda makes it very easy to call or automatically trigger function code (that can be written in Go, Node.js, Java, or Python) to execute on the platform.

You don’t have to worry about server maintenance or scaling—that is all handled by the Lambda service. You just write your code (to effectively do whatever you want), and you have AWS Lambda handle the execution of that code.

Why Use TimescaleDB With AWS Lambda?

TimescaleDB, an extension of PostgreSQL, is optimized for time-series data and other demanding workloads, making it an excellent choice for storing and querying large volumes of time-stamped data. This capability is particularly beneficial for IoT applications, where devices generate continuous streams of data that need to be processed and analyzed in real time.

The combination of AWS Lambda and TimescaleDB provides a robust and scalable solution for managing IoT data pipelines, enabling developers to focus on application logic rather than infrastructure management.

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

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

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:

  1. 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
Creating Lambda function
  1. 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.
  2. 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
Adding a Lambda layer
  1. 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.
  2. 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

  1. 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.
  2. 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
  1. 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.


About the author

J

By Juan Jose

// Related posts

Building IoT Pipelines for Faster Analytics With IoT Core
Building IoT Pipelines for Faster Analytics With IoT Core

AWS

IoT

Building IoT Pipelines for Faster Analytics With IoT Core

Build an endless stream of IoT pipelines for faster real-time dashboards and analytics by using the IoT Core message routing functionality and a single Lambda function.

By Mathis Van Eetvelde

August 9th, 2023

Saving Devs’ Time and Compute Power With Retention Policies: The Story of Crypto Trading Platform Pintu
Saving Devs’ Time and Compute Power With Retention Policies: The Story of Crypto Trading Platform Pintu

Dev Q&A

AWS Lambda

Saving Devs’ Time and Compute Power With Retention Policies: The Story of Crypto Trading Platform Pintu

Read how crypto trading platform Pintu accelerated their go-to-market motion and saved developers' time and compute power using retention policies and incrementally-updated materialized views.

By Christian Halim

June 27th, 2023

Do More With AWS in Timescale: An AWS Lambda Tutorial Using SAM CLI
Do More With AWS in Timescale: An AWS Lambda Tutorial Using SAM CLI

AWS

AWS Lambda

Do More With AWS in Timescale: An AWS Lambda Tutorial Using SAM CLI

Read this AWS Lambda tutorial and learn how to build and deploy a serverless time-series application on AWS Lambda and Timescale using SAM CLI.

By Mathis Van Eetvelde

April 5th, 2023

Stay updated with new
posts and releases.

Receive the latest technical articles and release notes in your inbox.