skip to Main Content

I have two AWS accounts. I develop code in one CodeCommit repository. Once it is done, I need to clone that code into the other account CodeCommit repository. Is there a way to do that using lambda function or any other method to automate the process.

Please help me, it was a really a headache more than a month. ๐Ÿ™‚

2

Answers


  1. There are several ways doing that. Essentially, what you’ll need is a trigger, that then kicks of the replication process into another account after each commit. Below are two possible ways documented doing this.

    Lambda + Fargate

    The first one uses a combination of Lambda, which you can select CodeCommit to be a trigger for. The Lambda function then runs a Fargate task, which in turn replicates the repository using git clone --mirror. Fargate is used here as the replication of larger repositories might exceed the temporary storage that Lambda can allocate.

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

    CodePipeline + CodeBuild

    This is probably the "cleaner" variant as it uses native CI/CD tooling in AWS, making it easier to set up as compared to ECS/Fargate, amongst other advantages.

    Here you’re setting up AWS CodePipeline, which will monitor the CodeCommit repository for any changes. When a commit is detected, it will trigger CodeBuild, which in turn runs the same git command outlined earlier.

    https://medium.com/geekculture/replicate-aws-codecommit-repositories-between-regions-using-codebuild-and-codepipeline-39f6b8fcefd2

    Login or Signup to reply.
  2. Assuming that you have repo 1 on account A, repo 2 on account B, you want to sync repo 1 -> repo 2

    The easiest way is to do the following:

    1. create SNS topic on Account A
    2. enable Notification for repo 1, and send all event to SNS topic
    3. create a lambda function to subscribe the SNS topic
    4. make sure you followed this guide https://docs.aws.amazon.com/codecommit/latest/userguide/cross-account.html to grant lambda function cross account CodeCommit permission
    5. write a python function to decide what git events you want to replicate. If you just want to sync the main branch and ignore all other branch, you can say something like: if event["source_ref"].endswith("main"), then use boto3 CodeCommit API https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codecommit.html, (take a look at batch_get_commits) to commit the change to the remote CodeCommit repo.

    However, I really doubt that do you really need to do this? How about just dump the all git history as a zip to S3 to your remote account? and just import everytime if you see any changes? I believe your remote account is mostly READ ONLY and just serve as a backup. If you only need backup, you can just dump to S3 and don’t even need to import.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search