0
0
AwsHow-ToBeginner · 4 min read

How to Create a Stack in AWS CloudFormation: Step-by-Step Guide

To create a stack in AWS CloudFormation, use the AWS Management Console or the AWS CLI with a valid CloudFormation template in JSON or YAML format. The stack defines your cloud resources and their configurations, which CloudFormation provisions and manages as a single unit.
📐

Syntax

The basic syntax to create a stack using AWS CLI is:

  • aws cloudformation create-stack --stack-name <stack-name> --template-body file://<template-file>
  • You can also specify parameters, tags, and capabilities as needed.

Key parts explained:

  • --stack-name: The name you give your stack.
  • --template-body: The path to your CloudFormation template file.
  • --parameters: Optional, to pass input values to the template.
  • --capabilities: Required if your template creates IAM resources.
bash
aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml --capabilities CAPABILITY_NAMED_IAM
💻

Example

This example shows how to create a simple CloudFormation stack that provisions an S3 bucket using AWS CLI.

yaml
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MyBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: my-unique-bucket-123456789

# Command to create the stack:
# aws cloudformation create-stack --stack-name MyS3Stack --template-body file://s3bucket.yaml
Output
An AWS CloudFormation stack named 'MyS3Stack' is created, provisioning an S3 bucket named 'my-unique-bucket-123456789'.
⚠️

Common Pitfalls

Common mistakes when creating CloudFormation stacks include:

  • Using an invalid or incorrectly formatted template causes stack creation to fail.
  • Not specifying --capabilities CAPABILITY_NAMED_IAM when your template creates IAM roles or policies.
  • Choosing a bucket name that is not globally unique for S3 buckets.
  • Forgetting to provide required parameters if your template expects them.

Example of missing capabilities error:

bash
# Wrong (missing capabilities):
aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml

# Right (with capabilities):
aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml --capabilities CAPABILITY_NAMED_IAM
📊

Quick Reference

Command OptionDescription
--stack-nameName of the CloudFormation stack to create
--template-bodyPath to the JSON or YAML template file
--parametersInput parameters for the template (optional)
--capabilitiesAcknowledges IAM resource creation (e.g., CAPABILITY_NAMED_IAM)
--tagsKey-value pairs to tag the stack (optional)

Key Takeaways

Use a valid JSON or YAML CloudFormation template to define your resources.
Create stacks via AWS CLI or Management Console by specifying stack name and template.
Include --capabilities flag when your template creates IAM resources.
Ensure resource names like S3 buckets are globally unique to avoid errors.
Check template syntax and required parameters before creating the stack.