Securing Your Code with AWS CodeCommit | A Step-by-Step Guide cover image

Securing Your Code with AWS CodeCommit | A Step-by-Step Guide

AZRAF AL MONZIM
by

Published on

Learn how to set up AWS CodeCommit seamlessly with these step-by-step instructions, ensuring smooth integration and efficient collaboration for your development projects.

Before you begin configuring AWS CodeCommit, make sure you have the following:

Prerequisites:

Now, let's dive into the step-by-step process:

Step 1: Create a repository in AWS CodeCommit

Start by logging into your AWS Management Console and navigating to the CodeCommit service. Click on "Create repository" and provide a name for your repository. Then, simply click on "Create repository" to proceed.

  1. Navigate to the AWS Management Console and open the CodeCommit console: https://console.aws.amazon.com/codesuite/codecommit/home
  2. Click on "Create repository."
  3. Enter a descriptive name for your repository in the "Repository name" field.
  4. Click "Create repository" to finalize the creation process.

Step 2: Set up IAM user for AWS CodeCommit

To access your repository, you'll need to create an IAM user with the necessary permissions. Go to the IAM service in your AWS Management Console. From the navigation pane, select "Users" and click on "Add user." Choose a username and select "Programmatic access." Proceed to the permissions step and attach the "AWSCodeCommitPowerUser" policy. Complete the setup and download the user's credentials.

  1. Go to the IAM console in the AWS Management Console: https://console.aws.amazon.com/iam/home
  2. From the navigation pane, select "Users" followed by "Add user."
  3. Assign a unique name to the user in the "User name" field.
  4. Choose "Programmatic access" for access type.
  5. Click "Next: Permissions" to proceed.
  6. Select "Attach existing policies directly" and search for "AWSCodeCommitPowerUser" in the search bar.
  7. Choose the "AWSCodeCommitPowerUser" policy to grant the necessary permissions.
  8. Click through "Next: Tags" and "Next: Review" to reach the final step.
  9. Click "Create user" to complete the process.
  10. Download the user credentials as a CSV file for later use.

Step 3: Configure AWS CLI

  1. Open your terminal and run the following command to configure the AWS CLI

    aws configure
  2. Enter your access key ID, secret access key, default region, and desired output format when prompted.

Step 4: Set up SSH Key

For secure access to your repository, you'll need to generate an SSH key pair.

  1. In your terminal, navigate to your SSH directory and generate a key pair using the following command:

    cd ~/.ssh
    ssh-keygen -t rsa -b 4096
  2. Press Enter to save the key at the default location (~/.ssh/) and optionally provide a name (e.g., codecommit_rsa).

  3. Enter a passphrase for additional security (optional).

  4. To copy the public key to your clipboard, run:

    cat ~/.ssh/codecommit_rsa.pub
  5. Now, let's upload the public key to your IAM user:

    • Go back to the IAM console (https://console.aws.amazon.com/iam/home).
    • Select "Users" from the navigation pane and choose the IAM user you created earlier.
    • Click on the "Security credentials" tab.
    • Select "Upload SSH public key."
    • Paste the copied public key into the "SSH public key" field and click "Upload SSH public key."
    • Note down the SSH key ID displayed for later use.
  6. Configure your SSH client to utilize the key when connecting to CodeCommit:

    • Open your SSH configuration file using a text editor:

      vi ~/.ssh/config
    • Add the following configuration to the file, replacing <SSH key ID> with the previously noted ID and <region> with your AWS region:

      Host git-codecommit.<region>.amazonaws.com
          User <SSH key ID>
          IdentityFile ~/.ssh/codecommit_rsa
    • Ensure the SSH configuration file has the correct permissions:

      chmod 600 ~/.ssh/config

Step 5: Clone the repository

Finally, clone your repository using the git clone command. Replace <region> with your AWS region and <repository-name> with the name of your repository. Run the command in your terminal:

git clone ssh://git-codecommit.<region>.amazonaws.com/v1/repos/<repository-name>

By following these steps, you've successfully configured AWS CodeCommit and established a secure connection using SSH keys. Now you can start collaborating on your code projects using Git commands