Working with AWS CodeCommit on Ubuntu

AWS Codecommit is a Git-based repository to maintain source code offered by AWS.

I. Creating CodeCommit Repository

We can create a code repository on CodeCommit by going to AWS Services menu and selecting CodeCommit service. Click on “Create Repository” button. Enter the name of your repository and click on Create Repository button. See http://docs.aws.amazon.com/codecommit/latest/userguide/getting-started-cc.html for more detailed info.

II. Installing Git client in local system

Once created, you can access the repository from your Linux system. In this example, we give you and overview of accessing the repository from Ubuntu. We need Git software

sudo apt-get install git

After Git is installed, we need to create an SSH key on Ubuntu and add the public key back into the CodeCommit repository. This is required to give access to the users to pull and push the code into the repository.

III. Creating IAM user in AWS

The user has to be created in AWS IAM to access the code repository.

  • Go to AWS IAM
  • Click on “Add User”
  • Enter the username.
  • Select Access Type as “Programmatic Access”
  • Click Permissions button and give access to “AmazonRDSFullAccess, AWSCodeCommitPowerUser, AmazonElastiCacheFullAccess, AmazonS3FullAccess” policies
  • Click on Review button
  • Click on Create button

The next screen will show the Access key ID and Secret access key. Make a note of these.

The user will be created and shown in the IAM Users list. Click on the user name to check the details.

IV. Creating SSH keys for AWS CodeCommit

In your Linux Terminal give the below command

$ ssh-keygen

Select a file name and enter a passphrase. Below is an example

lightracers@lightracers-laptop:~$ ssh-keygen
 Generating public/private rsa key pair.
 Enter file in which to save the key (/home/lightracers/.ssh/id_rsa): /home/lightracers/.ssh/id_codecommit_rsa
 Created directory '/home/lightracers/.ssh'.
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /home/lightracers/.ssh/id_codecommit_rsa.
 Your public key has been saved in /home/lightracers/.ssh/id_codecommit_rsa.pub.
 The key fingerprint is:
 SHA256:lim0...................mKy7QnDPxR/2pELs lightracers@lightracers-laptop
 The key's randomart image is:
 +---[RSA 2048]----+
 |o.+oo. o |
 |*o .+ = |
 |=o+ = O . |
 |+ @ = + . |
 |o.O * X S o |
 | o + + B . |
 | . . E o |
 | . |
 | |
 +----[SHA256]-----+

Next step is to add this RSA public key to the IAM credentials tab. The RSA public key will be available with the extension of .pub for the rsa id file you had created earlier. For ex. /home/lightracers/.ssh/id_codecommit_rsa.pub

$ cat /home/lightracers/.ssh/id_codecommit_rsa.pub

The output will be key file..

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZ3nfWB+........O+ULf lightracers@lightracers-laptop

Go back to IAM User details screen. Under Security Credentials tab, click on “Upload SSH Public Key” button. Enter the public key copied in to the field and click on “Upload SSH Public Key” button. Once this, this will generate a SSH Key ID. Create a ssh config file if not yet created

$ nano ~/.ssh/config

Copy the SSH Key ID generated created and paste it in ~/.ssh/config file. The file content will be

Host git-codecommit.*.amazonaws.com
 User APKAXXXXXX
 IdentityFile ~/.ssh/id_rsa

Go back to terminal and give following commands

$ ssh -v git-codecommit.ap-south-1.amazonaws.com

If connected successfully, you will get the success message.

You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. You can refer to http://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-ssh-unixes.html for more details.

V. Creating the first branch on your repository

Go to your required folder and give the following command in your terminal

$ git clone ssh://git-codecommit.ap-south-1.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo

This will clone your repository.

VI. Useful Git commands

Below are the useful git commands

Adding files

$ git add .

Specific files

$ git add path/to/your/file.xyz

Commit command

$ git commit -m "commit message "

Git push command

$ git push origin master

Git pull command

$ git pull origin master

Reverting the changes

$ git stash