How to: Set Up Labelbox Webhooks with AWS Lambda

Using the power of AWS Lambda and Labelbox webhooks, you can implement a variety of cloud-automated workflows to track activity in your Labelbox organization. In this post, I’ll give a step-by-step guide to setting up a Lambda function and connecting it to Labelbox to receive webhooks. For more general information on webhooks, check out the Labelbox docs page.

First, we’ll need to go to our workspace settings in Labelbox to set up our webhook. To do so, click on the gear icon in the bottom left corner.

Then, we’ll select the Webhooks tab and click Set up webhook. I’ll create a global webhook for this guide, but the steps will be the same for a project-specific webhook. You can also create multiple webhooks to be processed by different Lambdas. We’ll leave the webhook creation fields empty for now and go to AWS to set up a Lambda Function for our webhook.

Inside the AWS main dashboard, navigate to the Lambda home page by typing “Lambda” in the top search bar. Once on the main Lambda home page, select Manage Functions. Then, on the top right corner, select Create Function

On this page, keep the default Author from scratch on the top of the page, give your Lambda function a name, and select the Python 3.12 runtime. In this tutorial, we will use Python for our function, but feel free to adjust the code inside the function to work with a different runtime. As a default, we will have AWS create a new role with basic Lambda permissions, but later, if you want your Lambda function to interact with other AWS resources, you likely need to create a different IAM role to provide Lambda with the correct permissions. On the same page, go to the advanced settings tab and select Enable function URL checkbox with the None option selected for Auth type. Use the defaults for your other settings. Once you have all your settings configured, press Create function at the bottom of the page.

Now that you have created your Lambda function, you need to obtain its public URL so Labelbox can invoke it. To do this, navigate to the Configuration tab inside your function, head to the Function URL section, copy the URL shown, and ensure the Auth type is set to None.

Back to our webhook settings inside Labelbox, mark the webhook version as V2, and put the URL we copied from our Lambda function inside the URL text box. Lambda uses the HTTPS protocol by default to deliver our events securely. Next, add a secret that will be used to verify that requests are coming from Labelbox and select the topics for which you want to receive webhooks. I’ll select all topics for this guide. Finally, click the Save button at the bottom of the screen.

Our webhook is now fully configured on the Labelbox side, but we need to add the code we intend our Lambda to run upon invocation. In this tutorial, we will use the code below inside our Lambda. If you intend to follow along with this tutorial, go ahead and copy the code block below.

import json
import hmac
import hashlib
import re
import pprint

secret = "<replace with secret>"

def lambda_handler(event, context):
    webhook = re.sub(r'^sha256=', '', event['headers']['x-hub-signature-256'])
    
    digest = hmac.new(
        key = bytes(secret, 'utf-8'),
        msg = event['body'].encode('utf-8'),
        digestmod = hashlib.sha256
    )
    
    if webhook != digest.hexdigest():
        return {
            'statusCode': 400,
            'body': 'Error: computed_signature does not match'
        }
    
    print("=========== New Webhook Delivery ============")
    print("Delivery ID: %s" % event["headers"]["x-labelbox-id"])
    print("Event: %s" % event["headers"]["x-labelbox-event"])
    print("=========== Payload =============")
    print(event["body"])
    return {
        'statusCode': 200,
        'body': 'Success'
    }

Back inside AWS Lambda, navigate to your Lambda function’s Code tab. Paste the above code into your code source. Adjust your secret inside the code to match the secret you used within your Labelbox webhook setting. Then select the Deploy button to deploy your function.

You now have successfully deployed a Lambda that can utilize Labelbox webhooks! :clap:

Currently, the logs resulting from the webhook go into CloudWatch logs. To review the logs, navigate to the Monitor tab inside your Lambda function and select View CloudWatch logs at the top of the page.

This is where your webhook events are hosted. From here, you can navigate to different events on different days and review your full Labelbox project history. If you want a live stream of events to your Lambda, press the Start tailing button at the top right of the page.

If everything is working correctly, you should get a full event stream while labeling inside Labelbox, like the example event stream below:

Once your Lambda function is set up, you can send your logs to other AWS services for further analysis and record-keeping. I’ll demonstrate additional examples in the future, expanding on this topic with more in-depth use cases for webhooks. Please post any questions you have in the comments below!

3 Likes