DEV Community

Nurul Ramadhona for AWS Community Builders

Posted on • Updated on

GitHub Vibes on AWS: What Makes CodeCommit Special?

AWS CodeCommit is a secure, highly scalable, managed source control service that hosts private Git repositories. Simply means we can use GitHub on AWS with any special features such as easy scale and many more. One of the biggest benefits is it can be integrated with other AWS services. Before we talk about it, let me show you how to set up or use it first.

How To Use CodeCommit

1. Setup Credential

Just like we use GitHub, we need to set up credentials to be able to access our repositories but in this case, we will integrate it using IAM either HTTPS or SSH. Here I'll use SSH by uploading my public key to one of my IAM User credentials.

$ aws iam upload-ssh-public-key --user-name dhona --ssh-public-key-body file:///home/nurulramadhona/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Create config file under .ssh directory:

$ chmod 600 .ssh/config
$ cat .ssh/config 
Host git-codecommit.*.amazonaws.com
  User (ssh-key-id-goes-here)
  IdentityFile /home/nurulramadhona/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

2. Create & Clone Repository

Please specify the "other" region if it's not available yet in your region.

$ aws codecommit create-repository --repository-name ops-io --region ap-southeast-1
$ git clone ssh://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/ops-io
Cloning into 'ops-io'...
warning: You appear to have cloned an empty repository.
Enter fullscreen mode Exit fullscreen mode

3. Make Any Changes

  • GitHub commands:
$ cd ops-io/
~/ops-io$ cat test.txt
this is first file
~/ops-io$ git add -A
~/ops-io$ git commit -m "upload first file"
[master (root-commit) 1406bd0] upload first file
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
~/ops-io$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 236 bytes | 236.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git-codecommit.ap-southeast-1.amazonaws.com/v1/repos/ops-io
 * [new branch]      master -> master
Enter fullscreen mode Exit fullscreen mode
  • CodeCommit commands:
~/ops-io$ echo "this is second file" > file.txt
~/ops-io$ aws codecommit get-branch --repository-name ops-io --branch-name master --region ap-southeast-1
{
    "branch": {
        "branchName": "master",
        "commitId": "1406bd0566858448e486011edc364ca3a77d0b0d"
    }
}
~/ops-io$ aws codecommit create-commit --repository-name ops-io --branch-name master --parent-commit-id 1406bd0566858448e486011edc364ca3a77d0b0d --put-files "filePath=file.txt,fileContent='upload 2nd file'" --region ap-southeast-1
{
    "commitId": "307e9fb75d0a7ccc490aacbf28aba957342a8c3b",
    "treeId": "918e7c9702c916bc7b67f7645432e048cdfe94a4",
    "filesAdded": [
        {
            "absolutePath": "file.txt",
            "blobId": "6e29f4102c6b829414b53053cd65c1b123d28d29",
            "fileMode": "NORMAL"
        }
    ],
    "filesUpdated": [],
    "filesDeleted": []
}
Enter fullscreen mode Exit fullscreen mode

4. List Repositories & Branches

$ aws codecommit get-branch --repository-name ops-io --branch-name master --region ap-southeast-1
{
    "branch": {
        "branchName": "master",
        "commitId": "1406bd0566858448e486011edc364ca3a77d0b0d"
    }
}
$ aws codecommit get-folder --repository-name ops-io --folder-path "" --region ap-southeast-1
{
    "commitId": "307e9fb75d0a7ccc490aacbf28aba957342a8c3b",
    "folderPath": "",
    "treeId": "918e7c9702c916bc7b67f7645432e048cdfe94a4",
    "subFolders": [],
    "files": [
        {
            "blobId": "6e29f4102c6b829414b53053cd65c1b123d28d29",
            "absolutePath": "file.txt",
            "relativePath": "file.txt",
            "fileMode": "NORMAL"
        },
        {
            "blobId": "ce553855c343e93616e1e937cd288a9824f0d761",
            "absolutePath": "test.txt",
            "relativePath": "test.txt",
            "fileMode": "NORMAL"
        }
    ],
    "symbolicLinks": [],
    "subModules": []
}
Enter fullscreen mode Exit fullscreen mode

Special Task: Copy S3 Object To CodeCommit

As I mentioned before, one of the biggest benefits we host services on AWS is that they can be integrated with one another. Here I'll give you a simple task of how we can do it.

I'll go with CloudFormation to create a new repository named S3 and copy objects (zip format) from the S3 bucket to the branch bucket.

Before that, I'll create a new bucket named repository-ops-io with versioning enabled and upload the zip file to that bucket.

1. Create Bucket

$ aws s3 mb s3://repository-ops-io --region ap-southeast-1
make_bucket: repository-ops-io
$ aws s3api put-bucket-versioning --bucket repository-ops-io --versioning-configuration MFADelete=Disabled,Status=Enabled --region ap-southeast-1
$ aws s3api get-bucket-versioning --bucket repository-ops-io --region ap-southeast-1
{
    "Status": "Enabled",
    "MFADelete": "Disabled"
}
Enter fullscreen mode Exit fullscreen mode

2. Upload Object

$ touch s3.txt
$ zip s3.zip s3.txt
  adding: s3.txt (stored 0%)
$ ls
s3.zip
$ aws s3 cp s3.zip s3://repository-ops-io --region ap-southeast-1
upload: ./s3.zip to s3://repository-ops-io/s3.zip
$ aws s3api list-object-versions --bucket repository-ops-io --query 'Versions[].[Key, VersionId]' --region ap-southeast-1
[
    [
        "s3.zip",
        "Km7s2m1Jd2FlcvoIi9v7n.KUenzjHZ5R"
    ]
]
Enter fullscreen mode Exit fullscreen mode

3. Create Stack I give the template name ops-io.yaml.

AWSTemplateFormatVersion: 2010-09-09
Resources:
  Repository:
    Type: 'AWS::CodeCommit::Repository'
    Properties:
      RepositoryName: s3
      RepositoryDescription: S3 to Codecommit
      Code:
        BranchName: bucket
        S3:
          Bucket: repository-ops-io
          Key: s3.zip
          ObjectVersion: Km7s2m1Jd2FlcvoIi9v7n.KUenzjHZ5R
Enter fullscreen mode Exit fullscreen mode
$ aws cloudformation create-stack --stack-name ops-io --template-body file://ops-io.yaml --region ap-southeast-1 --region ap-southeast-1
{
    "StackId": "arn:aws:cloudformation:ap-southeast-1:0123456789:stack/ops-io/ae15bb10-dcaf-11ec-b865-0a5e031cf822"
}
Enter fullscreen mode Exit fullscreen mode

Let's check!

$ aws codecommit list-repositories --region ap-southeast-1
{
    "repositories": [
        {
            "repositoryName": "ops-io",
            "repositoryId": "706334d5-c480-4b69-9395-512e0aff3a4c"
        },
        {
            "repositoryName": "s3",
            "repositoryId": "71719f7e-5e45-4b25-aa82-3247bfd297a3"
        }
    ]
}
$ aws codecommit list-branches --repository-name s3 --region ap-southeast-1
{
    "branches": [
        "bucket"
    ]
}
$ aws codecommit get-folder --repository-name s3 --folder-path "" --region ap-southeast-1
{
    "commitId": "1c69f63e89e579a7e41b27c2b9a97e2f90344f26",
    "folderPath": "",
    "treeId": "3210de697af8657277ba1d8ec5509438f4521482",
    "subFolders": [],
    "files": [
        {
            "blobId": "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
            "absolutePath": "s3.txt",
            "relativePath": "s3.txt",
            "fileMode": "NORMAL"
        }
    ],
    "symbolicLinks": [],
    "subModules": []
}
Enter fullscreen mode Exit fullscreen mode

Additional:

To delete all resources created above, please do the following steps:

  1. Delete the S3 object with the version mentioned

  2. Delete the S3 bucket

  3. Delete the CloudFormation stack

  4. Delete the CodeCommit repository

That's it! Thank you for coming and I'm looking forward to your feedback. Follow me to get notified when my new post is published!

Top comments (0)