cs-icon.svg

Set up AWS Webhook Listener to Test Contentstack Webhook URLs

Using Contentstack Webhooks, you can post data to AWS when an event occurs in your stack. AWS responds to the payload from Contentstack Webhook Dispatcher by returning the appropriate data.

In this guide, let's understand how to configure a webhook listener in AWS, that listens for incoming requests from Contentstack and validates the request.

Process Overview:

We will set up an AWS API Gateway URL using AWS CLI that listens for a payload from Contentstack, and validates it with a mock response that can be viewed in Contentstack's webhook settings.

Prerequisites:

  1. AWS CLI installed and configured
  2. Basic lambda function with nodejs v14
  3. Setup a Webhook in your Stack

Steps for Execution:

  1. Create an AWS Lambda Function
  2. Create AWS API Gateway using AWS CLI
  3. Try it Out
  1. Create an AWS Lambda Function

    Perform the following steps to set up your AWS Lambda function:

    1. Login to your AWS Management Console and select Lambda from the Service list.
    2. Click on the Create Function button and then the Author from Scratch option.
    3. Configure the lambda based on your requirements. Choose Node.js 14.x as your run-time language and click on the Create function button.
    4. AWS Lambda offers an inline code editor. You can write your lambda code here. For this example, copy and paste the code snippet given below:
      exports.handler = async (event) => {
          const response = {
              statusCode: 200,
              body: event.body,
          };
      return response;
      };
      
    5. Save/ Deploy the lambda function.

    Note: Make sure to note the name & the ARN of your lambda function, as it would be required to link with API Gateway URL in the next step.

    We now have set up our Lambda Function. Let's move ahead and create the API Gateway through which our Lambda Function will be invoked.

  2. Create AWS API Gateway using AWS CLI

    Let's create an API Gateway URL and link it to the lambda function using AWS CLI.

    Additional Resource: Learn how to set up lambda proxy integrations in API Gateway.

    Note: The steps below are for demonstration purposes only. In practice, add your own API Gateway ID, name, oath, parent-id, ARN, API gateway URL etc.

    Run the following commands in your terminal after installing and configuring AWS CLI.

    1. Command to create AWS API Gateway.
      Provide the name of the REST API and add a description.

      Tip: To help you smoothly tryout this guide, we have attached example responses to each command stated below.

      aws apigateway create-rest-api --name <<rest_api_name>> 
      --description <<your_description>
      
      image.png
    2. Add get resources using the Rest API id you got in the last step.
      aws apigateway get-resources --rest-api-id <<rest_api_id>>
      
      image.png
    3. Run the below command to create a resource using the parent resource ID you got in the last step.
      aws apigateway create-resource 
      --rest-api-id <<rest_api_id>> 
      --parent-id <<parent_id>> 
      --path-part <<resource_path>>
      
      image.png
    4. Add a method to your AWS resource
      aws apigateway put-method 
      --rest-api-id <<rest_api_id>> 
      --resource-id <<resource_id>> 
      --http-method "ANY" 
      --authorization-type "NONE"
      
      image.png
    5. Define the response using the following command
      aws apigateway put-method-response 
      --rest-api-id <<rest_api_id>> 
      --resource-id <<resource_id>> 
      --http-method POST 
      --status-code 200
      
      image.png
    6. Adding/ Configuring API Gateway URL with lambda

      Note: Add the --type AWS_PROXY flag to execute the code and capture event body. 

      aws apigateway put-integration --rest-api-id <<rest_api_id>> 
      --resource-id <<resource_id>> --http-method ANY 
      --type AWS_PROXY --integration-http-method ANY 
      --uri "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-2:4063420xxxxx:function:webhook-test/invocations"
      
      image.png
    7. Now add integration response to receive the output from lambda function.
      aws apigateway put-integration-response 
      --rest-api-id <<rest_api_id>> 
      --resource-id <<resource_id>> 
      --http-method ANY --status-code 200 
      --content-handling CONVERT_TO_TEXT
      
      image.png
    8. Add permission/ inform lambda function to allow API calls from API Gateway http methods:
      aws lambda add-permission 
      --function-name webhook-test 
      --statement-id apigateway 
      --action lambda:InvokeFunction 
      --principal apigateway.amazonaws.com 
      --source-arn "arn:aws:lambda:us-east-2:4063420xxxxx:a6tminxxxx/*/ANY/"
      
      image.png
    9. Command to create a deployment on dev stage:
      aws apigateway create-deployment --rest-api-id <<rest_api_id>> 
      --stage-name dev 
      
      image.png
    10. After deploying the REST API, navigate to the lambda function which you have created in above steps click on the trigger section and copy the URL as shown below:
  3. Try it Out

    Create a webhook in Contentstack to invoke the Lambda Function through the API that we created in the last step.
    Log in to your Contentstack account and follow the steps given below:

    1. Navigate to your stack, click on the “Settings” icon on the left navigation panel, and click on Webhooks.
    2. On the Webhook page, click on + New Webhook.
    3. On the Create Webhook page, fill up the Name field (for example, AWS Listener). In the URL to notify field, enter the API endpoint URL that you generated when you deployed your APIs, in the previous step.
    4. Scroll down to the Conditional View section for creating a trigger for the webhook.
    5. Select the Enable Webhook checkbox option and Save your Webhook settings.

    Your webhook is now ready to invoke the Lambda Function when an entry from any content type is published successfully.
    Finally based on your webhook condition perform appropriate trigger action and check the webhook logs example.

    You would see the Request Details(left side) & Response Details (right side) will have the same entry response based on the trigger made.

Was this article helpful?
^