How To Access An AWS CodeCommit Repository Remotely
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to create an AWS CodeCommit repository using AWS CLI.
https://cloudaffaire.com/how-to-create-an-aws-codecommit-repository/
In this blog post, we will discuss on how to access an AWS CodeCommit repository remotely. There are several ways you can configure access to your CodeCommit repository. You can simply access and manage your CodeCommit repository from the AWS directly. If you intend to manage your CodeCommit repository from your local system then you can configure either SSH access or HTTP access. In this blog post, we will cover both SSH and HTTPS option.
Note: There are some additional methods of accessing your CodeCommit repository like HTTP with git-remote-codecommit which is required when you are using any identity provider or temporary credentials, but form simplicity we will cover the most common HTTPS and SSH methods.
How To Access An AWS CodeCommit Repository Remotely:
Requirements:
AWS CLI installed and configured. You can follow the below blog post to install and configure AWS CLI.
https://cloudaffaire.com/how-to-install-aws-cli/
https://cloudaffaire.com/how-to-configure-aws-cli/
Git installed and configured.
https://cloudaffaire.com/category/devops/git/
How To Configure HTTPS Connection To AWS CodeCommit Repository:
Step 1: (Optional) Create an IAM user to access CodeCommit.
If you have followed the above blog post to configure your AWS CLI then you already have full access and can skip this step. Else create a new IAM user with programmatic access and attach AWSCodeCommitPowerUser IAM policy to the user.
Once the user created, copy the IAM user Access key and Secret key and configure AWS CLI using the same set of keys.
1 2 3 4 5 6 7 8 9 10 11 |
## Open command prompt or shell and execute aws configure command ## Provide IAM myuser user secret key and access key when prompted aws configure --profile myuser #AWS Access Key ID [None]: #AWS Secret Access Key [None]: #Default region name [None]: #Default output format [None]: ## check if access is working aws sts get-caller-identity --profile myuser |
Step 2: Install Git in your local system.
https://www.atlassian.com/git/tutorials/install-git
Step 3: Create Git credentials for the IAM user to connect to CodeCommit.
Login to AWS console and navigate to IAM
Select the IAM user and navigate to the ‘Security Credentials’ tab and click on ‘Generate Credentials’
Copy the username and password, we will need that in the next step. You can also download the credential into your local system by clicking on the download link.
Step 4: Get your CodeCommit repository HTTPS clone URL.
Login to the AWS console and navigate to CodeCommit.
Select your repository and click ‘HTTPS’ as shown below figure. The URL will be copied to your clipboard, save it for the next step.
Or, if you are following my previous blog post.
1 2 3 4 5 6 |
## get your codecommit http clone url GIT_CLONE_HTTP_URL=$(aws codecommit get-repository \ --repository-name "my_aws_repo" \ --query 'repositoryMetadata.cloneUrlHttp' \ --output text) && echo $GIT_CLONE_HTTP_URL |
Step 5: Configure Git credentials and clone the CodeCommit repository in your local system.
1 2 3 4 5 6 |
## Open a command prompt (windows) or shell (Linux) and execute the below command. ## replace the git clone #or, if you are following previous blog git clone $GIT_CLONE_HTTP_URL |
Provide the git username and password (retrieved in step 3) when prompted. If everything worked fine then a new directory will be created which will serve as your local repository for your AWS CodeCommit remote repository.
Step 6: (Optional) Configure git username and email.
1 2 3 4 5 6 7 8 9 10 11 12 |
## navigate to your local repository cd #or, those who are following my previous blog post cd my_aws_repo ## check git status git status ## configure git username and email git config --global user.name "Debjeet" git config --global user.email "cloudaffaire@gmail.com" |
Now you are ready to perform any git operation on your AWS CodeCommit repository using your local system. Next, we are going to perform the same setup using SSH.
How To Configure SSH Connection To AWS CodeCommit Repository:
Step 1: (Optional) Create an IAM user to access CodeCommit.
If you have followed the above blog post to configure your AWS CLI then you already have full access and can skip this step. Else create a new IAM user with programmatic access and attach AWSCodeCommitPowerUser IAM policy to the user.
Once the user created, copy the IAM user Access key and Secret key and configure AWS CLI using the same set of keys.
1 2 3 4 5 6 7 8 9 10 11 |
## Open command prompt or shell and execute aws configure command ## Provide IAM myuser user secret key and access key when prompted aws configure --profile myuser #AWS Access Key ID [None]: #AWS Secret Access Key [None]: #Default region name [None]: #Default output format [None]: ## check if access is working aws sts get-caller-identity --profile myuser |
Step 2: Install Git in your local system.
https://www.atlassian.com/git/tutorials/install-git
Step 3: Create your SSH keys in your local system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
########## ## Windows ########## ## Open the Bash emulator (you will get a bash as part of git installation) ## generate your ssh keys ssh-keygen ## Generating public/private rsa key pair. ## Enter file in which to save the key (/drive/Users/user-name/.ssh/id_rsa): Type a file name here, ## for example /c/Users/ ## ## Enter passphrase (empty for no passphrase): ## Enter same passphrase again: ## ## Your identification has been saved in drive/Users/user-name/.ssh/codecommit_rsa. ## Your public key has been saved in drive/Users/user-name/.ssh/codecommit_rsa.pub. ## get the content of your public key cd /c/Users/ notepad codecommit_rsa.pub ######## ## Linux ######## ## Open your terminal ## generate your ssh keys ssh-keygen ## Generating public/private rsa key pair. ## Enter file in which to save the key (/home/user-name/.ssh/id_rsa): Type /home/ ## for example /home/ ## ## Enter passphrase (empty for no passphrase): ## Enter same passphrase again: ## ## Your identification has been saved in /home/user-name/.ssh/codecommit_rsa. ## Your public key has been saved in /home/user-name/.ssh/codecommit_rsa.pub. ## get the content of your public key cd /home/ cat codecommit_rsa.pub |
Copy the key which will be required in the next step.
Step 4: Configure SSH key for IAM user.
Login to AWS console and navigate to IAM
Select the IAM user and navigate to the ‘Security Credentials’ tab and click on ‘Upload SSH public key’. Paste the public key (retrieved in the previous step) and click ‘Upload SSH public key’
Step 5: Copy the key id once uploaded.
Step 6: Set your local SSH configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
########## ## Windows ########## ## open your ssh config file notepad ~/.ssh/config ## paste below content in your file ## replace Host git-codecommit.*.amazonaws.com User IdentityFile ~/.ssh/codecommit_rsa ## close and save the file ## make sure the file name is config and not config.txt ## rename the file if required, else ssh will not work ## test your ssh connection ssh git-codecommit.ap-south-1.amazonaws.com ######## ## Linux ######## ## create a ssh config file ## replace cat < Host git-codecommit.*.amazonaws.com User IdentityFile ~/.ssh/codecommit_rsa EOF ## change the config file permission chmod 600 ~/.ssh/config ## test your ssh connection ssh git-codecommit.ap-south-1.amazonaws.com |
Step 7: Get your CodeCommit repository HTTPS clone URL.
Login to the AWS console and navigate to CodeCommit.
Select your repository and click ‘SSH’ as shown in the below figure. The URL will be copied to your clipboard, save it for the next step.
Or, if you are following my previous blog post.
1 2 3 4 5 6 |
## get your codecommit ssh clone url GIT_CLONE_SSH_URL=$(aws codecommit get-repository \ --repository-name "my_aws_repo" \ --query 'repositoryMetadata.cloneUrlSsh' \ --output text) && echo $GIT_CLONE_SSH_URL |
Step 5: Configure Git credentials and clone CodeCommit repository in your local system.
1 2 3 4 5 6 |
## Open a command prompt (windows) or shell (Linux) and execute the below command. ## replace the git clone #or, if you are following the previous blog git clone $GIT_CLONE_SSH_URL |
Note: If you get an error, you can try with the Key-Id that you got in step 5.
git clone ssh://<SSH_KEY_ID>@git-codecommit.<AWS_REGION>.amazonaws.com/v1/repos/<REPOSITORY_NAME>
Step 6: (Optional) Configure git username and email.
1 2 3 4 5 6 7 8 9 10 11 12 |
## navigate to your local repository cd #or, those who are following my previous blog post cd my_aws_repo ## check git status git status ## configure git username and email git config --global user.name "Debjeet" git config --global user.email "cloudaffaire@gmail.com" |
Now you are ready to perform any git operation on your AWS CodeCommit repository using your local system.
Hope you have enjoyed this blog post. To get more details on AWS CodeCommit, please refer below AWS documentation
https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html
CodeCommit is a Git-based version control system and you need to have a basic understanding of Git in order to work with CodeCommit. You can follow the below link to get a basic understanding of Git.
https://cloudaffaire.com/category/devops/git/
From the command prompt or terminal, switch to your local repo directory and run the git remote add command to add the CodeCommit repository as a remote repository for your local repo.