AWS Code Commit Configurations w/CLI
Configuring Code Commit using the command line interface
In this walkthrough we’re going to configure and work with AWS Code Commit utilizing the command line interface (CLI). But first, what exactly is AWS Code Commit 🤔?
AWS Code Commit is a managed version control service that hosts private Git repositories. Code Commit is a lot like GitHub but it’s an AWS service that integrates seamlessly with other AWS services like Code Deploy, for example.
In this walkthrough, you’ll notice that our prerequisite setup is a big part of the project so be sure to following along as best as you can.
Ready to get started? Let’s go 🚀!
*For those of you who aren’t too familiar with Git or Git repositories, feel free to check out my previous article on Git/Github for a more in depth introduction.*
Prerequisites
- An EC2 instance that we will SSH into from our local terminal
- An IAM user w/proper Code Commit credentials (IAM git credentials, IAM generated Access Key and Secret Access Key)
- Git installed on your linux server
Part 1 (EC2 Setup)
First thing’s first, you’re going to want to of course login to your AWS account. Once that’s done, head over to EC2 and hit “Launch Instance”.
For this walkthrough we’re going to use an Amazon Linux 2 AMI…
On a t2.micro instance type. Now click “Next:Configure Instance Details”.
Here we’re going to enable utilizing the subnet setting.
This is what should be selected now. Next, click “Review and Launch”. There’s no need for us to add storage.
Now review all of the instance information to ensure everything looks good and click “Launch”.
Before we officially launch our instance though, we need to create a Key Pair. Don’t forget to download the Key Pair once it’s been created! Once your Key Pair has been downloaded, launch your instance.
This is the confirmation page you should end up with verifying that your EC2 instance has been created.
Scroll down to the bottom of the page and click “View Instances”.
Viola 🪄. Our EC2 instance has been created.
Now, open your terminal window on your computer (I’m using a Mac).
Next we’re going to cd into the directory that holds our Key Pair. Since we downloaded the .pem file onto our computer, it should be located in the downloads directory on your computer.
cd downloads
To confirm that your .pem file is located in this directory, use the ls command.
Next, change the permissions of your Key Pair using chmod 400 name_of_keypair.pem
Now that your Key Pair has the appropriate permissions, we can use it to SSH into our EC2 server. But first let’s head over to the EC2 instance we created to grab the public IP address
Copy the Public IP address to your clipboard.
Head back to your terminal and input ssh ec2-user@your_IP_addr_here -i name_of_keypair.pem Once you’re prompted to select yes/no select yes
You should now see that your terminal is logged into your EC2 instance! You’re going to want to run sudo yum update to update any packages.
Part 2 (IAM user credential setup)
Head over to IAM in the AWS Console. Here we’re going to create a new user. Select the “Users” tab.
Select the “Add Users” button.
Now you’re going to give your IAM user: a name, Programmatic access, and access to the AWS Management Console.
Uncheck the button where it says “Require password reset” and click “Next:Permissions”.
Select the “Attach existing policies directly”.
In the search bar input Code Commit and select “AWS CodeCommit FullAccess”. Click “Next:Tags”.
Skip past the tags section and to the final review page and click “Create User”.
Once you’re on the next page, be sure to download the CSV file with your Access Key ID and Secret Access Key. Also try to copy it down somewhere on your computer .
Get Toni Benn’s stories in your inbox
Join Medium for free to get updates from this writer.
Return back to your Users homepage and click on the user we’ve just created.
Click on the “Security Credentials” tab.
Once there, scroll down to the bottom where you’ll see “HTTPS Git credentials for AWS CodeCommit” and select “Generate credentials”.
This screen is what will appear as a result. Be sure to save your Git credential information as we will be using it in our terminal window later on.
Part 3 (Git Instillation on Linux Server)
In this step I will provide you with the command but will not add screenshots. I already have Git installed on my local environment but once running the command below, you’ll see that Git instillation is pretty straightforward.
sudo yum install git
Part 4 (Creating a repository in Code Commit through the CLI)
Now we’re going create a Code Commit repository from the CLI.
First run aws configure. Grab your IAM user’s Access Key ID and Secret Access Key.
You can input the same region name and output format that you see below.
To create a repository in Code Commit via the CLI input this command:
aws codecommit create-repository --repository-name Your_Repo_Name --repository-description "Saturday Walkthrough with Toni"
Once the repository has been created, you will receive JSON output as confirmation.
To verify that our repository has been created, lets double check in the AWS Console.
Great 👏🏾!
Part 5 (Cloning your Code Commit Repo and Pushing Up a New File)
We’re now going to create a file in the AWS console, clone it to our local machine, add a new file, and push the new file up to the main branch.
Click into the repository we made in the AWS management console.
Click “Create a file” and add your first line of text.
Afterwards input your name, email address and a message to add to the commit. Click “Commit Changes” at the bottom.
Before heading over to your local terminal window we need to select the “Clone URL” button in the screenshot above. The URL we were given in the JSON output in our local environment is now out of date since we have added a new file in the AWS management console.
Clone HTTPS because that is how we’ve chosen to communicate with Code Commit.
Head back over to your terminal and input this command:
git clone input_URL_here
You will be prompted to input a username/password. Remember when we created Git credentials? Head over to wherever you have this information saved, and insert it into the correct fields.
Once an updated copy of your repo is downloaded onto your local environment, hit ls to confirm that your repo is there.
cd into the repository.
Check to see if the file we created in the AWS console is in this directory by again using the ls command.
Perfect.
Part 5.1(Pushing a new file up to your repository)
Now we’re going to create a file on our local machine and push it up to our repo in Code Commit while in the CLI.
We’re going to use the vim text editor to create our new file.
vim name_of_file
i to input text, esc to stop editing and :wq! to save file and exit vim.
cat name_of_file to confirm our text was saved properly.
Now we’re going to stage this file to our local repository using the command: git add name_of_file
Next we’re going to commit the file to our local repository using git commit -m “add comment here”
To verify that this worked run git log
Now that we have committed our file to our local repository, we need to push the file up to our main repository using git push -u origin main. You’ll also need your Git credentials once more.
Let’s check in the AWS console if our new file has been added.
Success 🤠. Great job for making it to this end of this walkthrough and getting your hands dirty with Code Commit.










