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

1_U-OvTsvQdwlyssWSOWOS9A

  • 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.

1_3H203mKJltL-__VzfTPfAw

  • 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

1_hnEEXl6RMPP-hcx_rAKLuQ

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

1_oesn8MIdGpmjfO8FQ6GNuA

  • Enter the following commands.
version: 0.2

env:
  #variables:
     # key: "value"
     # key: "value"
  #parameter-store:
     # key: "value"
     # key: "value"
  #secrets-manager:
     # key: secret-id:json-key:version-stage:version-id
     # key: secret-id:json-key:version-stage:version-id
  #exported-variables:
     # - variable
     # - variable
  git-credential-helper: yes
#batch:
  #fast-fail: true
  #build-list:
  #build-matrix:
  #build-graph:
phases:
  #install:
    #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.
    #runtime-versions:
      #nodejs: 12
      # name: version
    #commands:
      # - command
      # - command
  #pre_build:
    #commands:
      #- ls -lt
  build:
    commands:
      - 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 https://git-codecommit.us-east-1.amazonaws.com/v1/repos/demoapp LocalRepository
      - cd LocalRepository
      - git remote set-url --push origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/demoapp
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - git fetch -p origin
      - git push --mirror
  post_build:
    commands:
      - echo Build completed
#reports:
  #report-name-or-arn:
    #files:
      # - location
      # - location
    #base-directory: location
    #discard-paths: yes
    #file-format: JunitXml | CucumberJson
#artifacts:
  #files:
    # - location
    # - location
  #name: $(date +%Y-%m-%d)
  #discard-paths: yes
  #base-directory: location
#cache:
  #paths:
    # - 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

1_aj6Eo77uOwTTH-AehlTdew

  • 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

1_brQs7CGq9WGE38_x4WPLdg

1_7nE_0ceOTu0oTczMCeAYEA

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

1_7tajBxeGlA7BFf-46x7nzw

  • 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.

1_qEi7STiqxdxlUw9Wi0cECg

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

1_JvAbcL6bRF1gYO87KB20NA

  • Click on Skip deploy stage

1_VYbaurYa072GrPaSBA4FyA

  • Review the pipeline details and click Create Pipeline.

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

1_ADy590zF_aJsc4C9gHw3Gg

Summary

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

References

Top comments (0)