AWS CodeCommit is a service for
Let’s learn how to create a code repository on CodeCommit and deploy some code to it.
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.
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.
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"}}
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.
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.
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
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 truegit config --global user.email example@email.com
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
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
Note: The
-T
flag with thecp
command is used to treat the destination, theweb-app
folder in the cloned repository’s directory, as a normal folder and replace it entirely with the source, theweb-app
folder in theusercode
directory.The
-R
flag with thecp
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
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"
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
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>
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.