Exploring AWS CodeCommit
Exploring AWS CodeCommit
AWS CodeCommit is a highly available, durable, and fully managed source code
service. It is a Git-based source code repository. At the time of writing, AWS
CodeCommit provides five free active users in a free tier. It can be used for various
purposes, such as documents, source codes, and binary files. After the free tier, AWS
charges $1 per active user. CodeCommit is also compliant with Health Insurance
Portability and Accountability Act (HIPAA) and Payment Card Industry Data
Security Standard (PCI DSS).
The aim of this chapter is to introduce you to AWS CodeCommit. The chapter covers
the following topics with relevant reference URLs for further reading, where
required:
[2]
Exploring AWS CodeCommit Chapter 1
[3]
Exploring AWS CodeCommit Chapter 1
$ git clone
https://git-codecommit.us-east-1.amazonaws.com/v1/repo
s/LearnCodeCommit
Before executing Git commands, make sure the Git CLI is installed
and configured on the developer's local machine.
In the Creating a repository section, we explained how to obtain an
HTTPS/SSH link to clone the remote repository to the local machine.
It will prompt you for a username and password. AWS
CodeCommit doesn't allow you to use your defined username and
password. Your username and password can be obtained for a
specific AWS IAM user from HTTPS Git credentials for AWS
CodeCommit inside Security credentials. More details about the
credentials are given in the subsequent section.
[4]
Exploring AWS CodeCommit Chapter 1
3. Once the developer has cloned a local copy for the remote repository, they
can start contributing their work (usually in the form of programming:
source code) or any binary files into the local repository. Storing a source
code or a file on a remote source code repository is a three-step process:
$ touch login.py
The git status command will list all the files, whether they
are newly created or modified existing ones. Based on the list,
the appropriate decision can be made to add files to the
staging for committing to the local repository. Therefore, we
use git add to add files to the local repository:
$ git add login.py
[5]
Exploring AWS CodeCommit Chapter 1
Now the git status command lists the newly created file,
which is staged and ready to commit in the local repository:
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: login.py
#
The preceding git add command will not add the newly
created file to the local repository, but it will prepare a file or
set of files to commit to the local repository when a
subsequent git commit command is executed. There may
be a situation where the developer has changed or created
many source code files for a given task or project on their
local machine. Out of these files, they just want to store (that
is, commit) a few selected and relevant source code or files to
the repository. This way, they have the liberty to select which
files they would like to prepare for committing by staging
them using the git add command.
These IDEs can directly add new files to the local repository
and commit to the configured remote repository.
[6]
Exploring AWS CodeCommit Chapter 1
2. Once recently added or modified files are staged, they are ready
to be stored (that is, committed) in a local repository, which can
be achieved using the git commit command. This command
will store staged files in a local repository, as in the following
example:
$ git status
# On branch master
nothing to commit, working directory clean
The Git repository stores the snapshot of the files (that is,
original files + modification).
[7]
Exploring AWS CodeCommit Chapter 1
When your local code repository is out of sync (behind) with the
remote source code repository (usually called the upstream
repository), you will get an error message saying, Note about fast-
forwards.
The git push command takes two arguments: origin and branch
name. Origin indicates the remote repository, which is from where
the local repository was cloned. In this case, the branch name is
master. The branch name depends on the bug fix, the feature, or
the part of the software development project you are working on.
By default, the AWS CodeCommit repository is blank and without
any branches. Once a file has been stored automatically, it creates
a master branch, and that is the default branch. Further down the
chapter, the Working with branches section describes source code
branching strategies at a high level.
[8]
Exploring AWS CodeCommit Chapter 1
Figure 20.1 and the following points explain various stages involved in a
developer's day-to-day activities during a software development lifecycle
when working with the source code repository. It is possible to carry out
these steps using an AWS CodeCommit web console. While CodeCommit
web console may be easy and quick for working with a couple of files, it is
not easy to handle more files using web console. For working with a large
number of files and doing constant development, it is recommended that
you use CLI or IDE integration with CodeCommit.
To work with remote the AWS CodeCommit source code repository using the CLI,
you need to configure a local Git client to communicate with it. This configuration
requires authentication to be carried out. AWS IAM provides three types of
credentials:
[9]
Exploring AWS CodeCommit Chapter 1
The HTTPS connection for the Git CLI to use AWS CodeCommit can be made
through the following steps.
1. Log into the AWS account and open the IAM web console.
2. Create a new user or use an existing IAM user. Make sure IAM users have
access and a secret key.
3. Once the user is created or an existing user is selected, go to the
Permissions tab and choose Add permissions.
In order for an IAM user to work with AWS CodeCommit, the user
needs access to the AWS Key Management Service (KMS). If you're
using an existing user, please make sure any policy attached doesn't
have exclusive deny rules for the AWS KMS service.
[ 10 ]
Exploring AWS CodeCommit Chapter 1
You can refer to following URLs for downloading and installing Git for your
operating system:
Linux: https://git-scm.com/download/linux
Windows: https://git-scm.com/download/win
Mac OS: https://git-scm.com/download/mac
For a better user experience with the Git CLI and the remote source
code repository, make sure basic Git configuration such as username
and email is done on the developer's local machine.
1. Log into the AWS account and open the IAM web console.
2. Select the IAM user that was created in the step 1 – creating the IAM user
section.
[ 11 ]
Exploring AWS CodeCommit Chapter 1
3. Choose the Security credentials tab, and select Generate under HTTPS Git
credentials for AWS CodeCommit:
[ 12 ]
Exploring AWS CodeCommit Chapter 1
These recently created usernames and passwords are required for each HTTPS
connection with the AWS CodeCommit repository.
Creating a repository
Once the IAM user is configured with SSH keys or HTTPS Git credentials for AWS
CodeCommit, the user gets the required privileges to interact with the remote source
code repository. It is essential to create an IAM user for accessing the repository. The
repository is a secured and reliable place to store code and files for the project. It
stores the history of each commit, and can also sends notifications to the repository
users to receive an email when any new commit has occurred.
To create a AWS CodeCommit repository using a web console, execute the following
steps:
1. Log into the AWS account as a user that has sufficient privileges to work
with AWS CodeCommit.
2. Select the appropriate AWS region from the top-right side of the AWS
dashboard.
[ 13 ]
Exploring AWS CodeCommit Chapter 1
Figure 20.3
Figure 20.4
[ 14 ]
Exploring AWS CodeCommit Chapter 1
Figure 20.5
The AWS CodeCommit repository can also be created using the AWS CLI for
CodeCommit by executing the following command:
aws codecommit create-repository --repository-name LearnCodeCommit --
repository-description " Repository to learn AWS CodeCommit"
Before executing the preceding command, make sure the aws configure command
is executed with access, the secret key, and the region.
[ 15 ]
Exploring AWS CodeCommit Chapter 1
Now that the repository is created, it is time to connect it with the remote repository.
A repository can be remotely connected over HTTPS or SSH protocol. It requires a
URL to do so. The URL can be obtained by selecting the desired repository to connect
to and clicking on HTTPS or SSH:
Figure 20.6
Other developers in a team can access the same AWS CodeCommit repository by
using the HTTPS/SSH protocol URL. This URL is also called a clone URL for the
remote source code repository. Make sure developers have sufficient privileges to
access the repository by creating an IAM user with appropriate minimum privileges
to fulfill their day-to-day tasks.
CodeCommit events
When a computer program or a service detects the occurrence of an action, it is called
an event. Event-driven programming is a mechanism that determines the flow of the
program based on the occurrence of events. CodeCommit supports event-driven
programming. We will see some events and their details in this topic.
[ 16 ]
Exploring AWS CodeCommit Chapter 1
When a CodeCommit source code repository is ready with its credentials, each of the
developers can access the repository to store project code and files. Developers can
also configure an event notification and define their development, testing, or
deployment workflow based on the events. Some of the notification events are
described in the following points:
Pull request update events: When you enable this event, subscribers
receive an email notification in either of the following scenarios:
When a pull request is created or closed
When a pull request is updated with code changes
When a title or description of the pull request changes
Pull request comment events: When you enable this event, subscribers
receive an email notification when somebody comments or replies to a
comment in a pull request.
Commit comment events: When you enable this event, subscribers receive
an email notification when somebody comments on a commit outside of a
pull request. It includes comments on the following:
Line of code in a commit
Files in a commit
The commit itself
Enabling such events is helpful to update each of the developers in a team about the
latest events, and accordingly they can perform their consecutive roles and tasks.
Events can be enabled by executing the following steps:
1. Log into the AWS account and open the AWS CodeCommit web console.
2. Click on the desired repository:
Figure 20.7
[ 17 ]
Exploring AWS CodeCommit Chapter 1
Figure 20.8
Figure 20.9
[ 18 ]
Exploring AWS CodeCommit Chapter 1
Figure 20.10
6. Select the desired existing SNS topic. If a suitable SNS topic doesn't exist, it
is also possible to create one. Enable the desired events:
Figure 20.11
[ 19 ]
Exploring AWS CodeCommit Chapter 1
Once event notifications are enabled, use the Enable notifications toggle
button to disable events temporarily if required. At present, to delete the
events notification, you can directly delete the SNS topic from AWS SNS
web console.
7. Click on Save to complete enabling events for the desired remote AWS
CodeCommit source code repository. Once the events are successfully
enabled, the list of Subscribers will be displayed:
Figure 20.12
[ 20 ]
Exploring AWS CodeCommit Chapter 1
While the development team works on new versions, features, or bug fixes, it may
break. It is recommended that you keep each of the features, versions, and bug fixes
independent and separate. Every team contributes individually contributing to their
designated branch in the code repository. With a distributed version-control system,
it becomes easy for developers to push or pull their code to keep their development in
line with the overall development of the project.
Git branches are simple pointers or references to a commit. These branches make it
possible for various developer teams to work in parallel on individual development
tasks. Once they are done with their part, they can merge into the production branch.
The Git repository branching strategy may vary from project to project and
organization to organization. It is beautifully explained by Vincent Driessen on his
blog: https://nvie.com/posts/a-successful-git-branching-model/.
Once you have the remote repository cloned to a local machine, there may be a
situation where you need to switch between various branches either to contribute
your code or to refer to existing code.
To get a list of existing branches, we can use the git branch command:
$ git branch
* master
The * in the output indicates the present active branch. Logically, git is pointing to
the master and all the file operations will be carried out against the same branch.
The preceding command only shows the branches in a local repository. To get the
branch list of all the repositories (local and remote), we can run the git branch --
all command:
To get the details only about remote branches, we can use the git branch –r
command:
$ git branch -r
origin/HEAD -> origin/master
origin/master
[ 21 ]
Exploring AWS CodeCommit Chapter 1
To create a new branch while keeping logical control over the present branch, use
the git branch command:
$ git branch feature-X # create a new branch named feature-X
$ git branch # list branches in a current local repo
feature-X
* master
You can check out your code repository using the git checkout command with
the –b parameter. The syntax is as follows:
$ git checkout -b feature-Y
Switched to a new branch 'feature-Y'
The preceding command clearly indicates that logical control has been switched to a
newly created branch, feature-Y. The –b parameter in the preceding command is
used to create a branch with the given name; in the previous example the branch
name is given as feature-Y. By running the git status command, we can observe
that * has been shifted from master to feature-Y. This indicates that the current
branch is feature-Y:
$ git branch
feature-X
* feature-Y
master
In general, the git checkout command can be used to switch from one branch to
another or to bring a file to one of its earlier states during a software development on
a local repository. To delete an existing branch from a local repository, we can run the
following command:
$ git branch -d feature-Y
output: Deleted branch feature-Y (was 394963d).
The output indicates that the feature-Y branch with the 394963d commit reference
is deleted.
[ 22 ]
Exploring AWS CodeCommit Chapter 1
When you are merging a child branch with a parent branch after a
long time, it is suggested that you pull the latest code on the master
branch from the remote repository.
The git merge command can be used to merge the two branches (that is, the
specified branch with the current branch).
Before executing the git merge command, make sure you are on
the right branch and that HEAD is pointing to the correct merge-
receiving branch. By executing a git status command, we can
verify whether HEAD is pointing to the correct merge-branch. If
required, use the git checkout command to switch the branch.
[ 23 ]
Exploring AWS CodeCommit Chapter 1
Ideally, it is recommended you merge branches many times during a day to keep a
minimum deviation of code between the two branches. This command will combine
multiple sequences of commits into one unified history. Usually, git merge will take
two commit pointers at the branch tip. In our example, it is the Master tip and
Feature X, as shown in the following diagram. It will find a common base commit
between them:
Figure 20.12
Once the Common base has found commit phase, the Common base will create a
new merge commit to combine the changes in each queued merge commit sequence,
as shown in the following diagram:
Figure 20.13
[ 24 ]
Exploring AWS CodeCommit Chapter 1
The preceding command's output clearly indicates a merge took place using the
recursive method. The git merge command will automatically select a merge
strategy unless it is explicitly specified using the –s parameter. The strategy types are
recursive, resolve, octopus, ours, and subtree.
Merge commits are different from other commits. Since a merge commit merges two
branches, it has two parent commits. When you create a merge commit, Git
automatically tries to merge the histories of the commits you want to merge. If Git
encounters some data that was changed in the history of both branches, it cannot
combine this data in the merge. This scenario is called version-control conflict. Git
needs manual intervention in order to continue merging the commits.
Summary
AWS CodeCommit is highly available, durable, and a fully-managed
source code service.
CodeCommit is compliant with regulatory compliance standards such as
HIPAA and PCI DSS.
AWS CodeCommit can be accessed using a web console and also using the
Git command line or AWS CLI.
Developers can sync up their local repository with the remote repository
using git pull command.
Executing Git commands require authentication. Whenever you use Git
commands, Git prompts you for your username and password.
When working with a large number of files and doing constant
development, it is recommended that you use CLI or IDE integration with
CodeCommit.
To work with remote the AWS CodeCommit source code repository using
the CLI, you need to configure a local Git client to communicate with it.
AWS IAM provides three types of credential: IAM-generated username
and password, SSH key and AWS Access key ID and secret key.
Git credentials can be downloaded only during the creation of the
credentials. You cannot download them later on. If credentials are lost, you
need to reset the password. To avoid this situation, make sure the
downloaded credentials are stored in a safe place.
[ 25 ]
Exploring AWS CodeCommit Chapter 1
The repository is a secure and reliable place to store code and files for the
project.
Developers can also configure an event notification and define their
development, testing, or deployment workflow based on the events.
When a computer program or a service detects occurrence of an action, it is
called an event.
Event-driven programming is a mechanism that determines the flow of the
program based on the occurrence of events.
A repository can be divided into multiple branches depending upon
organizational requirement, for instance, feature, dev, QA, staging,
production branch, and so on.
The Git repository branching strategy may vary from project to project and
from organization to organization.
To get a list of existing branches, we can use the git branch command.
The git checkout command can be used to switch from one branch to
another or to bring a file to one of its earlier states.
[ 26 ]