0
0
AwsHow-ToBeginner · 4 min read

How to Create AWS CloudFormation Template: Simple Guide

To create a CloudFormation template, write a JSON or YAML file that defines AWS resources and their properties. Use sections like Resources to specify what to create, then deploy it with AWS CloudFormation service to build your infrastructure automatically.
📐

Syntax

A CloudFormation template is a JSON or YAML file with key sections:

  • AWSTemplateFormatVersion: Template version (optional).
  • Description: Text about the template purpose.
  • Resources: Defines AWS resources to create.
  • Parameters: Inputs to customize the template.
  • Outputs: Values to return after creation.

The Resources section is required and lists each resource with its type and properties.

json
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "A simple CloudFormation template",
  "Resources": {
    "MyResource": {
      "Type": "AWS::Service::ResourceType",
      "Properties": {
        "PropertyName": "PropertyValue"
      }
    }
  }
}
💻

Example

This example creates an Amazon S3 bucket named my-sample-bucket-12345. It shows the basic structure and how to define a resource.

yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple S3 bucket creation
Resources:
  MyS3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: my-sample-bucket-12345
Output
When deployed, this template creates an S3 bucket named 'my-sample-bucket-12345' in your AWS account.
⚠️

Common Pitfalls

Common mistakes when creating CloudFormation templates include:

  • Missing the Resources section or resource Type.
  • Using invalid property names or values for resources.
  • Not specifying unique names for resources that require them.
  • Incorrect indentation or syntax errors in YAML or JSON.
  • Forgetting to update the template when changing resource names or dependencies.

Always validate your template with AWS tools before deployment.

json
Wrong example (missing Type):
{
  "Resources": {
    "MyBucket": {
      "Properties": {
        "BucketName": "my-bucket"
      }
    }
  }
}

Correct example:
{
  "Resources": {
    "MyBucket": {
      "Type": "AWS::S3::Bucket",
      "Properties": {
        "BucketName": "my-bucket"
      }
    }
  }
}
📊

Quick Reference

Tips for creating CloudFormation templates:

  • Use YAML for easier reading and writing.
  • Always include Resources section.
  • Validate templates with aws cloudformation validate-template.
  • Use Parameters to make templates reusable.
  • Use Outputs to get resource info after deployment.

Key Takeaways

A CloudFormation template is a JSON or YAML file defining AWS resources under the Resources section.
Always specify the resource Type and valid Properties to avoid deployment errors.
Use AWS CLI or console to validate and deploy your template safely.
YAML format is simpler and preferred for writing CloudFormation templates.
Parameters and Outputs make your templates flexible and informative.