DEV Community

Cover image for Replicate AWS CodeCommit Repositories Between Regions Using CodeBuild And CodePipeline
Amrut Patil
Amrut Patil

Posted on

Replicate AWS CodeCommit Repositories Between Regions Using CodeBuild And CodePipeline

Replicating code repositories from one AWS region to another is a commonly used DevOps task. This article demonstrates how to set up continuous replication of an AWS CodeCommit repository among multiple AWS regions using AWS CodeBuild and AWS CodePipeline. This approach can be useful to maintain backups of CodeCommit repositories in different regions. 

A major benefit of using this approach is that the replication process can be easily set up to trigger based on events, such as commits made to the repository.

Note: You need to have a basic understanding of CodeCommit, CodeBuild, CodePipeline, and Identity Access Management (IAM). Please refer to the AWS Documentation in case you are not familiar with these AWS Services.

We will be replicating a repository in us-east-1 (N. Virginia) to the us-east-2 (Ohio) region.

Let's get started

Step 1: Setting up Build Project in CodeBuild

  • Under Project Configuration, enter a name for the CodeBuild Project. In our case, it is demoappreplication


  • Under Source, select the Source Provider as AWS CodeCommit and select the repository and branch within the repository you wish to replicate to another region. In our case, we will replicate the test branch under demoapp repository.


  • Under Environment, select Operating System as Amazon Linux 2, Runtime(s) as Standard, Image as aws/codebuild/amazonlinux2-x86_64-standard:3.0, Image Version as Always use the latest image for this runtime version, and Environment type as Linux


  • Under Buildspec, select Insert build commands and click on Switch to editor.


  • Enter the following commands.
version: 0.2

     # key: "value"
     # key: "value"
     # key: "value"
     # key: "value"
     # key: secret-id:json-key:version-stage:version-id
     # key: secret-id:json-key:version-stage:version-id
     # - variable
     # - variable
  git-credential-helper: yes
  #fast-fail: true
    #If you use the Ubuntu standard image 2.0 or later, you must specify runtime-versions.
    #If you specify runtime-versions and use an image other than Ubuntu standard image 2.0, the build fails.
      #nodejs: 12
      # name: version
      # - command
      # - command
      #- ls -lt
      - git config --global --unset-all credential.helper
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - git clone --mirror LocalRepository
      - cd LocalRepository
      - git remote set-url --push origin
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - git fetch -p origin
      - git push --mirror
      - echo Build completed
      # - location
      # - location
    #base-directory: location
    #discard-paths: yes
    #file-format: JunitXml | CucumberJson
    # - location
    # - location
  #name: $(date +%Y-%m-%d)
  #discard-paths: yes
  #base-directory: location
    # - paths
Enter fullscreen mode Exit fullscreen mode
  • Let CodeBuild automatically create a service role for us. In our case, CodeBuild will create a new service role named codebuild-demoappreplication-service-role


  • Leave all the default options under Batch Configuration, Artifacts, Logs steps in CodeBuild and click Create Build Project button.

Step 2: Configuring CodeBuild Service Role in IAM

  • Navigate to IAM and add the following codeCommit:GitPush permissions for us-east-2 region resource to the service role CodeBuild created for us. In our case, we will update the permissions and resource for the codebuild-demoappreplication-service-role



Step 3: Setting up CodePipeline

You can further extend this setup to trigger code replication when a code merge happens in the CodeCommit repository in us-east-1, by triggering a CodeBuild within a CodePipeline.

  • Enter a name for the pipeline


  • Under Source provider, select AWS CodeCommit. In our case, we will trigger a build when CodeCommit detects a code change in the test branch under demoapp repository.


  • Under the Build step, select Build provider as CodeBuild and our project name demoappreplication


  • Click on Skip deploy stage


  • Review the pipeline details and click Create Pipeline.

  • Once the Code Pipeline runs successfully, you should see the following:



This article demonstrates setting up a repository replication of an AWS CodeCommit repository across multiple regions using CodeBuild and CodePipeline.


Top comments (0)