AWS CodeCommit
A quick guide to using AWS CodeCommit using the AWS CLI.
Getting help
Enough for getting around!
Generate SSH credentials
Required for cloning a repository:
ssh-keygen -f ~/.ssh/codecommit_rsa
Then, set this up with CodeCommit:
Sign to the AWS Console.
Open your IAM Users page, and select the
user you wish to give CodeCommit access to with this SSH key (or create a new one and use it for
the rest of the steps).
Click Add permissions, then Attach existing policies directly.
Use the Filter policies search box to search for "codecommit". Choose the checkbox
near either AWSCodeCommitFullAccess (to allow repository deletion) or AWSCodeCommitPowerUser
to allow write permission.
Click Next: Review, then Add permissions.
Switch to the Security Credentials tab for your IAM user, and click the
Upload SSH public key button.
Copy the contents of ~/.ssh/codecommit_rsa.pub
and paste it into the Upload
SSH public key dialog.
Create (or edit) ~/.ssh/config
, and add the following contents:
Host git-codecommit.*.amazonaws.com
User REPLACEME
IdentityFile ~/.ssh/codecommit_rsa
PubkeyAcceptedKeyTypes +ssh-rsa
You'll fill in that sample REPLACEME
value next...
Note
That last setting, PubkeyAcceptedKeyTypes
, was required on my Fedora installation
because the ssh-rsa algorithm used by CodeCommit has been deprecated.
Back in the AWS console, copy the SSH key ID from your IAM user's Summary page, and
paste it in place of REPLACEME
in the ~/.ssh/config
file.
Save and close the file.
Go to your command-line, and change ~/.ssh/config
's permissions to 600
(AKA
-rw-------
):
You should now be able to use the SSH key to access your CodeCommit repositories!
- For more info
-
Creating a repository
aws codecommit create-repository --repository-name repositoryName
Creates a repository using repositoryName
as the repository name, and prints its info (the
same info you get when running aws codecommit get-repository).
After creating a repository, you can clone it to add files to your
new, blank repository.
Listing your repositories
aws codecommit list-repositories
Prints a listing of the repositories for your account. For each, it reports the repositoryName
and
repositoryId
.
Getting info about a repository
aws codecommit get-repository --repository-name repositoryName
Lists the metadata associated with the repository, including:
accountId
- The ID of your AWS account.
repositoryId
- A GUID that represents your repository.
repositoryName
- The "friendly" name of the repository.
repositoryDescription
- A description of the repository. You can change this by using the
aws codecommit update-repository-descripotion
command.
defaultBranch
- The branch that will be cloned when one is not specified. You can change this
by using the aws codecommit update-default-branch
command.
lastModifieddate
- The last modified date of the repository.
creationDate
- The creation date of the repository
cloneUrlHttps
- The URL to use when cloning the repository using HTTPS
cloneUrlSsh
- The URL to use when cloning the repository with SSH
Arn
- The AWS Resource Name (ARN) for the repository.
The cloneUrlSsh
field, in particular, is important when you want to clone the repository in order to work with its files.
Cloning a repository
Once you've set up your SSH credentials correctly, you can clone the
repository by:
Using the aws codecommit get-repository command to get the
cloneUrlSsh
value for cloning the repository.
Using the cloneUrlSsh
value to clone your repository like any other git
repository:
git clone cloneUrlSsh
Once you've cloned the repository, you can use normal git commands to work with your
local copy, or git pull and git push to interact with your codecommit
repository.
Migrating your existing git repository to CodeCommit
Here's an example that puts it all together. ;)
Say that I have a repository (repositoryName
) hosted at another server and I want to move it to
CodeCommit, without losing any of my history. I can do it all with an AWS CLI command and a few
git commands:
Clone the repository I want to move:
git clone ssh://old-server/repositoryName.git
Enter the cloned repository directory:
cd repositoryName
Create the new repository on CodeCommit:
aws codecommmit create-repository --repository-name repositoryName
This prints information about your repository. For example:
{
"repositoryMetadata": {
"accountId": "AWSACCOUNTID"
,
"repositoryId": "0d1800c6-some-long-guid-9a667b8da8fb"
,
"repositoryName": "repositoryName"
,
"lastModifiedDate": timestamp
,
"creationDate": timestamp
,
"cloneUrlHttp": "https://git-codecommit.region.amazonaws.com/v1/repos/repositoryName"
,
"cloneUrlSsh": "ssh://git-codecommit.region.amazonaws.com/v1/repos/repositoryName"
,
"Arn": "arn:aws:codecommit:region:AWSACCOUNTID:repositoryName"
}
}
Create a new git remote, using the value of cloneUrlSsh:
git remote add codecommit ssh://git-codecommit.region.amazonaws.com/v1/repos/repositoryName
Now, push your repository to the new remote:
git push codecommit master
Remove the old origin remote:
Rename your codecommit remote as origin:
git remote rename codecommit origin
That's it...!