AWS Lambda - Amazon Timestream

AWS Lambda

You can create Lambda functions that interact with Timestream. For example, you can create a Lambda function that runs at regular intervals to execute a query on Timestream and send an SNS notification based on the query results satisfying one or more criteria. To learn more about Lambda, see the AWS Lambda documentation.

Warning

The AWS Lambda SDKs currently do not support Amazon Timestream. You can follow the steps outlined below as a work around until the AWS Lambda SDKs add support for Amazon Timestream.

Build AWS Lambda functions using Amazon Timestream with Python

To build AWS Lambda functions using Amazon Timestream with Python, follow the steps below:

  1. Create an IAM role for Lambda to assume that will grant the required permissions to access the Timestream Service, as outlined in Create an IAM User with Timestream access.

  2. Edit the trust relationship of the IAM role to add Lambda service. You can use the commands below to update an existing role so that AWS Lambda can assume it:

    1. Create the trust policy document:

      cat > Lambda-Role-Trust-Policy.json << EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" ] }, "Action": "sts:AssumeRole" } ] } EOF
    2. Update the role from previous step with the trust document

      aws iam update-assume-role-policy --role-name <name_of_the_role_from_step_1> --policy-document file://Lambda-Role-Trust-Policy.json
  3. Because the version of boto3 that Lambda uses does not yet include Timestream, you need to add it to the package that you will deploy to Lambda. The steps to do this are described below:

    1. (optional) Create a virtual environemnt:

      python3 -m venv venv . venv/bin/activate
    2. If your version of boto3 is older than 1.15.9, reinstall it with the following commands:

      pip3 uninstall boto3 pip3 install boto3
    3. Create a file named lambda_function.py, and copy and paste the code below:

      import json import os import boto3 from botocore.config import Config def lambda_handler(event, context): session = boto3.Session() write_client = session.client('timestream-write', config=Config(read_timeout=20, max_pool_connections=5000, retries={'max_attempts': 10})) result = write_client.list_databases(MaxResults=5) databases = result['Databases'] for database in databases: print(database['DatabaseName']) return { 'statusCode': 200, 'body': json.dumps('Hello Timestream from Lambda!') }
    4. Run the following commands in the same directory as the one in which you created lambda_function.py:

      # Install boto3 locally pip3 install --target ./package boto3 # Create zip with contents of the installed packages cd package zip -r9 ${OLDPWD}/function.zip . # Add lambda_function.py to the package cd $OLDPWD zip -g function.zip lambda_function.py # Create a function in AWS Lambda function aws lambda create-function --function-name TimestreamExample --runtime python3.8 --role <arn_of_the_role_from_step_1> --handler lambda_function.lambda_handler --zip-file fileb://function.zip

Build AWS Lambda functions using Amazon Timestream with JavaScript

To build AWS Lambda functions using Amazon Timestream with JavaScript, follow the instructions outlined here.

Build AWS Lambda functions using Amazon Timestream with Go

To build AWS Lambda functions using Amazon Timestream with Go, follow the instructions outlined here.

Build AWS Lambda functions using Amazon Timestream with C#

To build AWS Lambda functions using Amazon Timestream with C#, follow the instructions outlined here.