Sync AWS CodeCommit repositories
Tags: aws, sync, codecommit, repos
Published: 2022-12-23 08:22:31

In some scenarios you might have the need to replicate an [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html "Learn more about AWS CodeCommit") repository. When I was setting up a [test organization](https://binx.io/blog/2022/01/19/testing-your-landing-zone-when-using-adf/ "Why do I need a test organization?") using [AWS Deployment Framework (ADF)](https://github.com/awslabs/aws-deployment-framework "Learn more about AWS Deployment Framework") 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 `main` branch.  
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](https://binx.io/blog/2022/03/24/running-a-git-binary-in-aws-lambda/ "Running a git binary in AWS Lambda") to perform the following actions:

1. Clone the CodeCommit repository.
2. Checkout the `development` branch.
3. Assume the an [IAM role](https://github.com/binxio/aws-lambda-git/blob/main/target-template.yml "IAM Role example") in the target account.
4. 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 push` assuming 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 `development` branch. The production organization listens to the `main` branch.  
With this in place you have the following workflow:
5. Create a pull request for your feature branch into `development`.
6. When reviewed and approved you can merge the pull request.
7. (Automatic) an event is triggered, we use an [event rule](https://github.com/binxio/aws-lambda-git/blob/f29a1dbaadc5a00fd9f2aa4f963870c24bb64244/template.yml#L18 "Event Rule example") to trigger the Lambda function.
8. (Automatic) the Lambda function will then:
	1. Clone and pull the code from the production repository.
	2. Assume a role in the test organization.
	3. Push the code to the remote repository in the test organization.
9. (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 `main` branch. This will deploy the same changes in production.

Conclusion
----------

Synchronizing of git repositories can help you with automated testing. See the [aws-lambda-git](https://github.com/binxio/aws-lambda-git/ "Code example") repository for more information.