In some scenarios you might have the need to replicate an AWS CodeCommit repository. When I was setting up a test organization using AWS Deployment Framework (ADF) I ran into this myself. Because I want to test the deployment of my landing zone I needed to have a close replica. This includes the CodeCommit setup.
But at the same time I did not want to change the development workflow. The workflow is pretty straight forward. You create a feature branch to work in. When you are ready you merge it to a
development branch. When it needs to go to production you merge it into the
So we will use the
development branch to deploy to the test organization. But, because the test organization is a replica of production. Merging to the
development branch would not have effect on the test organization. For this we need to synchronize the
development branch to the test organization.
How does it work
We will use an AWS Lambda function with a git client to perform the following actions:
- Clone the CodeCommit repository.
- Checkout the
- Assume the an IAM role in the target account.
- Perform a
git push. The name of the CodeCommit repositories in both organization are identical. By assuming a role in the AWS Account that hosts the CodeCommit repositories in the test organization. You can perform a
git pushassuming you have the correct permissions. The changes are then pushed to the repository in the test organization. There is a difference between the test and production organization. The test organization is configured to listen to the
developmentbranch. The production organization listens to the
mainbranch. With this in place you have the following workflow:
- Create a pull request for your feature branch into
- When reviewed and approved you can merge the pull request.
- (Automatic) an event is triggered, we use an event rule to trigger the Lambda function.
- (Automatic) the Lambda function will then:
- Clone and pull the code from the production repository.
- Assume a role in the test organization.
- Push the code to the remote repository in the test organization.
- (Automatic) the CodePipeline for the repository is triggered.
Now you only need to confirm whether your changes are correct in the test organization. Once satisfied you can merge the change to the
mainbranch. This will deploy the same changes in production.
Synchronizing of git repositories can help you with automated testing. See the aws-lambda-git repository for more information.