0
0
AWScloud~5 mins

Creating stacks in AWS - Step-by-Step CLI Walkthrough

Choose your learning style9 modes available
Introduction
When you want to set up multiple cloud resources together as one unit, you create a stack. This helps you manage all related resources easily and keep them organized.
When you want to launch a web server with its database and network settings all at once.
When you need to update several cloud resources together without breaking dependencies.
When you want to delete all related resources safely in one step.
When you want to share your cloud setup with others using a template.
When you want to track changes to your cloud resources over time.
Config File - template.yaml
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple web server stack
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0c94855ba95c71c99
      Tags:
        - Key: Name
          Value: MyWebServer

This template defines a simple stack with one EC2 instance.

AWSTemplateFormatVersion sets the template version.

Description explains what the stack does.

Resources lists the cloud resources to create, here an EC2 instance with a small type and a common Amazon Linux image.

Commands
This command creates a new stack named 'my-web-stack' using the template file. It starts building all resources defined in the template.
Terminal
aws cloudformation create-stack --stack-name my-web-stack --template-body file://template.yaml
Expected OutputExpected
{ "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/my-web-stack/abcdefg0-1234-5678-abcd-1234567890ab" }
--stack-name - Names the stack so you can manage it later.
--template-body - Specifies the local template file to use.
This command checks the status and details of the stack to see if it was created successfully.
Terminal
aws cloudformation describe-stacks --stack-name my-web-stack
Expected OutputExpected
{ "Stacks": [ { "StackName": "my-web-stack", "StackStatus": "CREATE_COMPLETE", "CreationTime": "2024-06-01T12:00:00.000Z" } ] }
--stack-name - Specifies which stack to describe.
This command deletes the stack and all resources it created, cleaning up everything in one step.
Terminal
aws cloudformation delete-stack --stack-name my-web-stack
Expected OutputExpected
No output (command runs silently)
--stack-name - Specifies which stack to delete.
This command lists all stacks that have been successfully created, helping you see active stacks.
Terminal
aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE
Expected OutputExpected
{ "StackSummaries": [ { "StackName": "my-web-stack", "StackStatus": "CREATE_COMPLETE" } ] }
--stack-status-filter - Filters stacks by their status.
Key Concept

If you remember nothing else from this pattern, remember: a stack groups related cloud resources so you can create, update, or delete them together easily.

Common Mistakes
Using the wrong file path or missing 'file://' prefix in the template-body flag.
The CLI cannot find the template file and the stack creation fails.
Always use 'file://' before the local file path, like 'file://template.yaml'.
Trying to create a stack with a name that already exists and is active.
AWS CloudFormation will reject the request because stack names must be unique among active stacks.
Use a new unique stack name or delete the existing stack before creating a new one with the same name.
Not checking the stack status after creation.
You might think the stack is ready when it is still being created or has failed.
Use 'describe-stacks' to confirm the stack status is 'CREATE_COMPLETE' before using the resources.
Summary
Create a stack with 'aws cloudformation create-stack' using a template file.
Check the stack status with 'aws cloudformation describe-stacks' to ensure successful creation.
Delete the stack and all its resources with 'aws cloudformation delete-stack' when no longer needed.
List active stacks with 'aws cloudformation list-stacks' to see what is running.