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
- 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 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
- Leave all the default options under Batch Configuration, Artifacts, Logs steps in CodeBuild and click Create Build Project button.
- 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
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.