View a markdown version of this page

Working with CloudFormation templates - AWS CloudFormation

Working with CloudFormation templates

An AWS CloudFormation template defines the AWS resources you want to create, update, or delete as part of a stack. It consists of several sections, but the only required section is the Resources section, which must declare at least one resource.

You can create templates using the following methods:

  • AWS Infrastructure Composer – A visual interface for designing templates.

  • Text Editor – Write templates directly in JSON or YAML syntax.

  • IaC generator – Generate templates from resources provisioned in your account that are not currently managed by CloudFormation. The IaC generator works with a wide range of resource types that are supported by the Cloud Control API in your Region.

This section provides a comprehensive guide on how to use the different sections of a CloudFormation template and how to start creating stack templates. It covers the following topics:

Where templates get stored

Amazon S3 bucket

You can store CloudFormation templates in an Amazon S3 bucket. When creating or updating a stack, you can specify the S3 URL of the template instead of uploading it directly.

If you upload templates directly through the AWS Management Console or AWS CLI, an S3 bucket is automatically created for you. For more information, see Create a stack from the CloudFormation console.

Git repository

With Git sync, you can store templates in a Git repository. When creating or updating a stack, you can specify the Git repository location and branch containing the template instead of uploading it directly or referencing an S3 URL. CloudFormation automatically monitors the specified repository and branch for template changes. For more information, see Create a stack from repository source code with Git sync.

Validating templates

Validate your templates before deployment so that you can find and fix problems earlier. The CloudFormation service and the tools in this section check different parts of a template. You can use them together.

You can check whether a template uses valid JSON or YAML with the validate-template CLI command or by specifying your template in the AWS Management Console. The console performs this check automatically. These service-side checks don't run the additional property, security, or best-practice checks provided by the following tools:

  • CloudFormation Language Server – Get suggestions, documentation, and validation feedback while you write templates in an editor.

  • CloudFormation Linter – Check resource properties, allowed values, and common problems from the command line, an editor, or an automated build.

  • CloudFormation Validate – Run local, offline checks from the command line or a library, and add custom Rego or Guard rules. AWS CDK uses this validator automatically after it synthesizes your templates.

  • CloudFormation Guard – Write policy rules and check templates against your organization's security, compliance, and governance requirements.

Use with AI coding agents

AI coding agents can help you author, validate, and troubleshoot CloudFormation templates using the aws-cloudformation skill from the Agent Toolkit for AWS on GitHub. For installation instructions, see Agent setup guide.

Understand validation scope

Local tools check the template and the rules that you give them. They can't guarantee that deployment will succeed with the current resources, permissions, and quotas in an AWS account and AWS Region. Before deployment, review a change set and fix any problems reported by the CloudFormation service.

Getting started with templates

To get started with creating a CloudFormation template, follow these steps:

  1. Choose resources – Identify the AWS resources you want to include in your stack, such as EC2 instances, VPCs, security groups, and more.

  2. Write the template – Write the template in JSON or YAML format, defining the resources and their properties.

  3. Save the template – Save the template locally with a file extension like: .json, .yaml, or .txt.

  4. Validate the template – Validate the template using the methods described in the Validating templates section.

  5. Create a stack – Create a stack using the validated template.

Plan to use the CloudFormation template reference

As you write your templates, you can find documentation for the detailed syntax for different resource types in the AWS resource and property types reference.

Often, your stack templates will require intrinsic functions to assign property values that are not available until runtime and special attributes to control the behavior of resources. As you write your template, refer to the following resources for guidance:

  • Intrinsic function reference – Some commonly used intrinsic functions include:

    • Ref – Retrieves the value of a parameter or the physical ID of a resource.

    • Sub – Substitutes placeholders in strings with actual values.

    • GetAtt – Returns the value of an attribute from a resource in the template.

    • Join – Joins a set of values into a single string.

  • Resource attribute reference – Some commonly used special attributes include:

    • DependsOn – Use this attribute to specify that one resource must be created after another.

    • DeletionPolicy – Use this attribute to specify how CloudFormation should handle the deletion of a resource.

Sample templates

CloudFormation provides open-source stack templates that you can use to get started. For more information, see CloudFormation Sample Templates on GitHub.

Keep in mind that these templates are not meant to be production-ready. You should take the time to learn how they work, adapt them to your needs, and make sure that they meet your company's compliance standards.

Each template in this repository passes CloudFormation Linter (cfn-lint) checks, and also a basic set of AWS CloudFormation Guard rules based on the Center for Internet Security (CIS) Top 20, with exceptions for some rules where it made sense to keep the sample focused on a single use case.