Fixing AWS CodeCommit Permission Denied issue

Written on September 8, 2016

I’ve been looking into more of Amazon Web Services offerings these days as alternatives to other services I already use for web development.

One of those offerings is CodeCommit, which is a way to connect a local git repository with a remote repository to host your codebase. The service is similar to GitHub without much of the social aspects.

Getting started is fairly easy. You first setup a repo through the AWS CLI or through AWS’ web console. Then you’ll want to make sure you’ve given access to your machine on AWS by setting up a user under the IAM service. Once a user is setup, you’ll have to make sure to upload your machine’s SSH public key. Which could be obtained using this command on a Mac:

pbcopy < ~/.ssh/id_rsa.pub

What this command does is copies your SSH key to your system’s clipboard, which now you can provide in IAM.

Upon finishing all these steps and adding the CodeCommit repo as a remote repo, you’d think a simple git push would be all it takes to get up and running.

But I kept running into this warning when trying to push to the new remote repo.

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Turns out the solution to fixing this issue is to preface all connection attempts to the CodeCommit repo using the SSH Key ID provided to us through the IAM user setup.

So the command to add a remote repo should be:

git remote add name-for-remote ssh://ssh-key-id@git-codecommit.us-east-1.amazonaws.com/v1/repos/your-repo

Where ssh-key-id is the key ID provided to your user from the IAM window, name-for-remote is the name of the remote repo (the AWS documentation suggests origin, but if you’re like me and already have origin, name it to something like aws), and your-repo is the name of the CodeCommit repo you had setup on AWS.

Stay in touch

Thanks for reading this article. I'd love to stay in touch and share articles like this one in your inbox. Sign up for my newsletter.