AWS CodeCommit Tutorial

AWS CodeCommit Tutorial


1. Creating and Configuring an AWS CodeCommit Repository

Example 1: Create a New Repository

aws codecommit create-repository --repository-name MyRepo --repository-description "My first AWS CodeCommit repository"

Explanation

  1. Creates a new CodeCommit repository for version control.

    • aws codecommit create-repository
  2. Specifies a unique repository name (MyRepo).

    • --repository-name MyRepo
  3. Adds an optional description for organization purposes.

    • --repository-description "My first AWS CodeCommit repository"
  4. The repository is now available in AWS CodeCommit.

    • create-repository

Example 2: List Repositories

aws codecommit list-repositories

Explanation

  1. Retrieves all repositories in the AWS account.

    • aws codecommit list-repositories
  2. Useful for managing multiple repositories across teams.

    • list-repositories
  3. Returns repository names and IDs in JSON format.

    • list-repositories
  4. Helps verify repository creation or access status.

    • list-repositories

Example 3: Get Repository Details

aws codecommit get-repository --repository-name MyRepo

Explanation

  1. Fetches detailed information about a specific repository.

    • aws codecommit get-repository
  2. Provides metadata like ARN, clone URL, and description.

    • --repository-name MyRepo
  3. Useful for checking repository configurations.

    • get-repository
  4. Helps verify repository access and settings.

    • get-repository

Example 4: Delete a Repository

aws codecommit delete-repository --repository-name MyRepo

Explanation

  1. Deletes a CodeCommit repository permanently.

    • aws codecommit delete-repository
  2. Requires the exact repository name to prevent accidental deletion.

    • --repository-name MyRepo
  3. Cannot be undone once deleted.

    • delete-repository
  4. Frees up repository names for future use.

    • delete-repository

2. Cloning and Managing a CodeCommit Repository Locally

Example 1: Clone a Repository

git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyRepo

Explanation

  1. Clones an AWS CodeCommit repository to a local machine.

    • git clone
  2. Uses HTTPS for authentication (requires AWS IAM credentials).

  3. Downloads all repository files and commits.

    • git clone
  4. Allows local development and version control.

    • git clone

Example 2: Set Up SSH for Authentication

ssh-keygen -t rsa -b 2048 -C "user@example.com"
aws iam upload-ssh-public-key --user-name my-user --ssh-public-key-body file://~/.ssh/id_rsa.pub

Explanation

  1. Generates an SSH key pair for authentication.

  2. Uploads the public key to AWS IAM.

    • aws iam upload-ssh-public-key
  3. Links the key to a specific AWS IAM user.

    • --user-name my-user
  4. Required for using SSH-based CodeCommit URLs.

    • upload-ssh-public-key

Example 3: Add & Commit Files to Repository

git add .
git commit -m "Initial commit"
git push origin main

Explanation

  1. Stages all changes for commit.

    • git add .
  2. Creates a commit with a message for tracking changes.

    • git commit -m "Initial commit"
  3. Pushes changes to the remote repository.

    • git push origin main
  4. Ensures all updates are stored in CodeCommit.

    • git push

Example 4: Pull Latest Changes

git pull origin main

Explanation

  1. Fetches and merges the latest changes from CodeCommit.

    • git pull origin main
  2. Ensures local and remote repositories stay in sync.

    • pull
  3. Prevents conflicts by integrating remote updates.

    • pull
  4. Useful when collaborating with multiple developers.

    • git pull

3. Configuring AWS CodeCommit Triggers & Notifications

Example 1: Create a Trigger for Push Events

aws codecommit put-repository-triggers --repository-name MyRepo --triggers '[
  {
    "name": "NotifyOnPush",
    "destinationArn": "arn:aws:sns:us-east-1:123456789012:MySNSTopic",
    "events": ["all"]
  }
]'

Explanation

  1. Defines an event trigger for repository actions.

    • put-repository-triggers
  2. Links the trigger to an SNS topic for notifications.

    • "destinationArn": "arn:aws:sns:us-east-1:123456789012:MySNSTopic"
  3. Activates the trigger on all repository events.

    • "events": ["all"]
  4. Useful for CI/CD automation and monitoring.

    • put-repository-triggers

Example 2: List All Triggers

aws codecommit get-repository-triggers --repository-name MyRepo

Explanation

  1. Retrieves existing triggers on a repository.

    • get-repository-triggers
  2. Useful for auditing and debugging repository automations.

    • get-repository-triggers
  3. Displays trigger names, destinations, and events.

    • get-repository-triggers
  4. Ensures notifications and automations are configured correctly.

    • get-repository-triggers

Example 3: Delete a Trigger

aws codecommit put-repository-triggers --repository-name MyRepo --triggers '[]'

Explanation

  1. Removes all triggers from a repository.

    • "triggers": "[]"
  2. Prevents further notifications or automations.

    • put-repository-triggers
  3. Used when changing automation configurations.

    • put-repository-triggers
  4. Ensures unwanted triggers do not interfere with workflow.

    • put-repository-triggers

Example 4: Enable CloudWatch Logging for CodeCommit

aws logs create-log-group --log-group-name /aws/codecommit/MyRepo

Explanation

  1. Creates a CloudWatch Log Group for CodeCommit events.

    • create-log-group
  2. Useful for monitoring push, commit, and branch changes.

    • /aws/codecommit/MyRepo
  3. Helps with debugging and audit logging.

    • create-log-group
  4. Required for compliance and security tracking.

    • create-log-group