I was looking for AWS CodeCommit quick tutorial and ended up watching ACloudGuru’s “Manage and Deploy Code with AWS Developer Tools” 8 hour course (BTW, a great one!), reading some articles and AWS documentations. Based on that I’ve decided to create a quick guide to start using AWS CodeCommit via AWS CLI.
What is AWS CodeCommit?
Think of it as an AWS version of GitHub. In other words, AWS CodeCommit is a managed source control service that hosts private Git repositories. You can use CodeCommit to store and manage documents, source code, and binary files in the cloud.
CodeCommit is integrated with a number of AWS services such as CodeBuild, CodePipeline, Lambda, SNS, Amplify, CloudFormation, etc.
AWS CodeCommit vs GitHub
Both can be primarily classified as "Code Collaboration & Version Control" tools.
Similarities |
---|
Support code review |
Use two methods of authentication, SSH and HTTPS |
Use Git repositories |
Based on comparison provided by CompareCamp, here are some cons and pros of both services:
AWS CodeCommit Pros | AWS CodeCommit Cons |
---|---|
free private repos (for up to 5 users) | complex menu options |
faster deployments using AWS solutions | resubmit flow and code review unavailable |
encrypted repo data | limited triggers |
issue tracker | lack of CI system integrations |
On the other hand,
GitHub Pros | GitHub Cons |
---|---|
ease of use | notifications are hard to configure |
easy integration with third-party tools | lack of first-party support for mobile |
code highlighting for ObjectScript | lack of command-line configuration options |
comprehensive issue tracking | security scanning problems |
Prerequisites and setup (using HTTPS)
Of course, you can use AWS CodeCommit console with friendly UI to manage files and repositories directly. But in order to work with multiple files, files across branches, and also to control multiple AWS services from the command line and automate them through scripts consider setting up your local computer to work with AWS CodeCommit via AWS CLI.
Step 1. Install Git
Check installation by running the following command:
git --version
Step 2. Install AWS CLI
Check installation by running the following command:
aws --version
Step 3. Create Git credentials for IAM user
Firstly, create a new AWS IAM user and give it a Programmatic access
Then attach AWSCodeCommitFullAccess policy to the user.
Store a user's credentials in a safe place
Step 4. Configure AWS CLI
Use the following command and enter newly created Access key ID and Secret access key:
aws configure
AWS CLI will also ask you to enter the Region whose servers you want to send your requests to. By default it is typically the region closest to you, but you can enter any other region.
And finally, you might want to specify Output format how the results are formatted. json is used as the default, but you can use yaml, yaml-stream (streaming allows for faster handling of large data types), text, and table formats.
Alternatively, you can set up AWS CodeCommit using SSH connections for Windows and for Linux, macOS, or Unix.
Pricing
First 5 active users | Each additional active user beyond the first 5 |
---|---|
$0.00 | $1.00 per month |
Unlimited repositories | Unlimited repositories |
50 GB-month of storage | 10 GB-month of storage per active user |
10,000 Git requests/month | 2,000 Git requests/month per active user |
Check the latest prices here.
AWS CodeCommit commands
Here is a full list of AWS CodeCommit commands. In this article, we are going to understand how to use the top command. Just follow along the guide to get a basic experience with AWS CodeCommit.
- To create a new repository, run:
aws codecommit create-repository --repository-name MyDemoRepo --repository-description "This is a demo repo"
Output:
You should find a new repo on AWS CodeCommit Console:
- To get a list of all existing repositories, run:
aws codecommit list-repositories
- To get information about specific repository, run:
aws codecommit get-repository --repository-name MyDemoRepo
- To get information about multiple specific repositories, run:
aws codecommit batch-get-repositories --repository-names MyDemoRepo MyBestRepo
- To update repository’s name, run:
aws codecommit update-repository-name --old-name MyDemoRepo --new-name MyDemo2Repo
- To update repository’s description, run:
aws codecommit update-repository-description --repository-name MyDemo2Repo --repository-description "This is updated description"
- To clone repository, you need to get a url from AWS CodeCommit console
then run the following commands:
# navigate to your local directory
cd "C:\Users\{userName}\{folder}\{nested-folder}"
# # clone a repository from AWS CodeCommit to your local machine
git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemo2Repo localMyDemo2Repo
Important! If a Windows Security is popped up asking for username and password, then cancel it
In my case I still got the warning about an empty repository with a successfully copied repository on my local machine.
The issue has gone once I've uninstalled and reinstalled Git for Windows with cleared the check box for the option to install the Git Credential Manager utility (details) because the credential manager is not compatible with the credential helper for AWS CodeCommit.
If you got different kind of issues while attempting to clone a repository, see AWS's Troubleshooting section.
- To commit and push changes to AWS CodeCommit, run Git commands:
# navigate to your local repository
cd localMyDemo2Repo
# create a sample text file
echo "Greeting, AWS CodeCommit nerds! Let's rock it!" > hello.txt
# stage the file for commit to your local repository
git add .
# commit the file that you've staged
git commit -m"Add a new hello file"
# push the changes in your local repository to AWS CodeCommit
git push origin master
As a result, a new "hello.txt" file should be added to remote repository:
To manage branches, merges, tags, logs, etc, use Git commands. You can find the full list of Git commands here and one of my favorite Git cheat sheet here.
- To view information about current repository, run:
git remote show origin
Output:
Also, it you want to know how to migrating a repository from GitHub into AWS CodeCommit via AWS CLI, check this article.
Create AWS CodeCommit trigger to send notification via Amazon SNS
You can configure a AWS CodeCommit repository so that code pushes or other events trigger notification from Amazon SNS. Beware of limitations, as AWS allows up to 10 triggers for each CodeCommit repository.
Let's configure "MyDemo2Repo" repository in AWS CodeCommit so that any event triggers Amazon SNS to send a notification to email.
Step 1. Give an IAM user some permissions to manage Amazon SNS
An IAM user named 'code-commit-user' doesn't have enough permissions to manage an Amazon SNS. In addition to AWSCodeCommitFullAccess policy you might want to attach AmazonSNSFullAccess policy to that user:
Step 2. Create an SNS topic
To create a topic, use the aws sns create-topic command and specify the name to assign to the topic:
aws sns create-topic --name my-codecommit-trigger
You should get a TopicArn as a response - write it down for step 3:
Step 3. Subscribe to a topic
Create a subscription and subscribe to a topic specifying TopicArn:
aws sns subscribe --topic-arn arn:aws:sns:{your-region}:{your-accountId}:my-codecommit-trigger --protocol email --notification-endpoint {your-email@blah.blah}
Output:
AWS immediately sends a confirmation message by email to your address. You need to confirm the subscription.
Once confirmed, your browser should display a notification message with information similar to the following:
You can find a newly created topic on Amazon SNS Console:
Step 4. Create a JSON file with trigger description
Next step is to create a trigger for a CodeCommit repository so that events in that repository trigger notifications from SNS topic.
Firstly, create a JSON file that specifies SNS topic name, the repository and branches you want to monitor with this trigger, and the events that activate this trigger.
For example, to create a trigger for a repository named MyDemo2Repo that publishes all repository events to an Amazon SNS topic named my-codecommit-trigger for a master branch, save the following code in 'my-first-codecommit-trigger.json' file:
{
"repositoryName": "MyDemo2Repo",
"triggers": [
{
"name": "AllEventsTriggerForMyDemo2Repo",
"destinationArn": "arn:aws:sns:{your-region}:{your-accountId}:my-codecommit-trigger",
"customData": "",
"branches": [
"master"
],
"events": [
"all"
]
}
]
}
In our example we used all event type but here is the full list of event types you can create triggers for:
Event | Description |
---|---|
all | for all events in the specified repository and branches |
updateReference | for when commits are pushed to the specified repository and branches |
createReference | for when a new branch or tag is created in the specified repository |
deleteReference | for when a branch or tag is deleted in the specified repository |
Let's test our json file:
aws codecommit test-repository-triggers --cli-input-json "file://C:\Users\{your-folders}\my-first-codecommit-trigger.json"
Note, you have to use file:// before the json name.
If successful, this command returns information similar to the following:
Step 5. Create a trigger
Now it's time to create a trigger in AWS CodeCommit that is described in json file. The command is similar to the previous one but instead of 'test' use 'put':
aws codecommit put-repository-triggers --cli-input-json "file://C:\Users\{your-folders}\my-first-codecommit-trigger.json"
This command returns a configuration ID:
Check your email, you should get a very first notification from AWS:
Step 6. View trigger configuration
If you want to view the configuration of the trigger for target repository, run:
aws codecommit get-repository-triggers --repository-name MyDemo2Repo
Output:
Step 7. Make some repository changes
To test the functionality of the trigger itself, make and push a commit to the repository where you configured the trigger. You should see a response from the Amazon SNS topic.
# navigate to your local repository
cd localMyDemo2Repo
# create a text file
echo "This is a new file for MyDemo2Repo repository that should trigger a notification from AWS SNS topic" > test-sns.txt
# stage the file for commit to your local repository
git add .
# commit the file that you've staged
git commit -m"Add a new test-sns file"
# push the changes in your local repository to AWS CodeCommit
git push origin master
As a result, a new "test-sns.txt" file should be added to remote repository and a new notification from AWS should be sent to your email:
Clean up
To avoid ongoing charges for resources you created to complete this guide, let's delete our repository and sns topic:
# get the list of all subscriptions
aws sns list-subscriptions
# from the list above get a target SubscriptionArn and use it in command below to unsubscribe from a topic and stop receiving messages published to that topic
aws sns unsubscribe --subscription-arn {your-SubscriptionArn }
# get the list of all sns topics
aws sns list-topics
# from the list above get a target TopicArn and use it in command below to delete SNS topic
aws sns delete-topic --topic-arn {your-TopicArn}
# get the list of all repositories
aws codecommit list-repositories
# from the list above get a target repositoryName and use it in command below to delete repository
aws codecommit delete-repository --repository-name MyDemo2Repo
All done! Till the next time.
Top comments (0)