Skip to content

timescale/aws-lambda-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

timescale/aws-lambda-example

This repository serves as an example of an AWS Lambda time-series application that uses TimescaleDB on Timescale Cloud as a database!

Watch our video about this repository on YouTube!

YouTube Thumbnail

The application consists of two Lambda functions behind an API Gateway and a TimescaleDB instance on Timescale Cloud.

diagram of the architecture

PostSensorData

This function takes in an API Gateway event with a body containing the location and temperature of the sensor reading. The function parses those parameters from the body and inserts them into a TimescaleDB database.

Event body:

{ "location": "bedroom", "temperature": 74 }

Response:

[ { "time": 1675997918, "location": "bedroom", "temperature": 65.0 }, ... { "time": 1675997910, "location": "bedroom", "temperature": 67.0 } ]

GetSensorData

This function takes no inputs but returns the latest 5 sensor readings ordered by time in an array.

Template

In order to use this AWS SAM project, you need to modify the following records in the template.yaml file:

  • Globals.Function.Environment.Variables.CONN_STRING
  • Globals.Function.Environment.VpcConfig.SecurityGroupIds
  • Globals.Function.Environment.VpcConfig.SubnetIds

Test and Deploy

To test the functions locally we need to build the Docker container for each function. You can do this by executing the following command in the root of this project:

sam build

Afterwards we can test each function individually using the provided events in ./events.

# test the PostSensorData function sam local invoke "PostSensorDataFunction" -e events/post.json # test the GetSensorData function sam local invoke "GetSensorDataFunction" -e events/get.json

To deploy the function and it's required resources for the first time, execute:

sam deploy --guided 

If you've made changes to the code and want to deploy them to the cloud, execute:

sam build sam deploy 

About

A sample serverless AWS Lambda time-series application.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published