How to Create a CodeCommit Repo using CloudFormation

Create a CodeCommit repo using CloudFormation
Sharing is Caring:

Have you ever created a CodeCommit repo using CloudFormation?

Ever Wondered, how to create a CodeCommit repo using CloudFormation with existing code?

Created an empty repo and thought to update your stack to add code later? Is that even possible?

Don’t worry, we will answer all the above question in this post.

So ideally, In this post, I will help you create a CodeCommit repo using CloudFormation. I will also explain you the creation of an initial commit in your repo using CloudFormation.

Having said that, Let’s start with a brief introduction to AWS CodeCommit.

What is AWS CodeCommit?

AWS CodeCommit is a fully-managed source control service by amazon that hosts git based secure repository.

In other words, it is a fully managed private git repository. As it’s fully managed, you no longer need to host your own source control system or worry about scaling them.

  • CodeCommit encrypts all the files in transit and at rest
  • Integrates well with IAM to let us manage access to the repository
  • Supports all existing git commands and works well with a git client
  • Makes collaboration between developers an easy breezy process

The bare minimum template to create a CodeCommit repository

To create a CodeCommit repository, all you need is a AWS::CodeCommit::Repository resource.

The only mandatory parameter of this resource is RepositoryName so the simplest possible template to create a CodeCommit repo is below.

AWSTemplateFormatVersion: 2010-09-09
Description: Template to Create an empty  CodeCommit Repo

Resources:
  FirstRepo:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryName: DemoRepo

You can use above template to create a repo. It will create an empty repository.

Congratulation !!!

You have created your first CodeCommit repository using CloudFormation.

But wait a minute…

How do you put code there?

You can-

  • Navigate to your repository and upload code directory there : Check How
  • Clone your repo locally, add few files and push your changes : Check How
  • Create the repo with some existing code using CloudFormation : We will discuss in next section

As I said above, you will come across situations where you need to add some initial/sample code while creating a CodeCommit repo using CloudFormation.

I have seen a lot of people getting confused with the template around this topic so let’s address that in the next section.

How to create a CodeCommit Repo with initial Commit using CloudFormation

CloudFormation provides us with a parameter Code in AWS::CodeCommit::Repository resource. You can use it to provide information about code that you want to commit after repository is created.

Take your code -> Prepare a zip package -> Upload into an S3 Bucket -> Reference your code in your template.

Once you have zipped your code and uploaded into an s3 bucket, you can refer to it using Code parameter as mentioned below. (Don’t worry, I will share full template in some time.)

      Code:
        BranchName: main
 #Optional and used to specify a branch name as default branch
        S3: 
          Bucket: !Ref CodeBucket
 #Bucket in which zipped code lies
          Key: !Ref CodeKey
 #key of the zipped code

I don’t have versioning enabled on my bucket, so my code part looks like above.

But if you have versioning enabled on your bucket, please put the object version of your zipped code file as well like below.

      Code:
        BranchName: main
        S3: 
          Bucket: !Ref CodeBucket
 #Bucket in which zipped code lies
          Key: !Ref CodeKey
 #key of the zipped code
          ObjectVersion: 1 #version of the zipped code

Complete Template in YAML

AWSTemplateFormatVersion: 2010-09-09
Description: Template to Create CodeCommit Repo with Initial Commit

Parameters:
  CodeBucket:
    Type: String
    Description: Bucket in which you have code
    Default: code-bucket
  CodeKey:
    Type: String
    Description: key of zipped code
    Default: test-react/my-app.zip

Resources:
  FirstRepo:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryName: DemoRepo
      Code:
        BranchName: main
        S3: 
          Bucket: !Ref CodeBucket
          Key: !Ref CodeKey
          
Outputs:
  RepositoryID:
    Description: ID of the created CodeCommit repo
    Value: !Ref FirstRepo

Complete Template in JSON

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Template to Create CodeCommit Repo with Initial Commit",
    "Parameters": {
        "CodeBucket": {
            "Type": "String",
            "Description": "Bucket in which you have code",
            "Default": "code-bucket"
        },
        "CodeKey": {
            "Type": "String",
            "Description": "key of zipped code",
            "Default": "test-react/my-app.zip"
        }
    },
    "Resources": {
        "FirstRepo": {
            "Type": "AWS::CodeCommit::Repository",
            "Properties": {
                "RepositoryName": "DemoRepo",
                "Code": {
                    "BranchName": "main",
                    "S3": {
                        "Bucket": {
                            "Ref": "CodeBucket"
                        },
                        "Key": {
                            "Ref": "CodeKey"
                        }
                    }
                }
            }
        }
    },
    "Outputs": {
        "RepositoryID": {
            "Description": "ID of the created CodeCommit repo",
            "Value": {
                "Ref": "FirstRepo"
            }
        }
    }
}

Note: Changes to the Code property and all it’s sub properties are ignored after initial resource creation(we will discuss the same in the next section)

Can you create an empty repo using CloudFormation and update stack to add code later?

I see a lot of people creating an empty repo using CloudFormation. Later they try adding code by updating the stack with code parameter.

And it doesn’t have any effect on repo.

In other words, even after stack update repo is empty and not updated with code.

Well, fret not- If it happened with you.

This is the desired behavior.

At the time of writing this tutorial, you can’t use Code property to update code to an existing repository. You can add initial commit only during the stack creation using Code property.

After a stack is created, updating an stack with changed Code parameter will have no effect on your repo.

Steps to Create a CodeCommit Repo using CloudFormation

Now, we know the basics and we have the template so let’s go and create the stack.

  1. Grab the YAML or JSON template from above and change the parameter value of CodeBucket and CodeKey if your are creating repo with code
  2. Save the template with .yml or .json as per the choice of template and follow below steps.
  3. Login to AWS Management Console, navigate to CloudFormation and click on Create stack
  4. Click on “Upload a template file”, upload your saved .yml  or .json file and click Next
  5. Enter the stack name and click on Next. In configuration, keep everything as default and click on Next.
  6. In the events tab of stack, you can view the status.
  7. Once stack is successfully created, you can go to CodeCommit service and verify your repo
  8. You can see my repo created below.
create codecommit repo using cloudformation
create codecommit repo using cloudformation

Clean Up

If you are creating this repo for learning purpose. Don’t forget to delete your CloudFormation stack so that your repo is deleted and you don’t bear any cost for that.

Happy Learning !!!

Conclusion

In this post, you learnt to create a CodeCommit repo using CloudFormation. You also learnt to add an initial commit to your repo by putting it in S3 and referencing in Code parameter from your template.

Apart from above, you also understood that you can only use Code parameter during create stack to add an initial commit. Changed in Code parameter in stack update operations are ignored.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Sharing is Caring:

Leave a Reply

Your email address will not be published. Required fields are marked *