AWS CodeCommit

A quick guide to using AWS CodeCommit using the AWS CLI.

Getting help

aws codecommit help

Enough for getting around!

Generate SSH credentials

Required for cloning a repository:

ssh-keygen -f ~/.ssh/codecommit_rsa

Then, set this up with CodeCommit:

  1. Sign to the AWS Console.

  2. Open your IAM Users page, and select the user you wish to give CodeCommit access to with this SSH key (or create a new one and use it for the rest of the steps).

  3. Click Add permissions, then Attach existing policies directly.

  4. Use the Filter policies search box to search for "codecommit". Choose the checkbox near either AWSCodeCommitFullAccess (to allow repository deletion) or AWSCodeCommitPowerUser to allow write permission.

  5. Click Next: Review, then Add permissions.

  6. Switch to the Security Credentials tab for your IAM user, and click the Upload SSH public key button.

  7. Copy the contents of ~/.ssh/ and paste it into the Upload SSH public key dialog.

  8. Create (or edit) ~/.ssh/config, and add the following contents:

    Host git-codecommit.*
      User REPLACEME
      IdentityFile ~/.ssh/codecommit_rsa
      PubkeyAcceptedKeyTypes +ssh-rsa

    You'll fill in that sample REPLACEME value next...


    That last setting, PubkeyAcceptedKeyTypes, was required on my Fedora installation because the ssh-rsa algorithm used by CodeCommit has been deprecated.

  9. Back in the AWS console, copy the SSH key ID from your IAM user's Summary page, and paste it in place of REPLACEME in the ~/.ssh/config file.

  10. Save and close the file.

  11. Go to your command-line, and change ~/.ssh/config's permissions to 600 (AKA -rw-------):

    chmod 600 ~/.ssh/config

You should now be able to use the SSH key to access your CodeCommit repositories!

For more info

Creating a repository

aws codecommit create-repository --repository-name repositoryName

Creates a repository using repositoryName as the repository name, and prints its info (the same info you get when running aws codecommit get-repository).

After creating a repository, you can clone it to add files to your new, blank repository.

Listing your repositories

aws codecommit list-repositories

Prints a listing of the repositories for your account. For each, it reports the repositoryName and repositoryId.

Getting info about a repository

aws codecommit get-repository --repository-name repositoryName

Lists the metadata associated with the repository, including:

  • accountId - The ID of your AWS account.

  • repositoryId - A GUID that represents your repository.

  • repositoryName - The "friendly" name of the repository.

  • repositoryDescription - A description of the repository. You can change this by using the aws codecommit update-repository-descripotion command.

  • defaultBranch - The branch that will be cloned when one is not specified. You can change this by using the aws codecommit update-default-branch command.

  • lastModifieddate - The last modified date of the repository.

  • creationDate - The creation date of the repository

  • cloneUrlHttps - The URL to use when cloning the repository using HTTPS

  • cloneUrlSsh - The URL to use when cloning the repository with SSH

  • Arn - The AWS Resource Name (ARN) for the repository.

The cloneUrlSsh field, in particular, is important when you want to clone the repository in order to work with its files.

Cloning a repository

Once you've set up your SSH credentials correctly, you can clone the repository by:

  1. Using the aws codecommit get-repository command to get the cloneUrlSsh value for cloning the repository.

  2. Using the cloneUrlSsh value to clone your repository like any other git repository:

    git clone cloneUrlSsh

Once you've cloned the repository, you can use normal git commands to work with your local copy, or git pull and git push to interact with your codecommit repository.

Migrating your existing git repository to CodeCommit

Here's an example that puts it all together. ;)

Say that I have a repository (repositoryName) hosted at another server and I want to move it to CodeCommit, without losing any of my history. I can do it all with an AWS CLI command and a few git commands:

  1. Clone the repository I want to move:

    git clone ssh://old-server/repositoryName.git
  2. Enter the cloned repository directory:

    cd repositoryName
  3. Create the new repository on CodeCommit:

    aws codecommmit create-repository --repository-name repositoryName

    This prints information about your repository. For example:

        "repositoryMetadata": {
            "accountId": "AWSACCOUNTID",
            "repositoryId": "0d1800c6-some-long-guid-9a667b8da8fb",
            "repositoryName": "repositoryName",
            "lastModifiedDate": timestamp,
            "creationDate": timestamp,
            "cloneUrlHttp": "",
            "cloneUrlSsh": "ssh://",
            "Arn": "arn:aws:codecommit:region:AWSACCOUNTID:repositoryName"
  4. Create a new git remote, using the value of cloneUrlSsh:

    git remote add codecommit ssh://
  5. Now, push your repository to the new remote:

    git push codecommit master
  6. Remove the old origin remote:

    git remote remove origin
  7. Rename your codecommit remote as origin:

    git remote rename codecommit origin

That's it...!

More information

For more information about using CodeCommit, head over to the official AWS documentation: