AWS CloudFormation lets you define your entire infrastructure—servers, databases, networks, and more—as code in a template file. Instead of clicking through the AWS console to launch each resource manually, you create a stack that provisions everything together, consistently, every time. This guide walks through the resources you need and the exact steps to create your first stack, whether you use the console, the CLI, or a template file.

What You'll Need

  • An AWS account with permissions to create CloudFormation stacks and the resources you're deploying (e.g., EC2, S3, IAM roles).
  • A template file (JSON or YAML) that defines your resources. If you don't have one, you can use a sample template from the AWS documentation or the one in the steps below.
  • AWS CLI installed and configured (optional, for CLI steps). If you use the console, you don't need the CLI.
  • Basic familiarity with the AWS services you're deploying—knowing what an EC2 instance or S3 bucket is helps.

Step-by-Step

  1. Choose your template. A CloudFormation template is a text file that describes the resources you want. The simplest template has a Resources section. Here's a minimal YAML example that creates an S3 bucket:
    Resources:
      MyBucket:
        Type: AWS::S3::Bucket
    Save this as template.yaml.
  2. Open the CloudFormation console. Log in to your AWS account and navigate to CloudFormation. The URL is https://console.aws.amazon.com/cloudformation.
  3. Create a new stack. On the CloudFormation console, click Create stack (the button might say Create stack with a dropdown—choose With new resources (standard)).
  4. Specify the template. In the Create stack wizard, under Prerequisite - Prepare template, choose Template is ready. Then for Template source, select Upload a template file and upload your template.yaml. Alternatively, you can paste the template into the Amazon S3 URL field if it's stored in S3, or use Create template in Designer to build one visually.
  5. Click Next. On the Specify stack details page, enter a Stack name (e.g., my-first-stack). Stack names must be unique in your AWS account and region. If your template defines parameters, you'll see them here—fill in any required ones.
  6. Configure stack options (optional). Click Next to go to the Configure stack options page. You can add tags (key-value pairs) to help identify resources, set IAM roles, or configure advanced options like termination protection. For a first stack, you can leave everything default and click Next.
  7. Review and create. On the Review page, check that the stack name and template are correct. Scroll down and click Submit. CloudFormation now starts creating the resources.
  8. Watch the stack creation. The stack will show status CREATE_IN_PROGRESS. Click the stack name to see the Events tab, which lists each step (e.g., "S3 bucket created"). When the status changes to CREATE_COMPLETE, your resources are live.
  9. Verify the resource. Go to the service you created (e.g., S3) and confirm the bucket exists. You can also see the resource in the CloudFormation Resources tab.
  10. Update or delete the stack. To make changes, edit your template and use Update stack (same wizard) or delete the stack to remove all resources—CloudFormation cleans up everything it created.

If you prefer the CLI, the equivalent commands are:

aws cloudformation create-stack --stack-name my-first-stack --template-body file://template.yaml

Then check status with aws cloudformation describe-stacks --stack-name my-first-stack.

Common Mistakes

  • Forgetting to specify a region. CloudFormation stacks are regional. If you create a stack in us-east-1, it won't appear in eu-west-1. Always check the region selector in the console or add --region to CLI commands.
  • Invalid template syntax. A missing comma in JSON or a wrong indentation in YAML will fail. Validate your template before uploading—you can use the Validate template button in the console or aws cloudformation validate-template.
  • Resource dependencies not declared. If one resource needs another (e.g., an EC2 instance needs a security group), CloudFormation may create them in the wrong order. Use the DependsOn attribute or reference the resource's logical ID (like !Ref MySecurityGroup) so CloudFormation knows the order.
  • Hardcoding values that should be parameters. Putting an AMI ID or a password directly in the template makes it hard to reuse. Use Parameters and Mappings instead.
  • Not using change sets before updating. When you update a stack, CloudFormation may replace resources (e.g., if you change an EC2 instance type, it might recreate the instance). A change set shows you what will change before you apply it, avoiding surprises.

Tips

  • Start with a sample template. AWS provides many sample templates in the console (choose Use a sample template when creating a stack) and in the documentation. They show best practices.
  • Use YAML. YAML is more readable and supports comments, which helps when you revisit templates later.
  • Organize with nested stacks. For large infrastructures, break your template into reusable nested stacks (e.g., a network stack, an app stack). Use the AWS::CloudFormation::Stack resource.
  • Leverage output values. Use the Outputs section to expose useful information like the endpoint URL of a load balancer. You can view outputs in the console or use them in other stacks.
  • Version control your templates. Store them in Git. You can then track changes and roll back if needed.
  • Use AWS::CloudFormation::Init for EC2 configuration. Instead of scripting setup in user data, use the Metadata section with AWS::CloudFormation::Init to install packages and start services—it's more robust and CloudFormation can signal when it's done.

Troubleshooting

Stack creation fails. Go to the Events tab and look for the first red CREATE_FAILED event. The error message usually tells you exactly what's wrong (e.g., "Bucket name already exists"). Fix the issue in your template and retry. You can also click View logs if there are associated logs.

Resource creation hangs. If a resource like an EC2 instance is stuck in CREATE_IN_PROGRESS, it might be waiting for a signal. This often happens with CreationPolicy or UserData scripts that don't complete. Check the instance's system log in EC2 to see if the script is failing.

Permissions errors. If you get an error like "API: s3:CreateBucket Access Denied," your IAM user/role lacks permission. Ensure you have cloudformation:CreateStack and permissions for the specific resources (e.g., s3:CreateBucket). Ask your admin to update your IAM policy.

Template validation fails. Use the validate-template CLI command or the console's validate button. The error will point to the line number—fix the syntax.

Stack deletion fails. Sometimes a resource can't be deleted (e.g., an S3 bucket that isn't empty). CloudFormation will show the error. You may need to manually empty the bucket or delete the resource from the console, then retry the stack deletion. Be careful—deleting a stack permanently removes resources.

Changes not applying. If you update a stack and nothing changes, check whether the update actually modifies a property that CloudFormation tracks. Some properties are immutable; you may need to replace the resource.

Frequently Asked Questions

What is the difference between a CloudFormation template and a stack?

A template is the JSON/YAML file that defines your resources. A stack is the live set of resources created from that template. You can create multiple stacks from the same template (e.g., one for dev, one for prod).

Can I use CloudFormation to manage existing resources?

Yes, you can import existing resources into a stack using the Import resources into stack feature. You need to write a template that matches the existing resource's configuration, and the resource must be importable (most AWS resources are).

How do I pass parameters to my template?

Define parameters in the Parameters section of your template. When you create or update a stack, the console prompts you for values, or you can pass them via CLI with --parameters ParameterKey=KeyName,ParameterValue=MyKey.

What is a change set and when should I use it?

A change set is a preview of the changes that will occur when you update a stack. It shows what will be added, modified, or deleted. Use it before updating a stack to avoid unexpected resource replacements.

Can I use CloudFormation with Terraform?

They are separate tools. Terraform is multi-cloud and uses its own configuration language. You can't directly mix them, but you can manage some resources with CloudFormation and others with Terraform, though it adds complexity. Choose one for a given environment.

How do I see the cost of a stack before creating it?

CloudFormation itself doesn't show costs, but you can use the CloudFormation template with AWS Pricing Calculator or check the Cost Explorer after creation. Some third-party tools also estimate costs from templates.