Creating CodeCommit resources with Amazon CloudFormation - Amazon CodeCommit
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Creating CodeCommit resources with Amazon CloudFormation

Amazon CodeCommit is integrated with Amazon CloudFormation, a service that helps you to model and set up your Amazon resources so that you can spend less time creating and managing your resources and infrastructure. You create a template that describes all the Amazon resources that you want (such as repositories), and Amazon CloudFormation provisions and configures those resources for you.

When you use Amazon CloudFormation, you can reuse your template to set up your CodeCommit resources consistently and repeatedly. Describe your resources once, and then provision the same resources over and over in multiple Amazon Web Services accounts and Regions.

CodeCommit and Amazon CloudFormation templates

To provision and configure resources for CodeCommit and related services, you must understand Amazon CloudFormation templates. Templates are formatted text files in JSON or YAML. These templates describe the resources that you want to provision in your Amazon CloudFormation stacks. If you're unfamiliar with JSON or YAML, you can use Amazon CloudFormation Designer to help you get started with Amazon CloudFormation templates. For more information, see What is Amazon CloudFormation Designer? in the Amazon CloudFormation User Guide.

CodeCommit supports creating repositories in Amazon CloudFormation Unlike creating repositories from the console or command line, you can use Amazon CloudFormation to create repositories and automatically commit code to the newly created repository from a specified .zip file in an Amazon S3 bucket. For more information, including examples of JSON and YAML templates for repositories, see AWS::CodeCommit::Repository.

When you create a CodeCommit repository using Amazon CloudFormation, you have the option to commit code to that repository as part of the creation process by configuring properties in AWS:CodeCommit::Repository Code. You can specify the Amazon S3 bucket where the code is stored, and optionally use the BranchName property to specify the name of the default branch that will be created in the initial commit of that code. These properties are only used in initial repository creation, and are ignored on stack updates. You cannot use these properties to make additional commits to a repository, or to change the name of the default branch after the initial commit is made.

Note

On January 19, 2021, Amazon changed the name of the default branch in CodeCommit from master to main. This name change affects the default behavior of CodeCommit when creating the initial commit for repositories using the CodeCommit console, the CodeCommit APIs, the Amazon SDKs, and the Amazon CLI. Repositories created with Amazon CloudFormation or the Amazon CDK with an initial commit of code as part of creation align with this change beginning March 4, 2021. This change does not affect existing repositories or branches. Customers who use local Git clients to create their initial commits have a default branch name that follows the configuration of those Git clients. For more information, see Working with branches, Create a commit, and Change branch settings.

You can also create templates that create related resources, such as notification rules for repositories, Amazon CodeBuild build projects, Amazon CodeDeploy applications, and Amazon CodePipeline pipelines.

Template examples

The following examples create a CodeCommit repository named MyDemoRepo. The newly created repository is populated with code stored in an Amazon S3 bucket named MySourceCodeBucket and placed in a branch named development, which is the default branch for the repository.

Note

The name of the Amazon S3 bucket that contains the ZIP file with the content that will be committed to the new repository can be specified using an ARN or the name of the bucket in the Amazon Web Services account. The Amazon S3 object key is as defined in the Amazon S3 Developer Guide.

JSON:

{ "MyRepo": { "Type": "AWS::CodeCommit::Repository", "Properties": { "RepositoryName": "MyDemoRepo", "RepositoryDescription": "This is a repository for my project with code from MySourceCodeBucket.", "Code": { "BranchName": "development", "S3": { "Bucket": "MySourceCodeBucket", "Key": "MyKey", "ObjectVersion": "1" } } } } }

YAML:

MyRepo: Type: AWS::CodeCommit::Repository Properties: RepositoryName: MyDemoRepo RepositoryDescription: This is a repository for my project with code from MySourceCodeBucket. Code: BranchName: development S3: Bucket: MySourceCodeBucket, Key: MyKey, ObjectVersion: 1

For more examples, see AWS::CodeCommit::Repository.

Amazon CloudFormation, CodeCommit, and the Amazon Cloud Development Kit (Amazon CDK)

Repositories created using the Amazon CDK use Amazon CloudFormation functionality in their creation. Understanding how Amazon CloudFormation templates work with CodeCommit resources can help you create and manage your Amazon CDK code. For more information about the Amazon CDK, see the Amazon Cloud Development Kit (Amazon CDK) Developer Guide and the Amazon CDK API Reference.

The following Amazon CDK Typescript example creates a CodeCommit repository named MyDemoRepo. The newly created repository is populated with code stored in an Amazon S3 bucket named MySourceCodeBucket and placed in a branch named development, which is the default branch for the repository.

import * as cdk from '@aws-cdk/core'; import codecommit = require('@aws-cdk/aws-codecommit'); export class CdkCodecommitStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // The code creates a CodeCommit repository with a default branch name development new codecommit.CfnRepository(this, 'MyRepoResource', { repositoryName: "MyDemoRepo", code: { "branchName": "development", "s3": { "bucket": "MySourceCodeBucket", "key": "MyKey" } }, } ); } }

Learn more about Amazon CloudFormation

To learn more about Amazon CloudFormation, see the following resources: