How to set Branch Filter option in AWS CodeBuild cloudformation template?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people ask for a branch filter in an AWS CodeBuild CloudFormation template, they usually want one build project to react only to specific Git branches. The important detail is that CodeBuild does not use a dedicated BranchFilter property in CloudFormation. For webhook-based sources, you express the rule with Triggers.FilterGroups and match full Git refs such as refs/heads/main.
How Branch Filtering Actually Works
Two CodeBuild settings are easy to confuse:
- '
SourceVersiontells CodeBuild which revision to build when a build is started manually or by another service.' - '
Triggers.FilterGroupstells CodeBuild which webhook events should start a build automatically.'
If your source provider sends webhooks directly to CodeBuild, branch filtering belongs in FilterGroups. Each filter group is an AND condition, and multiple groups are OR conditions. That lets you express rules such as "build pushes to main and release/*, and also build pull requests targeting main."
The branch names are matched against full refs, not short names. That means main is usually wrong and refs/heads/main is right.
CloudFormation Example
The following template creates a GitHub-backed CodeBuild project that reacts only to pushes on main or release/*, plus pull requests aimed at main.
A few details matter here:
- '
Webhook: trueis required. Without it, the filter groups never receive webhook events.' - '
HEAD_REFchecks the source branch on a push or pull request.' - '
BASE_REFchecks the destination branch for pull request events.' - The regex should match the entire ref. Anchors such as
^and$prevent accidental matches.
You can deploy a template like this with the AWS CLI:
Choosing the Right Filter Type
The filter type depends on what you are trying to control:
- Use
HEAD_REFwhen you care about the branch that produced the event. - Use
BASE_REFwhen you care about the branch a pull request targets. - Use
EVENTto limit the trigger to push or pull-request activity.
That combination is more precise than trying to overload SourceVersion. For example, if you set SourceVersion: refs/heads/main, CodeBuild can still receive webhook events for other branches unless the webhook filters block them.
If your build is started by CodePipeline instead of a direct webhook, branch filtering normally belongs in the pipeline source stage, not in the CodeBuild project. In that setup, CodeBuild just builds what the pipeline hands to it.
Testing the Rule
A practical way to verify your filter is to push a small change to a branch that should match and then to one that should not. If only the matching branch starts a build, your regex and event type are aligned.
For a quick regex sanity check, you can test the same pattern locally:
That snippet should print True, True, and False in that order.
Common Pitfalls
The most common mistake is using a short branch name such as main in a HEAD_REF filter. CodeBuild webhook filters usually see the full ref, so the safer pattern is ^refs/heads/main$.
Another common mistake is assuming SourceVersion is a trigger filter. It is not. It controls what revision is built when a build starts, but it does not decide whether a webhook starts the build.
Teams also trip over filter-group logic. Inside one group, every filter must match. Across groups, any one group can match. If you intend two separate scenarios, put them in two separate groups.
Finally, do not try to solve a CodePipeline branch problem inside CodeBuild. If CodePipeline owns the source checkout, configure the branch at the source action level and keep CodeBuild focused on the build itself.
Summary
- In CloudFormation, branch filtering for CodeBuild webhooks is done with
Triggers.FilterGroups. - Use full Git refs such as
refs/heads/main, not short branch names. - '
HEAD_REFmatches the source branch, andBASE_REFmatches the pull-request target branch.' - '
SourceVersionis not a webhook filter.' - If CodePipeline triggers the build, branch selection usually belongs in the pipeline source stage.

