How to Create a CodeCommit Repo using CloudFormation
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 questions in this post.
So ideally, In this post, I will help you create a CodeCommit repo using CloudFormation. I will also explain to 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.
Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.
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 example 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 the 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 the code there?
You can-
- Navigate to your repository and upload the code directory there: Check How
- Clone your repo locally, add a few files and push your changes: Check How
- Create the repo with some existing code using CloudFormation: We will discuss this 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 the code that you want to commit after the 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 it into an s3 bucket, you can refer to it using the Code parameter as mentioned below. (Don’t worry, I will share the 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 the 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 its 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 the 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 parameters.
And it doesn’t have any effect on the repo.
In other words, even after stack update repo is empty and not updated with code.
Well, fret not- If it happened to you.
This is the desired behaviour.
At the time of writing this tutorial, you can’t use the Code property to update code to an existing repository. You can add an initial commit only during the stack creation using the Code property.
After a stack is created, updating a stack with a 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.
- Grab the YAML or JSON template from above and change the parameter value of CodeBucket and CodeKey if you are creating a repo with code
- Save the template with .yml or .json as per the choice of template and follow the below steps.
- Login to AWS Management Console, navigate to CloudFormation and click on Create stack
- Click on “Upload a template file”, upload your saved .yml or .json file and click Next
- Enter the stack name and click on Next. In the configuration, keep everything as default and click on Next.
- In the events tab of the stack, you can view the status.
- Once the stack is successfully created, you can go to the CodeCommit service and verify your repo
- You can see my repo created below.
Note: You can also Deploy Your CloudFormation Template using AWS CLI
Clean Up
If you are creating this repo for learning purposes. 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 !!!
Related: Setup an AWS Cost Budget so that you never have to pay unwanted costs.
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 it in the Code parameter from your template.
Apart from the above, you also understood that you can only use the Code parameter during create stack to add an initial commit. Changed in Code parameters 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.
Don’t forget to motivate me by-
- Adding a comment below on what you liked and what can be improved.
- Follow us on
- Share this post with your friends
Suggested Read:
- 5 Ways to Create and Manage your AWS Resources
- How to Create an EC2 instance in an existing VPC using CloudFormation
- Attach an IAM role to an EC2 instance using CloudFormation
- How to create an S3 bucket using CloudFormation
- Understand IAM PassRole to Secure your AWS Infrastructure
- Serverless Services on AWS with Explanation
- How to Install Git on Amazon Linux 2 Instance.