Sync AWS CodeCommit repositories

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 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 to perform the following actions:

  1. Clone the CodeCommit repository.
  2. Checkout the development branch.
  3. Assume the an IAM role 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 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 repository for more information.

Joris has been working with the AWS cloud since 2009 and focussing on building event driven architectures. While working with the cloud from (almost) the start he has seen most of the services being launched. Joris strongly believes in automation and infrastructure as code and is open to learn new things and experiment with them, because that is the way to learn and grow. In his spare time he enjoys running and runs a small micro brewery from his home.
Share this article: Tweet this post / Post on LinkedIn