Prerequisites

Create AWS Lambda function

Create a Lambda function with Python runtime and the following content. For more information, see the AWS documentation. The Lambda function acts as an intermediary to process data from AWS IoT and send it to Axiom.

import os  # Import the os module to access environment variables
import json  # Import the json module to handle JSON data
import requests  # Import the requests module to make HTTP requests

def lambda_handler(event, context):
    # Retrieve the dataset name from the environment variable
    dataset_name = os.environ['DATASET_NAME']
    
    # Construct the Axiom API URL using the dataset name
    axiom_api_url = f"https://api.axiom.co/v1/datasets/{dataset_name}/ingest"
    
    # Retrieve the Axiom API token from the environment variable
    api_token = os.environ['API_TOKEN']
    
    # Define the headers for the HTTP request to Axiom
    headers = {
        "Authorization": f"Bearer {api_token}",  # Set the Authorization header with the token
        "Content-Type": "application/json",  # Specify the content type as JSON
        "X-Axiom-Dataset": dataset_name  # Include the dataset name in the headers
    }
    
    # Create the payload for the HTTP request
    payload = {
        "tags": {"source": "aws-iot"},  # Add a tag to indicate the source of the data
        "events": [{"timestamp": event['timestamp'], "attributes": event}]  # Include the event data
    }
    
    # Send a POST request to the Axiom API with the headers and payload
    response = requests.post(axiom_api_url, headers=headers, data=json.dumps(payload))
    
    # Return the status code and a confirmation message
    return {
        'statusCode': response.status_code,  # Return the HTTP status code from the Axiom API response
        'body': json.dumps('Log sent to Axiom!')  # Return a confirmation message as JSON
    }

In the environment variables section of the Lambda function configuration, add the following environment variables:

  • DATASET_NAME is the name of the Axiom dataset where you want to send data.
  • API_TOKEN is the Axiom API token you have generated. For added security, store the API token in an environment variable.

This example uses Python for the Lambda function. To use another language, change the code above accordingly.

Create AWS IoT rule

Create an IoT rule with an SQL statement similar to the example below that matches the MQTT messages. For more information, see the AWS documentation.

SELECT * FROM 'iot/topic'

In Rule actions, select the action to send a message to a Lambda function, and then choose the Lambda function you created earlier.

Check logs in Axiom

Use the AWS IoT Console, AWS CLI, or an MQTT client to publish messages to the topic that matches your rule. For example, iot/topic.

In Axiom, go to the Datasets tab and select the dataset you specified in the Lambda function. You now see your logs from your IoT devices in Axiom.