CloudFormation JSON syntax is a template file that describes AWS resources in a fixed structure: one top-level object with optional sections like AWSTemplateFormatVersion, Description, Parameters, Mappings, Conditions, Resources, Outputs, and metadata. Every template must contain a Resources section, and that is the only required one. Write the file with strict JSON rules and CloudFormation will parse it, validate it, and create a stack that reflects it.
The steps below build a small, working template from scratch and then show how the sections connect. They require nothing but a text editor and an AWS account with permission to create stacks; you can copy the template into the AWS Management Console, or use the AWS CLI if you prefer.
What You’ll Need
- An AWS account and an IAM identity allowed to create CloudFormation stacks and the resources the template declares.
- A plain text editor that saves files as UTF-8 and can validate JSON (many editors highlight syntax errors as you type).
- A file ending in
.json, for exampletemplate.json. - Optional: the AWS CLI installed and configured if you want to validate or deploy from a terminal instead of the console.
Step-by-Step
- Start with the outer braces and the version line. CloudFormation templates are a single JSON object. Open the file with
{and add the format version as the first key. The supported value is2010-09-09; it is optional but harmless and helps readers of the file:{ "AWSTemplateFormatVersion": "2010-09-09", ... } - Add a Description. This is a plain string, limited to 1024 bytes, placed near the top:
"Description": "Demo template for a resource bucket". It shows in the console stack list and in template details, so make it meaningful. - Declare Parameters for values that change between deployments. A Parameter block has a name, a
Type, aDescription, and optionallyDefault,AllowedValues,MinLength, orMaxLength:"Parameters": { "BucketSuffix": { "Type": "String", "Description": "Lowercase suffix for the bucket name", "AllowedPattern": "^[a-z0-9-]{3,12}$" } }. Types such asString,Number,CommaDelimitedList, and theAWS::-prefixed parameter types (for example an existing VPC ID) tell CloudFormation how to validate the input. - Add Mappings if you need static lookup tables. Mappings map keys to values and cannot change at deploy time:
"Mappings": { "RegionMap": { "us-east-1": { "Ami": "ami-xxxxxxxxexample" } } }. Use a mapping for region-specific values, and read it withFn::FindInMap. - Add Conditions for optional resources. Conditions are boolean expressions evaluated at create and update time, using parameters or mappings:
"Conditions": { "IsProduction": { "Fn::Equals": [ { "Ref": "Environment" }, "prod" ] } }. Then a resource can set"Condition": "IsProduction"and CloudFormation skips it for other environments. - Write the Resources section — the part every template needs. Each resource has a logical ID (alphanumeric, unique in the template), a
Typesuch asAWS::S3::Bucket, and aPropertiesobject whose keys depend on the resource type. A minimal example: - Reference other resources with Ref and GetAtt.
{ "Ref": "MyBucket" }returns the resource’s identifier, which is often its name.{ "Fn::GetAtt": [ "MyBucket", "Arn" ] }reads an attribute that CloudFormation exposes after creation. UseFn::JoinorFn::Subwhen you need to build strings such as an ARN or a URL. - Add Outputs so the stack returns useful values. Outputs have a name and a value, and optionally a description and an export name:
"Outputs": { "BucketName": { "Description": "Name of the bucket", "Value": { "Ref": "MyBucket" } } }. Exports let other stacks import the value. - Check the JSON syntax before deploying. Run
python -m json.tool template.jsonorjq . template.json; both print an error with a line number if the file is malformed. If you use the CLI, you can also callaws cloudformation validate-template --template-body file://template.json. - Deploy and read the events. In the console, choose Create stack, upload the template, give the stack a name, and submit. CloudFormation will create resources in dependency order. Watch the events tab: the first red line points to the real failure, not the lines after it.
- Iterate with change sets. Edit the template, then create a change set against the existing stack to preview what would be added, changed, or replaced. Many property changes force resource replacement, and replacement can destroy data if it recreates a stateful resource.
Common Mistakes
Most CloudFormation JSON errors come from a handful of habits:
- Trailing commas and comments. JSON allows neither. A trailing comma after the last property or a
//comment makes the whole template invalid, even though CloudFormation YAML templates do allow comments. - Using shell or JavaScript syntax for references. Write
{ "Ref": "MyParam" }, not${MyParam}. The short-form substitutions are a YAML/string convenience, not valid JSON. - Forgetting that properties are type-specific. A property valid on one resource type may be rejected on another. When a validation error names a property, check the resource reference for that exact type rather than guessing.
- Unquoted keys or single quotes. JSON requires double quotes around every key and string value.
- Placing Outputs inside Resources or mixing sections. Sections are siblings of one another at the top level; nesting
OutputsunderResourcesmakes it look like a resource and the template fails. - Using templates over the size limit. A local template file must be under 51,200 bytes; if it grows past that, upload it to S3 and pass the URL instead.
Tips
- Pick clear logical IDs:
WebBucketreads better thanBucket1, and the logical ID appears in event logs and stack resource lists. - Keep account IDs, CIDR ranges, and other environment values in Parameters rather than hard-coding them. It makes one template usable across accounts.
- Use
Fn::Subfor strings with several substitutions; it is easier to read than nestedFn::Joinarrays. - Order the top-level sections consistently (version, description, parameters, mappings, conditions, resources, outputs). CloudFormation does not care about order, but reviewers and diff tools do.
- Set
DeletionPolicyandUpdateReplacePolicytoRetainon stateful resources when a deleted stack must not delete data. - Start small: get a single resource deploying, then add parameters and outputs. Debugging a five-resource template before the first one works is much harder than debugging an empty skeleton.
Troubleshooting
If the console rejects the file before creating any resources, the problem is the template syntax: re-run the JSON validator, check for trailing commas and smart quotes pasted from a document, and confirm the file is UTF-8. If the stack starts creating but rolls back, open the events list and find the earliest error; resource-level errors often cite the exact property or the missing permission. A parameter error usually means the value does not match AllowedPattern, AllowedValues, or type, so check the parameter definition against the input. If the error mentions the template body size, upload the file to S3 and provide the object URL. If a stack gets stuck in an update state, do not cancel the action blindly, because cancellation itself can leave resources in an inconsistent state; inspect the last event and revisit the change set. Finally, if the update reports that a resource requires replacement, check whether replacement will delete data, and take a backup or retain the resource before proceeding.
Frequently Asked Questions
Is Resources the only required section?
Yes. Every valid template must contain a Resources object, and it cannot be empty. The other standard sections are optional.
Can I write CloudFormation templates in YAML instead?
Yes. CloudFormation accepts JSON and YAML. YAML supports comments and short-form intrinsic functions, but JSON is stricter and easier to parse with common tooling.
How do I reference a parameter inside a resource property?
Use "Ref": { "Ref": "BucketSuffix" }. CloudFormation replaces it with the parameter value at deploy time.
Why does my valid JSON still fail validation?
Syntax and schema are different checks. The JSON can be well-formed while a property name, type, or required value is wrong for that resource. The error message usually names the property.
What is the maximum size of a template file?
A template supplied directly must stay under 51,200 bytes. Larger templates work when stored in S3 and referenced by URL.