Bird
Raised Fist0
AWScloud~5 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating a stack in AWS CloudFormation?
easy
A. To group related cloud resources for easy management
B. To write code for AWS Lambda functions
C. To monitor AWS billing and usage
D. To create user accounts in AWS IAM

Solution

  1. Step 1: Understand what a stack represents

    A stack is a collection of AWS resources that you manage as a single unit.
  2. Step 2: Identify the purpose of grouping resources

    Grouping resources simplifies deployment, updates, and deletion.
  3. Final Answer:

    To group related cloud resources for easy management -> Option A
  4. Quick Check:

    Stack = Group resources [OK]
Hint: Stacks group resources to manage them together [OK]
Common Mistakes:
  • Confusing stacks with user accounts
  • Thinking stacks are for billing
  • Mixing stacks with Lambda code writing
2. Which AWS CLI command is used to create a stack from a template file named template.yaml?
easy
A. aws s3 create-bucket --template template.yaml
B. aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml
C. aws cloudformation deploy --stack-name MyStack --template-file file://template.yaml
D. aws ec2 create-stack --template-file template.yaml

Solution

  1. Step 1: Identify the correct AWS service and command

    Creating stacks uses the AWS CloudFormation service and the command is create-stack.
  2. Step 2: Check the correct syntax for template file input

    The template file is specified with --template-body file://filename.
  3. Final Answer:

    aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml -> Option B
  4. Quick Check:

    Use create-stack with --template-body file:// [OK]
Hint: Use create-stack with --template-body file:// [OK]
Common Mistakes:
  • Using wrong AWS service like EC2 or S3
  • Using --template-file instead of --template-body
  • Confusing create-stack with deploy command
3. Given this AWS CLI command:
aws cloudformation create-stack --stack-name TestStack --template-body file://template.yaml --parameters ParameterKey=Env,ParameterValue=prod
What will happen when this command runs?
medium
A. The command will fail because parameters must be in JSON format
B. The stack will be created with default parameter values ignoring Env
C. The stack will be created but the Env parameter will be ignored
D. A stack named TestStack is created using the template with Env parameter set to prod

Solution

  1. Step 1: Understand parameter usage in create-stack command

    The --parameters option allows passing key-value pairs to customize the stack.
  2. Step 2: Check the syntax correctness

    The syntax ParameterKey=Env,ParameterValue=prod is valid for AWS CLI parameters.
  3. Final Answer:

    A stack named TestStack is created using the template with Env parameter set to prod -> Option D
  4. Quick Check:

    Parameters customize stack creation [OK]
Hint: Parameters use ParameterKey= and ParameterValue= pairs [OK]
Common Mistakes:
  • Thinking parameters must be JSON
  • Assuming parameters are ignored if passed
  • Confusing parameter syntax with other commands
4. You run this command to create a stack:
aws cloudformation create-stack --stack-name MyStack --template-body file://template.yaml
But you get an error: Template format error: YAML not well-formed.
What is the most likely cause?
medium
A. The template.yaml file has syntax errors in YAML formatting
B. The stack name MyStack is invalid
C. The AWS CLI command is missing the region parameter
D. The template file path is incorrect

Solution

  1. Step 1: Analyze the error message

    The error says "YAML not well-formed" which means the template file has syntax issues.
  2. Step 2: Check other possible causes

    Stack name and region issues cause different errors; file path errors cause file not found errors.
  3. Final Answer:

    The template.yaml file has syntax errors in YAML formatting -> Option A
  4. Quick Check:

    YAML errors cause template format error [OK]
Hint: Check YAML syntax carefully for formatting errors [OK]
Common Mistakes:
  • Assuming stack name causes YAML errors
  • Ignoring file path correctness
  • Forgetting to check template syntax
5. You want to create a stack that launches an EC2 instance with a parameter to choose the instance type. Your template uses a parameter named InstanceType. Which AWS CLI command correctly creates the stack with t3.micro as the instance type?
hard
A. aws cloudformation deploy --stack-name EC2Stack --template-file file://ec2.yaml --parameter-overrides InstanceType=t3.micro
B. aws cloudformation create-stack --stack-name EC2Stack --template-body file://ec2.yaml --parameters InstanceType=t3.micro
C. aws cloudformation create-stack --stack-name EC2Stack --template-body file://ec2.yaml --parameters ParameterKey=InstanceType,ParameterValue=t3.micro
D. aws ec2 run-instances --instance-type t3.micro --template-file ec2.yaml

Solution

  1. Step 1: Identify the correct command and syntax for stack creation with parameters

    The create-stack command uses --parameters ParameterKey=key,ParameterValue=value syntax.
  2. Step 2: Verify the parameter key and value match the template

    The parameter key is InstanceType and value is t3.micro, correctly formatted in aws cloudformation create-stack --stack-name EC2Stack --template-body file://ec2.yaml --parameters ParameterKey=InstanceType,ParameterValue=t3.micro.
  3. Final Answer:

    aws cloudformation create-stack --stack-name EC2Stack --template-body file://ec2.yaml --parameters ParameterKey=InstanceType,ParameterValue=t3.micro -> Option C
  4. Quick Check:

    Use ParameterKey= and ParameterValue= for parameters [OK]
Hint: Use ParameterKey= and ParameterValue= pairs for parameters [OK]
Common Mistakes:
  • Using wrong parameter syntax
  • Confusing deploy with create-stack
  • Using EC2 CLI instead of CloudFormation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard