Replicate AWS CodeCommit Repositories Between Regions Using CodeBuild And CodePipeline
Tags: aws, devops, programming
Published: 2021-04-16 02:17:20

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6bhb6kaf068i9uthd2tw.png)
 

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvggo2vybov0rko1g6mh.png)
 

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/74yqln2vhslwjdpze6gd.png)
 

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

![1_oesn8MIdGpmjfO8FQ6GNuA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oniwq6541bjj96epwv1y.png)
 

* 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
```

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jb2ic21dyi11bdl43x4.png)
 

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdl1ay6lrub3um6dinon.png)

![1_7nE_0ceOTu0oTczMCeAYEA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q330w8di3h9s0pryezge.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzydsedsoztzney48kpf.png)

* 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53epo9lbbj32scr7wnry.png)

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

![1_JvAbcL6bRF1gYO87KB20NA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67xk13klsz2ekjzz9c66.png)

* Click on Skip deploy stage

![1_VYbaurYa072GrPaSBA4FyA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yjpsjwigv85v5jzhbqh.png)

* Review the pipeline details and click Create Pipeline.

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

![1_ADy590zF_aJsc4C9gHw3Gg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyhd6wg9jizxcvhf2wst.png)

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

### References
* https://aws.amazon.com/blogs/devops/replicate-aws-codecommit-repository-between-regions-using-aws-fargate/