Accessing Multiple CodeCommit Repos

AWS provides a handy Git based repository to their customers called CodeCommit. Accessing the repository is easy enough, upload your SSH Key to the AWS Console, and then add CodeCommit to your SSH configuration:

# ~/.ssh/config
Host git-codecommit.*.amazonaws.com
User USER1EXAMPLEARN1
IdentityFile ~/.ssh/USER1EXAMPLEARN1_rsa

This is wonderful until you need to access more than one AWS Account’s CodeCommit repo. How do you identify between two different AWS Account repos that use the same URL? You update your SSH configuration like this:

# ~/.ssh/config
Host project1
  Hostname git-codecommit.*.amazonaws.com
  User USER1EXAMPLEARN2
  IdentityFile ~/.ssh/USER1EXAMPLEARN2_rsa

Host project2
  Hostname git-codecommit.*.amazonaws.com
    User USER2EXAMPLEARN2
    IdentityFile ~/.ssh/USER1EXAMPLEARN1_rsa

finally, you need to update your Git configuration in each project accordingly:

# ${PROJECT1}/.git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://project1/v1/repos/my-repo-name
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/head/master

and

# ${PROJECT2}/.git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://project2/v1/repos/my-repo-name
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/head/master

NOTE: The url for the project just needs to match the Host line in your SSH config. So for clarity try something like

# ~/.ssh/config
Host my-dev-site

and

# ${PROJECT1}/.git.config
...
url = ssh://my-dev-site/v1/repos/my-repo-name 
...