How to create and deploy repositories with AWS CodeCommit

AWS CodeCommit is a service for source code management (SCM)Source code management is the practice of tracking all changes to a source code repository across a period of time, as well as resolving any conflicts that emerge from multiple developers merging their source code. Source code simply refers to the collection of files and directories that make up the codebase of a software application. and version control systems (VCS)Version control systems, also called source control systems, are essentially software tools developers use to track and manage periodic changes to the code of a software application. . It is a highly scalable and secure service where users don’t need to worry about managing their source control system. Whenever we need to host private Git repositories, CodeCommit is one of the best choices.

Let’s learn how to create a code repository on CodeCommit and deploy some code to it.

Prerequisites

To progress with this Answer, we must have a working AWS account with the required CodeCommit permissions and also have our AWS access keys generated for that account.

Creating a CodeCommit repository

We can use the AWS CLI command create-repository, pass the command-line option repository-name, and create a repository of our choice with the name stored in the repository_name environment variable.

aws codecommit create-repository --repository-name $repository_name

Let’s execute the command above in the terminal below to create a new CodeCommit repository on the AWS Cloud. Make sure to add our AWS account’s access keys to the “aws_access_key_id” and “aws_secret_access_key” prompts. For the “repository-name” prompt, enter a CodeCommit repository name.

Terminal 1
Terminal
Loading...

Note: In the terminal above, we first configure the AWS CLI using the aws configure command before we can create a CodeCommit repository using the AWS CLI.

Upon the successful creation of the CodeCommit repository, we’ll get a response similar to this output:

{
"repositoryMetadata": {
"accountId": "536317743507",
"repositoryId": "6188b5d0-5c09-47f6-a3d2-94cb797cd0c9",
"repositoryName": "answers",
"lastModifiedDate": "2023-10-31T11:19:04.496000+00:00",
"creationDate": "2023-10-31T11:19:04.496000+00:00",
"cloneUrlHttp": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/answers",
"cloneUrlSsh": "ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/answers",
"Arn": "arn:aws:codecommit:us-east-1:536317743507:answers"
}
}
The CodeCommit repository

In the output above, we have created the CodeCommit repository named answers.

We can’t use a CodeCommit repository name that already exists on our AWS Cloud.

Deploying code to CodeCommit repository

Now that we’ve successfully created a CodeCommit repository let’s add some code to it using Git commands. We’ll be using the following commands to commit a web-app folder to the CodeCommit repository.

1) Configure the AWS CLI

We’ll use the following aws configure command to configure the AWS CLI in our environment with the AWS access keys and appropriate region.

aws configure set aws_access_key_id $aws_access_key_id && aws configure set aws_secret_access_key $aws_secret_access_key && aws configure set default.region $aws_region
Command to configure the AWS CLI

2) Configure the AWS access keys with Git

We use the git config --global credential.helper command to connect our AWS CodeCommit CLI as a credentials helper with Git. This way, we can use our AWS access keys from the aws configure command to gain access to the CodeCommit repository through Git.

Executing the git config --global credential.UseHttpPath command after specifying the CodeCommit credential helper ensures that once credentials are provided for a specific repository path, they are reused for subsequent interactions with that path, thereby reducing the need to reenter credentials frequently. This allows us to avoid repeatedly reentering the AWS CodeCommit credentials for Git.

We use the git config --global user.email command to configure the Git user email as example@email.com.

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
git config --global user.email example@email.com
Command to configure Git

3) Clone CodeCommit repository

We’ll use the following Git command to clone our CodeCommit repository into the environment. Note that the URL is the same as the one stored in the cloneUrlHttp parameter we got in response to the aws codecommit create-repository command, where $repository_name represents the repository name we set.

git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/$repository_name
Command to clone the CodeCommit repository

4) Add files to the cloned repository

We use the cp command below to copy the web-app folder into the cloned repository’s directory from the usercode directory. The usercode directory is where all the files from the code widget are located.

The second cd command is used to open up the cloned repository’s directory.

cp -TR /usercode/web-app/ /$repository_name/web-app/
cd $repository_name
Command to locally add files to the clonned repository folder

Note: The -T flag with the cp command is used to treat the destination, the web-app folder in the cloned repository’s directory, as a normal folder and replace it entirely with the source, the web-app folder in the usercode directory.

The -R flag with the cp command is used for recursively copying directories, subdirectories, and files from the source folder into the destination folder.

We’ll use the following Git command to add the web-app folder to the commit.

git add web-app
Command to locally add the web-app folder to the commit

5) Create commit

We’ll use the following Git command to create a commit with the web-app folder.

git commit -m "Added web-app folder with node project"
Command to create the commit

6) Push commit to CodeCommit repository

We’ll use the following Git command to push our commit and, hence, any changes in the code repository, which is the addition of the web-app folder to the master branch on our CodeCommit repository on the AWS Cloud.

git push -u origin master
Command to push the commit to CodeCommit

Code example

Let’s commit the web-app folder in the widget below that contains Node.js project to the CodeCommit repository we previously created. To do so, simply click the “Run” button in the widget below, which will clone the repository and commit the files present in the widget to the repository on the AWS CodeCommit.

Note: Make sure to add the AWS account’s access keys to the “aws_access_key_id” and “aws_secret_access_key” prompts. For the “repository-name” prompt, enter the CodeCommit repository name.

Also, ensure that we’ve already created our CodeCommit repository.

<!DOCTYPE html>
<html>

    <body>
        <h1>Hello World from Educative</h1>
        <p>This is version 1</p>
    </body>

</html>
Commit code to CodeCommit repository

To validate if our commit was successful, we can execute the following command to deploy the Node.js project inside the cloned repository. Try changing the text in the index.html file and running the widget again to test out how the files are changing on the CodeCommit repository.

cd web-app && node index.js > /dev/null 2>&1 &

Note: The server can be accessed by clicking the link after “Your app can be found at” in the widget above.

Additionally, for further validation, we can execute the following command in the widget above after closing the node server.

aws codecommit get-folder --repository-name $repository_name --folder-path web-app

The complete commit information about the files in the web-app folder should appear in the response to this command.

Copyright ©2024 Educative, Inc. All rights reserved