Create New Entries Automatically Using AWS Lambda and Webhooks

In this document, we will learn how to create an entry automatically in a content type, whenever an entry is created or updated in some other content types.

In our example, we will assume that there are three content types named “Books,” “Videos,” and “All”. We will set up a system such that whenever an entry is created or updated, the webhook will invoke a Lambda function that will create or update the corresponding entry, respectively, in the “All” content type.

Having such a system in place immensely enhances search queries operations. So instead of performing different search queries on “Books” and “Videos,” you can simply perform such queries in a single REST API request in “All” content type.

Note: The schema or the structure for the three content types (Books, Videos, and All) should be same. While creating the “All” content type, make sure to add an extra single line field and name it “Entry UID”. It will store the UID of the entries created in “Books” and “Videos” and the field should be “unique” to be set in the properties. 

Steps for Execution

Here are the steps to automate the creation of an entry in Contentstack:

  1. Create Content Types
  2. Generate Management Token
  3. Create and set up AWS Lambda function
  4. Set up Webhook in Contentstack
  5. Try out the setup

Prerequisites

  1. Create Content Types

    To proceed with this guide, you’ll need to create three content types: Books, Videos, and All. The schema for these content types is as shown in the images below:

    • For Books content type:

      books.png

    • For Videos content type:

      videos.png
    • For All content type:

      all content type.png

    Note: The schema or the structure for the three content types (Books, Videos, and All) should be the same. While creating the “All” content type, make sure to add an extra single line field and name it “Entry UID”. It will store the UID of the entries created in Books and Videos content types and this field should be set as “Unique.”

    Now let’s proceed to generate a management token. 

  2. Generate Management Token

    For the Lambda function to perform the create, update, and delete operations on the All content type, you’ll first need to generate a management token in your stack.

    After generating a management token, make a note of it as you’ll need it for setting up your Lambda function. 

  3. Create and Set up AWS Lambda Function

    To trigger an action on the “All” content type in Contentstack whenever you create/update/delete entries of Videos and Books content types, you need to create the AWS Lambda function as shown below:

    1. Login to your AWS Management Console, select Lambda from the Services list.
    2. On the Functions page, click on the Create function button.
    3. On the Create function page, select the Author from Scratch option to write your custom lambda function, provide a name for your function, and select any available version of Node.js as the Runtime language.
    4. Then, click the Create function button.
    5. AWS Lambda offers an inline code editor. Here, you will write the code for the task that we will perform. For this exercise, we have created the sample code. You can get that code by contacting our Support team.

      Note: You can either write your lambda code here. Another option is to write the lambda code on your machine and then load it in the code editor. The inline editor has the basic code written, you can replace it with your own code.


    6. After getting the code zip file, upload it by scrolling to the Code source section and selecting the .zip file option from the Upload from the dropdown menu.

      Note: Ensure the Handler is set as index.handler in the Runtime settings section.


    7. Staying on your lambda function’s page, go to the Configuration tab and click on Environment variables.
    8. Then, in the Environment variables section, click on Edit and then the Add environment variable button. Add the following environment variables to your lambda function:
      1. baseUrl : https://api.contentstack.io/ (if your region is European then the baseUrl will be https://eu-api.contentstack.com/)
      2. managementToken : <your_management_token>
    9. Click on Save.
    10. Staying in the Configuration tab, go to the Triggers section, and click the Add trigger button.
    11. On the Add trigger page, in the Trigger configuration section, select API Gateway from the dropdown menu.
    12. From the API dropdown menu, select the Create an API option, select API type as REST API, select Open from the Security dropdown menu.
    13. Finally, click on Add. Then, you’ll be redirected to the Triggers section on your Lambda page.
    14. Click on the Details link and note down the API endpoint URL as you’ll need it while creating a webhook in Contentstack.
      trigger.png

  4. Set up Webhook in Contentstack

    Now to trigger the event on any update on the entry, you will have to set up a Webhook in Contentstack. To set up a Webhook, log in to your Contentstack account and perform the following steps:

    1. Go to your stack, hover over the “Settings” gear icon, and click on Webhooks
    2. On the Webhooks page, click + New Webhook.
    3. Enter the name for your Webhook, for example, AWS-Webhook Listener.
    4. In the URL to notify field, enter the API endpoint URL generated in Step 3.
    5. Inside the Conditional View section, you need to add conditions for your Books and Videos content types as shown below:webhook-conditions.png
      This implies that if any entry inside the Books and Videos content types is created, updated, or deleted, a webhook will be triggered to invoke the AWS Lambda function.
    6. Select the Enable Webhook checkbox option and click on the Save button to save your webhook settings.
  5. Try out the Setup

    Now try creating or updating an entry in Books or Videos content types. You should see the corresponding changes in the “All” content type.

More Resources

We have various guides that document how you can integrate webhooks with AWS Lambda. Refer to our Webhook Integrations page for more details



Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

^