In this hands-on lab, we go through the process of configuring a CodeCommit repository from the AWS Command Line Interface. We 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 anytime you work with CodeCommit from the CLI.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create a Server from an Amazon Linux 2 AMI
- Navigate to EC2.
- Click Launch Instance.
- Select the Amazon Linux 2 AMI (HVM), SSD Volume Type 64bit (x86) AMI.
- Select
t2.micro
as the instance type. - Select the VPC provided in the lab.
- Select the subnet provided.
- Select Enable for Auto-assign Public IP.
- Click Review and Launch > Launch.
- Select Create a new key pair.
- Enter "confighttps" as the key pair name.
- Click Download Key Pair.
- Click Launch Instances > View Instances.
- Install Git and Create IAM CodeCommit Credentials
Select the newly created instance.
Click Connect at the top of the screen.
Open a terminal window and navigate to the folder for your downloads.
Linux/Mac users: Execute the following command:
chmod 400 confighttps.pem
Linux/Mac users: Run the example SSH command from the Connect window in the AWS Management Console in your terminal application.
Note: If you are using Windows/PuTTY, use the link in the Connect window in the AWS Management Console that says connect using PuTTY and follow the instructions.
In the terminal, run the following command:
sudo yum update -y
*Note: We were just describing the IAM steps, but the needed permissions already exist for the cloud_user.
In the AWS Management Console, navigate to IAM > Users.
Click on cloud_user.
Click Add permissions > Attach existing policies directly.
Type "AWSCodeCommit" in the filter search box.
Select the
AWSCodeCommitFullAccess
policy.Click Cancel. (We are clicking cancel here because the cloud_user already has the needed permissions and the lab’s cloud_user account can not be modified).
In the terminal, run the following command to install Git:
sudo yum install git -y
In the AWS Management Console, with
cloud_user
opened, click the Security credentials tab.Click Create access key.
Click Download .csv file.
Navigate to the bottom of the screen, and click Generate under HTTPS Git credentials for AWS CodeCommit.
Click Download credentials > Close.
- Create CodeCommit Repository
Open the terminal window.
Configure the AWS CLI credentials:
aws configure
Enter the AWS
Access Key
andSecret Access Key
for the cloud_user user.Enter
us-east-1
as the region and accept the Default output format.Run the following command to create a CodeCommit repository from the AWS CLI:
aws codecommit create-repository --repository-name RepoFromCLI --repository-description "My demonstration repository"
In the AWS Management Console, open CodeCommit and refresh the screen.
Click on the RepoFromCLI repository link to open it.
Click Add file > Upload file.
Click Choose file.
Select any small file from your machine you don’t mind uploading.
Enter your name as the author name and your email as the email address.
Click Commit changes.
Return to your terminal window.
Copy the URL next to
"cloneUrlHttp":
, and be careful not to copy the quotation marks.Run the following command to clone the repository to the server:
git clone [paste the URL you just copied]
Enter the Git credentials username from the file downloaded earlier for the username prompt.
Enter the Git credentials password from the file downloaded earlier for the password prompt.
Check the clone:
ls
- Go into the repo directory:
cd RepoFromCLI
Look for files in the repo:
ls
Run the following command to create a local text file:
vim test.txt
Hit
i
to enter insert mode and type the text:This is just a test of working with CodeCommit from the CLI
Hit the Escape key, type
:wq!
, and press Enter.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
Push the change to the CodeCommit repository:
git push -u origin main
Enter the Git credentials username from the file downloaded earlier for the username prompt.
Enter the Git credentials password from the file downloaded earlier for the password prompt.
Refresh the AWS Management Console view of the CodeCommit repository for RepoFromCLI and verify the new file was uploaded.