Scheduling Repeatable Releases Using Management APIs and AWS Lambda

In this guide, we will look at the steps required to create an automated system for scheduling repeatable releases using AWS Lambda and Contentstack's Content Management APIs.

Once the system creates a release automatically, you can add items (entries and assets) to it and they will be deployed automatically on the scheduled time. This is a continuous process where the release gets created every day and if there are items added to it, they get deployed.

Process Overview

In this example, we will use two Lambda Functions: one for creating a release and another one for deploying the release. The “create” release lambda function will create a new release for every new day. For example, a release will be scheduled every midnight after 12:00 AM, a new day, and create a release for this specific day using the Releases API.

Note: The date can be changed as per your convenience, but ensure that it is after 12:00 AM or a new date 00:01 (24 hr format).

In the following image, you can see that release Thu May 14 2020 is the release name and created by using the Create Release Lambda Function every new day for entries and assets required to be deployed on that day, that is, “Thu May 14 2020.”

Release_example.png

The Deploy Release Lambda Function deploys the release created on that specific date. For example, if the release is scheduled for “Thu May 14 2020,” the Deploy Release lambda will look for a release created for that day. 

If it finds the release for that day, it fetches the UID of that release and looks for any entries or assets within that release. And if there are any entries or assets within that release, it deploys the release. If there are no entries or assets for deployment in the release, it does nothing as there is nothing to deploy.

The next day, the Create Release lambda function will be triggered again to create another release. And this continues making it repeatable. If the release with entries and assets is deployed, it locks the release (as shown in the above screenshot) and it cannot be accessed for adding new entries or assets (it will be disabled).

Note: These lambda functions can be “enabled” or “disabled” to stop lambda from creating and deploying releases.

Prerequisites

  • Contentstack account
  • AWS account and access to AWS Lambda
  • Working knowledge of AWS Lambda
  • AWS CloudWatch events access
  • Knowledge of CRON expression for scheduling Lambda Function in CloudWatch Events

Steps for Execution

  1. Download the code
  2. Set up the Create Release Lambda Function
  3. Add Trigger to the Create Release Lambda Function
  4. Set up the Deploy Release Lambda Function
  5. Add Trigger to the Deploy Release Lambda Function

Let's now move ahead with the steps and create the required system.

  1. Download the Code

    Go to our GitHub page and download the code for the Create Release and Deploy Release lambda functions.

    After downloading the code, fire up the terminal, go to the respective project directories, and install the required node modules using the npm install command.

  2. Set up the Create Release Lambda Function

    Perform the following steps to set up the Create Release Lambda function:

    1. Login to your AWS Management Console and select Lambda from the Services 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 12.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 or alternatively upload the code. For our example, we have created a sample code. 
    5. After installing the node modules in the createRelease project, upload createRelease.zip on Lambda by selecting the Upload a .zip file option from the Code entry type drop-down. Keep Handler as createRelease/index.handler and click on Save.

      This is how it will look when you upload the code in the editor:

      Create Release.png

    6. Once we have uploaded the code in the editor, let's now set up the Environment variables by adding the Stack API key, your Contentstack region, and Stack Management Token as follows:

      Create Release EV.png

    7. Once you have added these environment variables, click on Save.

    With these steps, we have set up our Lambda Function. Let's now move ahead with creating a trigger for this Lambda Function.

  3. Add Trigger to the Create Release Lambda Function

    We will add a trigger to our lambda function to invoke it at a particular time. We do this as follows:

    1. In the Designer section within the Lambda dashboard, click on + Add trigger:

      Create Release Trigger.png

    2. On the Add trigger page, select CloudWatch Events/EventBridge from the drop-down menu.
    3. From the Rule drop-down menu, you can select Create a new Rule or an existing rule (if you have created any rule before) as shown below:

      Create Rule.png

    4. Select the Schedule expression option from Rule type.
    5. Then, provide a “CRON” expression as shown in the following screenshot in order to trigger the lambda at a scheduled time.

      cronexpression.png

    6. Ensure to check the Enable trigger option and click on Add.

    Additional Resources: Refer to Cron-Generator and AWS-guide-on-using-cron for more information. Also, refer to the AWS guide on How-to-schedule-lambda-using-cloudwatch-events.

    For the Create Release Lambda Function, the scheduled cron expression that we just set will trigger on cron(00 01 * * ? *), that is, at 1:00 AM (which will be on a new day). You can adjust the scheduled cron expression but make sure it is on a new date, that is, after 12:00 AM.

    Note: You can use cron or rate expression to create self-trigger rules on an automated schedule in CloudWatch Events. All your created scheduled events use the UTC time zone with the minimum precision of 1 minute for the schedule.

    After you have set up the trigger, this is how it will look in the Lambda dashboard:

    Complete_Create_Release_LF.png

    Note: You can disable or enable events rules using the options as shown in the above screenshot.

  4. Set up the Deploy Release Lambda Function

    Perform the following steps to set up the Deploy Release Lambda function:

    1. Login to your AWS Management Console, select Lambda from the Services 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 12.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 or alternatively upload the code. For our example, we have created a sample code. 
    5. After installing the node modules in the create release project, upload deployRelease.zip on Lambda by selecting the Upload a .zip file option from the Code entry type drop-down. Keep Handler as deployRelease/index.handler and click on Save.
    6. Once we have uploaded the code in the editor, let's now set up the Environment variables by adding the Stack API key, your Contentstack region, Stack Management Token, and the publishing environment(s) as follows:

      deploy release EV.png

      Note:If you plan to deploy your release on multiple environments, you can click on the Add environment variable button and add more environments. If you have added more environment for deployment make sure to add the name of those environments in the deployRelease code as shown:

      code change.png

      In the above code, publishEnvironmentOne and publishEnvironmentTwo are the names of our multiple environments.

    With these steps, our Lambda Function for Deploy Release is configured. Let's now add a trigger to this lambda function.

  5. Add Trigger to the Deploy Release Lambda Function

    We will add a trigger to our lambda function to invoke it at a particular time. We do this as follows:

    1. In the Designer section within the Lambda dashboard, click on + Add trigger:

      Deploy release Trigger.png

    2. On the Add trigger page, select CloudWatch Events/EventBridge.
    3. From the Rule drop-down menu, you can select Create a new Rule or an existing rule (if you have created any rule before) as shown below:

      Create rule for Deploy release Trigger.png
    4. Select the Schedule expression option from Rule type.
    5. Then, provide a “CRON” expression in order to trigger the lambda at a scheduled time. The cron expression used for DeployRelease lambda is cron(40 23 * * ? *), that is, the lambda will trigger at 11:40 PM or 23:40 (24 hr format). This can be changed based on your requirement but make sure that the date on which “createRelease” is created and the date on which “deployRelease” is triggered should be of the same date/same day.

      For example, if a release is created on Thursday, May 14 2020, the deployRelease should also be triggered on that same day/date, that is, Thursday, May 14 2020.
    6. Ensure to check the Enable trigger option and click on Add.

    Once the trigger is configured, it will look similar to this:

    Complete_Deploy_Release_LF.png

    Note: You can disable or enable events rules using the options as shown in the above screenshot.

    With these steps, we have the set up ready. On the scheduled time and date, the Create Release Lambda Function will be invoked to create a release. 

    Accordingly, the Deploy Release Lambda Function will be invoked as well at the scheduled time and it will lock the release.

    Once the release is created, this is how appears:

    Final_Output.png

Additional Resource: We have created various other guides that cover the use of AWS Lambda functions. You may refer to our Webhook Integrations page for more details.

Was this article helpful?

Thanks for your feedbackSmile-icon

On This Page

top-arrow