Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda for CodeCommit repo sync

I want to mirror my Bitbucket repository to CodeCommit and build from it, without affecting developer's workflow. Migration is easy, now the goal is to sync CodeCommit with Bitbucket, ideally, triggered by pushing to Bitbucket, but time-based sync is also acceptable.

I found this post: https://aws.amazon.com/blogs/devops/replicating-and-automating-sync-ups-for-a-repository-with-aws-codecommit/ and unanswered question here Mirror a Git Repo directly to AWS CodeCommit

Please, share you ideas how to write aws lambda function to sync CodeCommit repository with Bitbucket.

like image 515
C.B. Avatar asked Jul 10 '17 13:07

C.B.


People also ask

How do you trigger lambda in CodeCommit?

To create a trigger for a Lambda functionOpen the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home . In Repositories, choose the repository where you want to create triggers for repository events. In the navigation pane for the repository, choose Settings, and then choose Triggers.

How do I connect to CodeCommit repository?

To connect to a CodeCommit repositoryOpen the CodeCommit console at https://console.aws.amazon.com/codesuite/codecommit/home . In the region selector, choose the AWS Region where the repository was created. Repositories are specific to an AWS Region. For more information, see Regions and Git connection endpoints.

How do I update my CodeCommit repository?

To change a CodeCommit repository's nameRun the update-repository-name command, specifying: The current name of the CodeCommit repository (with the --old-name option). To get the CodeCommit repository's name, run the list-repositories command. The new name of the CodeCommit repository (with the --new-name option).


2 Answers

While I haven't tried this, I suspect the following workflow could work. The general idea would be to use the BitBucket webhooks feature to trigger the lambda function through the use of API gateway.

  1. Write a Lambda function which 'mirror' clones from your bitbucket repository url and pushes to your CodeCommit repository url. Your function would likely need to include its own standalone git client library.

  2. Create an API using API gateway which calls your lambda function. Potential challenges here might be verifying that POST requests going to your API are coming from Bitbucket, and not some other source.

  3. Create a new webhook for your Bitbucket repository with the URL being the url of your API you created in step 2. A 'Repository push' trigger would be sufficient to trigger a replication event after each push.

like image 185
David Jackson Avatar answered Sep 28 '22 06:09

David Jackson


One option is to use CodePipeline. Unfortunately with CodePipeline there is currently a weird process in making a Lambda function work as the sole process for the pipeline. Basically it comes down to:

  1. Make a CodePipeline with the AWS CodeCommit as your source repo
  2. Create one of the required Build/Deploy stages with the settings it asks for. Note that you will remove these later (unless you actually plan to use CodePipeline stages as given) so create a new CodeBuild project or something just to get through the wizard.
  3. Create a Lambda function that talks to BitBucket to sync your changes. The role attached must have permissions to interface with CodePipeline and CodeCommit. The Lambda function must also call either one of PubJobSuccessResult or PutJobFailureResult so that CodePipeline knows the Lambda actually did something and to not sit and wait for the function to complete.
  4. Now go back to the CodePipeline and edit it. For whatever stage was generated remove the existing action. Add a new Invoke action that points to the Lambda function and set the Role which gives access to CodePipeline for setting the job result and CodeCommit for reading the repo.

Another option is to use CloudWatch scheduling to invoke the Lambda at X interval if you're okay with a more delayed sync. This will probably be easier to setup in the long run, with the possibility of having null Lambda runs if there's nothing to sync, counting against your allocation.

like image 37
Chris White Avatar answered Sep 28 '22 07:09

Chris White