Skip to content

How do I override AWS CodePipeline variables using Amazon EventBridge and conditionally skip stages with entry conditions?

11 minute read
Content level: Advanced
0

I want to dynamically override AWS CodePipeline variables using Amazon EventBridge rules or schedules, and use those variables with entry conditions to conditionally skip pipeline stages. This article walks through two use cases: overriding variables based on a CodeCommit event, and overriding variables on a schedule to skip a manual approval stage.

With recent improvements to the AWS CodePipeline and Amazon EventBridge integration, EventBridge now supports passing variables that override CodePipeline variables. This allows you to override existing CodePipeline variables based on a source trigger event or schedule.


Architecture Components

The architecture to override pipeline variables consists of the following components:

  1. AWS CodePipeline with existing variables
  2. Amazon EventBridge rule or schedule with Input Transformer

Section A — EventBridge Rule flow:

CodeCommit Event → EventBridge Rule (Event Pattern) → Input Transformer → CodePipeline (variables overridden)

Section B — EventBridge Schedule flow:

EventBridge Schedule (Rate/Cron) → Input Transformer → CodePipeline (variables overridden)

Important:

  • Pipeline variable names in the Input Template must match the variable names defined in the pipeline exactly (case-sensitive).
  • The Input Template must follow the CodePipeline StartPipelineExecution API format for the variables parameter.
  • A maximum of 50 variables can be defined per pipeline.

Steps

Follow the steps below to override pipeline variables using an EventBridge rule or EventBridge schedule:

  • Section A: Override pipeline variables based on a specific AWS CodeCommit event (e.g., pull request merge status update). Instructions on how to use an EventBridge rule to override CodePipeline variables based on specific events, such as a pull request merge status change in CodeCommit.

  • Section B: Override pipeline variables on a schedule for conditional execution of pipeline stages (e.g., skipping a manual approval stage). Instructions on how to use an EventBridge schedule trigger to periodically override a CodePipeline variable, in this case to conditionally skip a manual approval stage based on the variable value.


Prerequisites

The EventBridge rule requires an IAM role with permission to start the pipeline. When configuring the EventBridge target, either create a new role or attach the following policy to an existing role:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "codepipeline:StartPipelineExecution",
            "Resource": "arn:aws:codepipeline:us-east-1:123456789012:your-pipeline-name"
        }
    ]
}

Note: Replace 123456789012 with your AWS account ID and your-pipeline-name with your pipeline name.

A. EventBridge Rule

For this approach, CodePipeline variables are overridden based on events from different source types. In this example, the source event is the pullRequestMergeStatusUpdated event in CodeCommit. For the different types of CodeCommit events available for EventBridge rules, see [1]. Based on this event, details like destinationCommit, sourceCommit, destinationReference, and pullRequestStatus are passed to the CodePipeline.

Follow the steps below to configure an EventBridge rule trigger for CodePipeline:

CodePipeline

  1. Start the CodePipeline initialization process from the CodePipeline console. In Step 2 (Choose Pipeline Settings), under Advanced Settings, add the following variables:
"variables": [
            {
                "name": "destinationReference",
                "defaultValue": "TestRef"
            },
            {
                "name": "destinationCommit",
                "defaultValue": "TestCommit"
            },
            {
                "name": "pullRequestStatus",
                "defaultValue": "TestpullRequest"
            },
            {
                "name": "sourceCommit",
                "defaultValue": "TestSourceCommit"
            }
        ]

Pipeline variables configuration showing destinationReference, destinationCommit, pullRequestStatus, and sourceCommit

  1. In Step 3 (Add source stage), while selecting the source, uncheck the option "Create EventBridge rule to automatically detect source changes". This ensures there is only one trigger for the pipeline and avoids multiple executions.

  2. For this example, the Commands build stage is used to consume the pipeline variable. Any stage that accepts pipeline variables works here. The commands build stage is defined with environment variables referring to the pipeline variables defined in the previous step. The commands section includes echo commands to print the environment variables during execution:

"environmentVariables": [
                            {
                                "name": "destinationReference",
                                "value": "#{variables.destinationReference}"
                            },
                            {
                                "name": "destinationCommit",
                                "value": "#{variables.destinationCommit}"
                            },
                            {
                                "name": "pullRequestStatus",
                                "value": "#{variables.pullRequestStatus}"
                            },
                            {
                                "name": "sourceCommit",
                                "value": "#{variables.sourceCommit}"
                            }
                        ]

Commands build stage configuration with environment variables referencing pipeline variables

  1. At this point, a two-stage pipeline should be configured.

EventBridge

  1. From the EventBridge console, create a new rule. For Step 1 (Define rule detail), provide the rule name, event bus, and select the rule type Rule with an event pattern.

  2. For Step 2 (Build event pattern), under Event source select Others, and under Event Pattern section select Custom Pattern (JSON Editor).

  3. For the custom pattern for CodeCommit, see [2] for the pullRequestMergeStatusUpdated event. The pattern for pullRequestMergeStatusUpdated is:

{
  "version": "0",
  "id": "01234567-0123-0123-0123-012345678901",
  "detail-type": "CodeCommit Pull Request State Change",
  "source": "aws.codecommit",
  "account": "123456789012",
  "time": "2019-06-12T10:23:43Z",
  "region": "us-east-2",
  "resources": [
    "arn:aws:codecommit:us-east-2:123456789012:MyDemoRepo"
  ],
  "detail": {
    "author": "arn:aws:sts::123456789012:assumed-role/Admin/Mary_Major",
    "callerUserArn": "arn:aws:sts::123456789012:assumed-role/Admin/Mary_Major",
    "creationDate": "Mon Mar 11 14:42:31 PDT 2019",
    "description": "An example description.",
    "destinationCommit": "4376719EXAMPLE",
    "destinationReference": "refs/heads/main",
    "event": "pullRequestMergeStatusUpdated",
    "isMerged": "True",
    "lastModifiedDate": "Mon Mar 11 14:42:31 PDT 2019",
    "mergeOption": "FAST_FORWARD_MERGE",
    "notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: MyDemoRepo.",
    "pullRequestId": "1",
    "pullRequestStatus": "Closed",
    "repositoryNames": ["MyDemoRepo"],
    "revisionId": "bdc0cb9beEXAMPLE",
    "sourceCommit": "0701696EXAMPLE",
    "sourceReference": "refs/heads/test-branch",
    "title": "My Example Pull Request"
  }
}
  1. Based on the sample pattern, a custom pattern is created for a repository named "MyDemoRepo" where the pull request is merged with destination reference as "refs/heads/main". The custom pattern for this example:
{
  "detail-type": ["CodeCommit Pull Request State Change"],
  "source": ["aws.codecommit"],
  "account": ["123456789012"],
  "region": ["us-east-1"],
  "resources": ["arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo"],
  "detail": {
    "event": ["pullRequestMergeStatusUpdated"],
    "pullRequestStatus": ["Closed"],
    "isMerged": ["True"],
    "destinationReference": ["refs/heads/main"],
    "repositoryNames": ["MyDemoRepo"]
  }
}

Note: Replace 123456789012 with your AWS account ID and MyDemoRepo with your repository name.

EventBridge event pattern configuration for CodeCommit pull request merge

Note: In this scenario, the source is CodeCommit and most EventBridge events related to CodeCommit are listed in [1]. If you need to trigger or pass variables based on a custom event, create an event rule for the service and target it to a CloudWatch log group. In the log group, identify the relevant event and create an EventBridge rule for the pipeline accordingly.

  1. After setting the custom pattern, specify the target type as AWS Service and select CodePipeline for the target option. Enter the pipeline ARN to specify which pipeline this EventBridge rule triggers.

  2. On the same page for Step 3 Select target(s) section, choose Additional settings. Under Configure target input, select Input Transformer.

  3. For Input Transformer, choose Configure Input Transformer and fill in the Input path and Input template as follows:

Input Path:

{
  "destinationCommit": "$.detail.destinationCommit",
  "destinationReference": "$.detail.destinationReference",
  "pullRequestStatus": "$.detail.pullRequestStatus",
  "sourceCommit": "$.detail.sourceCommit"
}

Input Template:

{
  "variables": [
    {
      "name": "destinationCommit",
      "value": "<destinationCommit>"
    },
    {
      "name": "destinationReference",
      "value": "<destinationReference>"
    },
    {
      "name": "pullRequestStatus",
      "value": "<pullRequestStatus>"
    },
    {
      "name": "sourceCommit",
      "value": "<sourceCommit>"
    }
  ]
}

Input Transformer configuration showing Input Path and Input Template

The Input Path maps event details to variables that are passed to the pipeline. Modify the Input Template to include additional variables or hardcoded values as needed.

  1. Choose Confirm, then choose Next to create the rule.

  2. Once the pull request is merged, the CodePipeline variables are overridden based on the pull request event.

Pipeline execution showing overridden variables from EventBridge rule

  1. The variables can also be verified in the Commands section of the pipeline:

Commands build stage output showing overridden variable values


B. EventBridge Schedule

For this approach, CodePipeline variables are overridden based on an EventBridge schedule. A schedule passes a hardcoded value through the Input Template. One use case is skipping a manual approval stage based on an entry condition. The entry condition evaluates the variable and either skips or executes the approval stage.

Follow the steps below to configure an EventBridge schedule rule trigger for CodePipeline:

CodePipeline

  1. Start the CodePipeline initialization process from the CodePipeline console. In Step 2 (Choose Pipeline Settings), under Advanced Settings, add the following variables:
"variables": [
            {
                "name": "SkipManualApproval",
                "defaultValue": "false"
            }
        ]

Pipeline variable configuration showing SkipManualApproval variable

  1. In Step 3 (Add source stage), while selecting the source, uncheck the option "Create EventBridge rule to automatically detect source changes". This ensures there is only one trigger for the pipeline and avoids multiple executions.

  2. For this example, the Commands build stage is used to perform build actions. Any stage that accepts pipeline variables works here. The two-stage pipeline is created first.

  3. Once the pipeline is created, edit the pipeline to add a Manual Approval stage between the source stage and the build stage. The manual approval stage has an entry condition that verifies the pipeline variable SkipManualApproval and based on the result either skips or executes the stage.

Note: If the entry condition is met, the stage executes; otherwise, it is skipped. For more information about entry conditions based on pipeline variable checks, see [3].

  1. The entry condition is configured for the manual approval stage with a variable check for #{variables.SkipManualApproval}, value set to True with a Not Equals operator.

Entry condition configuration showing variable check for SkipManualApproval

The logic works as follows:

  • When SkipManualApproval = false → condition (false != true) is true → stage executes (approval required)
  • When SkipManualApproval = true → condition (true != true) is false → stage skipped (approval bypassed)
  1. The pipeline configuration is now saved.

EventBridge Schedule

  1. From the EventBridge console, create a new rule. For Step 1 (Define rule detail), provide the rule name, event bus, and select the rule type Schedule. Choose Continue to create rule.

  2. In Step 2 (Define Schedule), choose either a cron expression or a rate-based schedule. For this example, select a rate of 1 hour:

EventBridge schedule configuration showing rate of 1 hour

  1. For Step 3 (Select target(s)), specify the target type as AWS Service and select CodePipeline for the target option. Enter the pipeline ARN.

  2. On the same page for Step 3 "Select target(s)" section, choose Additional settings. Under Configure target input, select Input Transformer.

  3. For Input Transformer, choose Configure Input Transformer and fill in the Input path and Input template as follows:

Input Path:

{}

Note: The Input Path is empty because the schedule event does not contain dynamic values — the variable value is hardcoded in the Input Template.

Input Template:

{
  "variables": [
    {
      "name": "SkipManualApproval",
      "value": "true"
    }
  ]
}

Input Transformer configuration for schedule with hardcoded SkipManualApproval value

  1. Choose Confirm, then choose Next to create the schedule rule.

  2. The schedule rule triggers the pipeline every hour with the variable SkipManualApproval set to true. Because the variable is set to true, the entry condition fails and the manual approval stage is skipped.

  3. Pipeline execution:

Pipeline execution showing manual approval stage skipped

Entry Condition:

Entry condition evaluation showing stage skipped because condition was not met

Because the entry condition failed, the manual approval stage was skipped and the build stage executed.


Troubleshooting

Pipeline does not trigger after EventBridge rule is created:

  • Verify the EventBridge rule's IAM role has codepipeline:StartPipelineExecution permission for the target pipeline.
  • Confirm the event pattern matches the actual event. Target the event to a CloudWatch log group first to verify the event structure.
  • Check the EventBridge rule is enabled and in the correct region.

Variables are not overridden (default values appear):

  • Verify the Input Template variable names match exactly with the pipeline variable names (case-sensitive).
  • Confirm the Input Path JSONPath expressions correctly reference the event detail fields.
  • Ensure the Input Template follows the StartPipelineExecution API format with the variables array structure.

Pipeline triggers multiple times:

  • Ensure the "Create EventBridge rule to automatically detect source changes" option is unchecked in the pipeline source stage configuration.
  • Verify there are no duplicate EventBridge rules targeting the same pipeline.

EventBridge rule shows "Failed invocations" metric:

  • Check the EventBridge rule's IAM role permissions in the CloudWatch metrics for the rule.
  • Verify the pipeline ARN in the target configuration is correct.

References

[1] CodeCommit monitoring events - https://docs.aws.amazon.com/codecommit/latest/userguide/monitoring-events.html

[2] pullRequestMergeStatusUpdated event - https://docs.aws.amazon.com/codecommit/latest/userguide/monitoring-events.html#pullRequestMergeStatusUpdated

[3] Stage conditions in CodePipeline - https://docs.aws.amazon.com/codepipeline/latest/userguide/stage-conditions.html#stage-conditions-considerations-rules

[4] CodePipeline pipeline variables - https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-variables.html

[5] EventBridge Input Transformation - https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-transform-target-input.html

[6] StartPipelineExecution API - https://docs.aws.amazon.com/codepipeline/latest/APIReference/API_StartPipelineExecution.html