Tutorial: Schedule Automated Builds Using CodeBuild - Amazon CloudWatch Events

Tutorial: Schedule Automated Builds Using CodeBuild

Note

Amazon EventBridge is the preferred way to manage your events. CloudWatch Events and EventBridge are the same underlying service and API, but EventBridge provides more features. Changes you make in either CloudWatch or EventBridge will appear in each console. For more information, see Amazon EventBridge.

In the example in this tutorial, you schedule CodeBuild to run a build every week night at 8PM GMT. You also pass a constant to CodeBuild to be used for this scheduled build.

To create a rule scheduling an CodeBuild project build nightly at 8PM
  1. Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.

  2. In the navigation pane, choose Events, Create rule.

  3. For Event Source, do the following:

    1. Choose Schedule.

    2. Choose Cron expression and specify the following as the expression: 0 20 ? * MON-FRI *. For more information about cron expressions, see Schedule Expressions for Rules.

  4. For Targets, choose Add target, CodeBuild project.

  5. For Project ARN, type the ARN of the build project.

  6. In this tutorial, we add the optional step of passing a parameter to CodeBuild, to override the default. This is not required when you set CodeBuild as the target. To pass the parameter, choose Configure input, Constant (JSON text).

    In the box under Constant (JSON text), type the following to set the timeout override to 30 minutes for these scheduled builds: { "timeoutInMinutesOverride": 30 }

    For more information about the parameters you can pass, see StartBuild. You cannot pass the projectName parameter in this field. Instead, you specify the project using the ARN in Project ARN.

  7. CloudWatch Events can create the IAM role needed for your build project to run:

    • To create an IAM role automatically, choose Create a new role for this specific resource.

    • To use an IAM role that you created before, choose Use existing role. This must be a role that already has sufficient permissions to invoke the build. CloudWatch Events does not grant additional permissions to the role that you select.

  8. Choose Configure details

  9. For Rule definition, type a name and description for the rule.

  10. Choose Create rule.