AWS CodeCommit: Authenticating to a repository (HTTPS)

AWS CodeCommit: Authenticating to a repository (HTTPS)

We know that CodeCommit hosts private Git repositories. So, you need to authenticate or connect to the repository before you push, pull, or fetch the code. No one can connect to your repository except yourself, the repository owner, or someone with credentials.

We can connect to a repository in multiple ways. We created a repository in our previous post. To push the code, we need to clone the repository to our local machine by authenticating it. In this post, we will see how we can connect to a repository using Git credentials (HTTPS) for CodeCommit.

HTTPS Git Credentials:

This is the simplest way to set up connections to the repositories. You configure Git credentials in the IAM console and use them to connect to the repository.

The prerequisites to this method are,

  • IAM user with access granted to the repository. The user should have the AWSCodeCommitPowerUser managed policy or a custom policy that allows specific CodeCommit actions per the requirement.

  • Git client installed on your local system.

Once the requirements are in place, HTTPS Git credentials for CodeCommit needs to be created.

  • Go to AWS IAM console > Users.

  • Choose the IAM user you want, and go to the security credentials tab.

  • Scroll down to the “HTTPS Git credentials for CodeCommit” section. Click on generate credentials.

  • IAM creates the credentials, and you can download them. A CSV file will be downloaded to your downloads directory.

  • Now, let’s create a directory on our local system and switch to it.
mkdir my-first-application
cd my-first-application

  • Copy the HTTPS clone URL from the CodeCommit repository.

git clone {paste_the https_clone_url_thats copied_in_previous_step}
  • Input the username and password that we downloaded above.

The repository is empty. So, the git clone warns that you have cloned an empty repository. A new directory is created within the my-first-application directory with the same name as the CodeCommit repository.

The authentication is successful, and the remote CodeCommit repository is copied to our local. You can create new files and push them back to the CodeCommit repository.

  • Switch to the DemoRepo directory, create a new file, and add some content.
cd DemoRepo
touch sample-file-https
echo "This is a sample file pushed using HTTPS Git credentials for CodeCommit" > sample-file-https
cat sample-file-https

  • Check the git status which shows an untracked file in the working directory.
git status

  • Let Git track the file by adding it to the Git staging area.
git add .
git status

  • Now, commit it to the local repository and then push it to the remote repository. Key in the credentials if asked.
git commit -m "your message"

git push

  • Navigate the remote repository via the AWS CodeCommit repository console and refresh the page, you should see the file.

We have successfully connected to the repository, cloned it, and pushed files to it.

Happy Learning!