DEV Community

Cover image for CodeCommit Setup Workflow From the CLI
Ameh Mathias Ejeh
Ameh Mathias Ejeh

Posted on

CodeCommit Setup Workflow From the CLI

Introduction
In this lab, I will go through the process of configuring a CodeCommit repository from the AWS Command Line Interface. I will also go through some key prerequisite steps, such as installing Git, configuring HTTPS credentials, and preparing the environment to work with Git and CodeCommit. This lab covers the steps that need to be taken any time you work with CodeCommit from the CLI.

Lab Scenerio
Image description

In this lab, I will take on the role of a developer. As a developer, I need to work with AWS CodeCommit, so I will require CodeCommitFullAccess permissions. I will also create a repository and use an EC2 instance for my development work. To achieve this, I will ensure that Git is installed on the EC2 instance and set up Git credentials for seamless interaction with AWS CodeCommit. Once everything is configured, I will clone my repository to the local environment on the EC2 instance to start working.

Create a Server from an Amazon Linux 2 AMI
● Log in to the AWS Management Console as an IAM User
● Navigate to the EC2 dashboard.
● Click on the Launch Instance dropdown menu > Launch Instance.
● Under Name and tags, name the instance webserver.
● Under Application and OS Images (Amazon Machine Image), ensure the Amazon Linux AWS tile is selected and Amazon Linux 2 AMI is selected from the dropdown menu directly underneath the tile.
● Under Instance type, click on the dropdown menu and select t3.micro.
● Under Key pair (login) > Key pair name, click on the dropdown menu and select Proceed without a key pair (Not recommended).
● Under Network Settings, click Edit.
● Under Auto-assign public IP, click on the dropdown menu and select Enable.
● Leave the other settings at default, and click Launch instance.
● Once the instance is launched, click on the instance ID to navigate to the instance; ensure the Instance state is running.

Image description

Install Git and Create IAM CodeCommit Credentials
● Click on the checkbox next to webserver and, in the upper right, click Connect.
● Click Connect again.
● Once connected to the instance, update the server:
sudo yum update -y
● Install Git:
sudo yum install git -y
● Return to the AWS Management Console.
● Navigate to IAM
● In the left navigation menu, under Access management, click Users.
● Click on cloud_user.
● In the Permissions tab under Policy name, Click Add permissions > Add permissions
● Under Permission options, select Attach policies directly.
● Type CodeCommit in the filter search box, and add theAWSCodeCommitFullAccess policy.

Image description

Generate an Access Key
● Click the Security credentials tab.
● Under Access keys, click Create access key.
● Under Access key best practices & alternatives, select Command Line Interface (CLI).
● Scroll down and click on the checkbox next to I understand the above recommendation and want to proceed to create an access key.
● Click Next.
● Click Create access key.
● Copy the access key and secret access key, and save them to a separate text file; you'll need them later.
● Click Done, then click Continue on the pop-up.

Generate HTTPS Git Credentials for AWS CodeCommit
● Under HTTPS Git credentials for AWS CodeCommit, click Generate credentials.
● In the Generate credentials pop-up, copy the user name and password and save them to a separate text file; you'll need them later.
● Click Close.

Create a CodeCommit Repository
● Return to the terminal window.
● Configure the AWS CLI credentials:
aws configure
● When prompted, enter the AWS access key, then the secret access key you copied earlier.
● For the Default region name, enter us-east-1.
● For the Default output format, enter json.
● Run the following command to create a CodeCommit repository from the AWS CLI:
aws codecommit create-repository --repository-name RepoFromCLI

Image description

Clone the Repo
● Back in the AWS console, In the search bar at the top, enter CodeCommit; right-click on it and open in a new tab.
● Click on the RepoFromCLI repository link to open it.
● Click Add file.
● Click Upload file.
● Click Choose file.
● Select any small file from your machine you don't mind uploading.
● Enter your name under Author name and your email under Email address.
● Click Commit changes.
● Return to your terminal window.
Note: If your connection timed out, go back to the Connect to instance browser tab and click Connect again.
● Back in the CodeCommit browser tab, click on Repositories in the breadcrumb trail.
● Under Clone URL, copy the HTTPS hyperlink.
● Return to your terminal window.
● Run the following command to clone the repository to the server:
git clone
● For the username prompt, enter the Git credentials username you copied earlier.
● For the password prompt, enter the Git credentials password you copied earlier.
● Check the clone:
ls
● Go into the repo directory:
cd RepoFromCLI
● Confirm the file you uploaded earlier is in the repo:
ls

Image description

Test the Repo
● Run the following command to create a local text file:
vim test.txt
● Press i to enter insert mode and type the text:
This is a test file.
● Press the Escape key, type :wq!, and press Enter.
● Confirm the test.txt file was created:
ls
● Add the file to Git:
git add test.txt
● Commit the file to the local repository:
git commit -m "added test.txt"
● Verify the file was committed:
git log

Image description

● Push the change to the CodeCommit repository:
git push -u origin main
● Enter the Git credentials username you copied earlier for the username prompt.
● Enter the Git credentials password you copied earlier for the password prompt.

Image description

● Back in the CodeCommit browser tab, refresh the view of the RepoFromCLI repository and verify the new file was uploaded.

Image description

Top comments (0)