Skip to main content

AWS Lambda function - hide

In this guide we'll walkthrough setting up AWS Lambda function to receive data from monoZ:Link and trigger email upon successfully receiving data. Creating an SNS topic in the AWS SNS Console and subscribing to it. This integration allows for directly passing data from monoZ:Link to user`s Lambda function in their AWS environment thereby facilitating efficient data handling for critical applications.

Setup SNS service

  1. First step is to create a SNS topic. In the AWS Management Console, go to SNS (Simple Notification Service) and click "Create Topic."
  1. Choose the type as "Standard" and give your topic a name (e.g., LambdaEmailTopic).
    Click "Create Topic."
  1. After creating the topic, click "Create subscription”.
  1. Set the protocol as Email. Enter the email address you want to receive notifications and click "Create subscription."
  1. Check your email inbox and confirm the subscription by clicking the link in the confirmation email from AWS.

Create the Lambda Function

  1. In the AWS Management Console, go to Lambda.
  1. Click "Create Function."
  1. Choose "Author from scratch". Enter a name for the function (e.g., ProcessIncomingData) and select your runtime (e.g., Python 3.x). Create a new role with basic Lambda permissions.
  1. The following code written in Python receives an event (which can be a JSON payload), processes it, and publishes a message to the SNS topic.
    import json
    import boto3

    sns_client = boto3.client(‘sns’)

    def lambda_handler(event, context):
    # Extract only the body field from the event
    if "body" in event:
    message = event["body"]
    else:
    return {
    "statusCode": 400,
    "body": json.dumps("Invalid event format, 'body' not found.")
    }

    # Send the message to SNS
    sns_client.publish(
    TopicArn='arn:aws:sns:region:account-id:topic-name',
    Message=message
    )
    return {
    "statusCode": 200,
    "body": json.dumps("Message sent to SNS successfully.")
    }
  1. To setup environment variable, go to "Configuration > Environment variables" in the Lambda console and add the following variable: SNS_TOPIC_ARN: The ARN of the SNS topic you created (find it under SNS topic details).
  1. Next, to assign Lambda permissions, navigate to IAM Console from the AWS Management Console. Under Roles, find the execution role created for your Lambda function (its name will be similar to the Lambda function name).
  1. Click on the role, then click Add permissions > Attach policies.
  1. In the search bar, type SNS, check the box for AmazonSNSFullAccess
  1. Alternatively you can create a custom policy that allows sns:Publish to your topic ARN.

    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Effect": "Allow",
    "Action": "sns:Publish",
    "Resource": "<your-SNS-topic-ARN>"
    }
    ]
    }
  2. To test the function, open your ProcessIncomingData function in lambda console. Click "Test" and create a test event with sample data (e.g., a simple JSON payload). Click "Test" again to invoke the function. Or send it through the Device

  1. Before concluding, copy the custom endpoint for sending data to AWSLambda. Access AWS Lambda console and click your created Function (e.g., ProcessIncomingData) and copy the contents shown in [Function URL] under [Description]

Setup monoZ:Link

  1. Go to Protocol Configuration→Add Configuration
  1. Select Webhook from the dropdown list
  1. Add the configuration details and click “Save”

    Source ProtocolMQTT
    Configuration NameTest (Any suitable name)
    HostCustom end point to send data to lambda
  2. Go to Groups→Add Group

  3. Add the necessary details in the fields and click “Save”

    Group NameTest Group (Any Value)
    Available ConfigurationTest Config (the configuration created with AWS Lambda)
  4. Go to Devices→Click “Edit icon”. Enter the details in the fields and click “Update”

    Device Name12344
    GroupSelect the Lambda group for this device

Send data from the device

Send payload data from the registered device to monoZ:Link. monoZ:Link shall translate protocol and push the received data to AWS Lambda function which in turn invoke the email with the incoming data.