Cloud formation tools are the utilities that let you define your cloud infrastructure as code and then create, update and delete it repeatably. The shortest working path is: install the AWS CLI or use the console, write a YAML or JSON template, validate it, deploy it as a stack, and then update or delete the stack rather than editing resources by hand. This guide uses AWS CloudFormation because it is the reference implementation, but the same ideas apply to Terraform, Azure Resource Manager and Google Cloud Deployment Manager.
What You'll Need
- An AWS account with permission to create the resources in your template (IAM, EC2, S3 and so on).
- The AWS CLI installed and configured, or access to the AWS Management Console in a browser.
- A text editor that understands YAML or JSON, with indentation visible.
- A version control folder for your templates, even if it is just a local Git repository.
- An S3 bucket if any template is larger than the inline limit or uses nested stacks.
Step-by-Step
- Decide what the stack should own. Write down the resources you want CloudFormation to create, such as a VPC, a subnet, a security group and an EC2 instance. Anything you create by hand outside the stack will not be tracked, so plan to move it in or leave it out.
- Install and configure the AWS CLI. Install it from the official AWS download page, then run
aws configureand enter your access key, secret key, default region and output format. Confirm it works withaws sts get-caller-identity, which should return your account and user ARN. - Create the template file. Save a file named
stack.yaml. Every template needs aResourcessection; the other top-level sections are optional. A minimal template that creates one S3 bucket looks like this:AWSTemplateFormatVersion: '2010-09-09', thenResources:, then a logical ID such asMyBucket:, thenType: AWS::S3::Bucket. Logical IDs must be alphanumeric and unique inside the template. - Add parameters instead of hard-coding values. Declare a
Parameterssection for things that change between environments, such as an instance type or a bucket name prefix. Reference them with!Refin YAML. This keeps one template usable for dev and production. - Validate the template before deploying. Run
aws cloudformation validate-template --template-body file://stack.yaml. This catches syntax and structural errors but not every semantic problem, so read the output carefully. Labels and flags can differ slightly between CLI versions, so checkaws cloudformation validate-template helpif a flag is rejected. - Deploy the stack. Run
aws cloudformation create-stack --stack-name my-first-stack --template-body file://stack.yaml --capabilities CAPABILITY_IAMonly if your template creates IAM resources; otherwise omit the capabilities flag. In the console, choose Create stack, With new resources, upload the template, name the stack, review and submit. - Watch the events. Run
aws cloudformation describe-stack-events --stack-name my-first-stackor open the Events tab. Each resource moves through CREATE_IN_PROGRESS to CREATE_COMPLETE, and the first failure is the one to read. - Update rather than edit by hand. Change the template, then run
aws cloudformation update-stackwith the same stack name and new template. CloudFormation works out which resources change. Some changes require replacement, which deletes and recreates the resource; the change set preview tells you this before you commit. - Use change sets for anything important. Run
aws cloudformation create-change-set, inspect the proposed changes, then execute the change set. This is the safest way to see whether a small template edit will replace a database or a load balancer. - Delete the stack when you are done. Run
aws cloudformation delete-stack --stack-name my-first-stack. Deletion removes every resource the stack created. Resources with aDeletionPolicy: Retainattribute are kept, so check for that before deleting anything holding data.
Common Mistakes
The most frequent error is editing resources in the console after the stack created them. The next stack update can then fail or overwrite your manual change, because CloudFormation compares the template to its own record, not to what is currently live. Another common mistake is hard-coding account-specific values such as AMI IDs or bucket names; these break when you deploy to another region or account. People also forget that stack names must be unique within a region and that a failed create leaves a stack in ROLLBACK_COMPLETE state, which cannot be updated and must be deleted first. Finally, many templates fail simply because a referenced security group or subnet is created later in the file; use DependsOn or !Ref so the order is explicit.
Tips
Keep templates small and split shared infrastructure into nested stacks or separate stacks that export values with Outputs and Export. Store templates in Git and review diffs before every update. Use a linter such as cfn-lint to catch problems before they reach AWS. Name resources with a prefix parameter so you can tell environments apart. Add a short description and comments to each template so the next person knows what it owns. For anything holding data, set a deletion policy and take a backup before you delete the stack.
Troubleshooting
If a stack fails to create, open the Events tab and read the earliest failure, not the last one. A message about insufficient permissions means the IAM user or role running the stack lacks an action the template needs. A message about a resource already existing usually means a name is hard-coded and collides with something outside the stack; make the name a parameter or remove the explicit name so CloudFormation generates one. If validation passes but deployment fails, check region-specific values such as AMI IDs. If an update fails and rolls back, the stack returns to its previous state; fix the template and run the update again rather than deleting the stack. If a delete fails because a resource is in use, remove the dependency, such as a non-empty S3 bucket, and retry. When the console and CLI disagree, confirm you are pointed at the same region with aws configure get region.
Frequently Asked Questions
Is CloudFormation free?
CloudFormation itself has no charge for creating stacks. You pay only for the resources the stack creates, such as EC2 instances or S3 storage.
Can I use JSON instead of YAML?
Yes. Both formats are supported, and the console accepts either. YAML is usually easier to read and supports short-form tags such as !Ref.
What is the difference between a stack and a template?
A template is the file describing resources. A stack is the deployed instance of that template in a region, with its own name and state.
Can I edit a resource that CloudFormation created?
You can, but the change is not tracked and may be overwritten on the next update. Make the change in the template and update the stack instead.
How do I share values between stacks?
Use the Outputs section with an Export name in one stack, then import it in another with !ImportValue. Exported values cannot be deleted while another stack imports them.
Does CloudFormation work with other clouds?
No, it is specific to AWS. Terraform, Azure Resource Manager and Google Cloud Deployment Manager are the equivalents elsewhere, and the template-and-stack workflow is broadly the same.