Lesson 2 of 2 Source Control and Pipeline Triggers

Connecting Repositories and Triggering Pipelines

A pipeline is only useful if the right events start it. This lesson explains how AWS CodeConnections connects third-party Git providers to AWS, which source options AWS CodePipeline and AWS CodeBuild support, and how webhooks, Amazon EventBridge rules and pipeline triggers start builds and pipelines without polling.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how AWS CodeConnections authorises AWS services to access GitHub, GitLab and Bitbucket repositories.
  • Describe the source actions available in CodePipeline, including Amazon S3 and Amazon ECR sources.
  • Distinguish between CodeBuild webhooks, CodePipeline triggers and EventBridge-based starts.
  • Choose an event-driven trigger configuration that meets a scenario's branch, tag and path requirements.

AWS CodeConnections

AWS CodeConnections is the service that lets AWS developer tools access repositories hosted by third-party Git providers. It was previously named AWS CodeStar Connections; the resource type and ARN prefix changed to codeconnections, but older connections with the codestar-connections prefix continue to work. A connection is an AWS resource that represents an authorised link between an AWS account and an account or organisation in GitHub, GitHub Enterprise Server, GitLab (including self-managed GitLab) or Bitbucket Cloud.

Creating a connection is a two-step process. The connection is first created in a PENDING state, for example by CloudFormation or the AWS CLI. An administrator must then complete the handshake in the console, which installs an AWS connector app in the Git provider and grants it access to selected repositories. Only then does the connection become AVAILABLE. This manual step cannot be automated for cloud-hosted providers, because it requires the provider's own authorisation flow; exam scenarios that ask why a newly deployed pipeline fails at the source stage with a connection error often have this as the cause.

For self-managed providers such as GitHub Enterprise Server or GitLab self-managed, a host resource is created first to represent the installation. If the server is in a private network, the host is configured with VPC details so that AWS can reach it.

Permissions for using a connection

A pipeline's service role or a CodeBuild project's service role must be allowed to use the connection. The permission is codeconnections:UseConnection (or codestar-connections:UseConnection for older connections) on the connection ARN. Missing this permission is a frequent cause of source-stage failures. Connections can also be shared with other accounts in the same organisation through AWS Resource Access Manager (AWS RAM).

Git sync for CloudFormation

CodeConnections also underpins Git sync, a feature that keeps a CloudFormation stack synchronised with a template and a deployment file in a repository branch. When a commit changes the template, CloudFormation updates the stack without a separate pipeline. It suits simple infrastructure stacks; complex releases that need tests and approvals still use CodePipeline.

True or false

CodeConnections facts

Exam skills 1.1.2

Check four facts about connecting AWS developer tools to Git providers.

Log in to try this activity Interactive activities are free with an account.

Source Options in AWS CodePipeline

A CodePipeline source action produces the first artifact of the pipeline. The exam expects you to know which source action fits each scenario. The descriptions below are followed by a summary table.

Git repositories through CodeConnections

The CodeStarSourceConnection action type retrieves source from GitHub, GitLab, Bitbucket and GitHub Enterprise Server through a connection. By default it produces a ZIP of the files at the commit. Selecting the full clone output option instead passes a reference that lets a later CodeBuild action clone the repository with its Git metadata, which is needed when a build runs Git commands such as reading tags or history. Full clone requires the CodeBuild role to have UseConnection permission as well.

AWS CodeCommit

AWS CodeCommit is an AWS-hosted Git service. It is available to new customers again, but it is not in the DOP-C02 in-scope service list, so exam scenarios focus on third-party Git providers connected through CodeConnections, Amazon S3 and Amazon ECR.

Amazon S3

An S3 source action starts the pipeline when a specific object key in a versioned bucket changes. The recommended detection method is an EventBridge rule that matches the CloudTrail or S3 event for the object; periodic polling is a legacy option. S3 sources suit artifacts produced outside the pipeline, such as a ZIP uploaded by another team or a template bundle exported from another system. The bucket must have versioning enabled, because the pipeline records the object version it used.

Amazon ECR

An ECR source action starts the pipeline when an image with a specified tag is pushed to a repository, using an EventBridge rule on the ECR image push event. It suits pipelines that deploy a container image built by a separate process, for example a base image team or a vendor.

The following table summarises the source options.

SourceChange detectionTypical use
GitHub, GitLab, Bitbucket through CodeConnectionsEvents from the connection (webhook-based)Application and infrastructure code in a third-party Git provider
Amazon S3EventBridge rule on object changes (polling is legacy)Artifacts uploaded by external processes
Amazon ECREventBridge rule on image push for a tagDeploying images built elsewhere

Starting AWS CodeBuild with Webhooks

CodeBuild can run independently of CodePipeline. When a CodeBuild project's source is a GitHub, GitHub Enterprise Server, GitLab or Bitbucket repository, the project can create a webhook in the provider. The provider then calls CodeBuild whenever a matching event occurs. Webhook filter groups decide which events start a build. A filter group contains filters such as EVENT (for example PUSH, PULL_REQUEST_CREATED, PULL_REQUEST_UPDATED, PULL_REQUEST_MERGED), HEAD_REF and BASE_REF (branch or tag names as regular expressions), FILE_PATH, COMMIT_MESSAGE and ACTOR_ACCOUNT_ID. All filters in a group must match (logical AND), and a build starts if any group matches (logical OR).

Webhooks are the usual way to run builds for pull requests, because a CodePipeline execution is typically tied to a branch rather than to individual pull requests. CodeBuild reports the build status back to the pull request, where branch protection can require it. CodeBuild can also act as a self-hosted runner for GitHub Actions and GitLab CI, which lets those workflows run jobs on CodeBuild compute.

# Example: create a webhook that builds pull requests targeting main
aws codebuild create-webhook --project-name orders-pr-build \
  --filter-groups '[[{"type":"EVENT","pattern":"PULL_REQUEST_CREATED,PULL_REQUEST_UPDATED,PULL_REQUEST_REOPENED"},
                     {"type":"BASE_REF","pattern":"^refs/heads/main$"}]]'

The filter above starts a build when a pull request whose base branch is main is opened, updated or reopened. The BASE_REF filter refers to the target branch of the pull request, whereas HEAD_REF refers to the source branch.

CodePipeline Triggers and Execution Starts

CodePipeline has two pipeline types. V1 pipelines start whenever their source changes and have limited filtering. V2 pipelines add triggers, which are configured on the pipeline and apply to a CodeConnections source action. A trigger can respond to push events or pull request events and filter on branches, Git tags and file paths, with include and exclude patterns that accept glob syntax. For example, a trigger can start the pipeline for tags matching release-* but not for ordinary pushes, or for pull requests opened against main. V2 pipelines are charged per action execution minute rather than per active pipeline per month, which also changes the cost of rarely used pipelines.

A pipeline can also be started in other ways: by the StartPipelineExecution API, for example from a Lambda function or another pipeline; by an EventBridge rule with the pipeline as a target, which lets any AWS event start a release; or on a schedule using an EventBridge Scheduler schedule. Change detection for older pipelines that use periodic polling should be migrated to event-based detection, because polling is slower and AWS recommends events.

When a V2 pipeline is started manually or by the API, source revisions can be overridden, for example to deploy a specific earlier commit ID or S3 object version. Pipeline-level variables can also be supplied at start time and used in action configuration.

Choosing a Trigger Mechanism

Several mechanisms can start automation from a repository event. The comparison below shows when each one is the most appropriate.

CodeBuild webhook
StartsA single build project
Event sourcesGit provider pushes and pull requests
FilteringFilter groups on event, refs, file paths, actor and commit message
Choose when Pull request validation or a standalone CI build.
CodePipeline V2 trigger
StartsA whole pipeline
Event sourcesPushes and pull requests through CodeConnections
FilteringBranches, tags and file paths with include and exclude globs
Choose when A release pipeline that should run only for specific branches, tags or paths.
EventBridge rule
StartsAny supported target, including pipelines, builds and Lambda functions
Event sourcesAny AWS service event, schedules and custom events
FilteringEvent patterns on any event field
Choose when Starting automation from non-Git events such as an ECR push, an S3 upload or a schedule.
Sort into groups

Choose the trigger

Exam skills 1.1.2 · 1.2.1

Choose the mechanism that should start automation for each repository event.

Log in to try this activity Interactive activities are free with an account.
Worked example

Separate Validation and Release Flows

An organisation hosts its code in GitHub. It wants every pull request to be built and unit tested before review, and it wants production releases only when a release manager pushes a semantic version tag. The release must deploy exactly the tagged commit.

  1. Create the connection

    An administrator creates a CodeConnections connection to the GitHub organisation, completes the handshake in the console and limits the app to the required repositories.

  2. Validation build

    A CodeBuild project with the GitHub source has a webhook filter group for pull request created, updated and reopened events against main. It reports status to GitHub, and branch protection requires the check.

  3. Release pipeline

    A V2 CodePipeline pipeline has a CodeConnections source action and a push trigger with a tag include filter of v*.*.*. Ordinary pushes to main do not start it.

  4. Permissions

    The pipeline service role has codeconnections:UseConnection on the connection ARN, and the CodeBuild role has the same permission if full clone is used.

Check yourself

A pipeline deployed by CloudFormation fails at the source stage with an error that the connection is not available. The template created the connection successfully. What is the most likely cause?

Show answer

The connection is still in the PENDING state. A connection created by CloudFormation or the CLI must be completed manually in the console by authorising the AWS app in the Git provider before it becomes AVAILABLE.

Exam Tips

Remember for the exam

  • AWS CodeConnections (formerly CodeStar Connections) links CodePipeline and CodeBuild to GitHub, GitLab and Bitbucket; connections created by IaC stay PENDING until the handshake is completed in the console.
  • Roles that use a connection need codeconnections:UseConnection on the connection ARN.
  • Use CodeBuild webhooks with filter groups for pull request builds; use CodePipeline V2 triggers to filter pipeline starts by branch, tag or file path.
  • S3 and ECR source actions should use EventBridge-based change detection; polling is a legacy option.
  • Choose the full clone output for a CodeConnections source when a build needs Git metadata such as tags or history.