0
0
AWScloud~5 mins

Updating and deleting stacks in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to change or remove your cloud resources after creating them. Updating stacks lets you change settings or add features. Deleting stacks removes all resources to keep things clean.
When you want to add a new server or database to your existing cloud setup
When you need to fix a mistake in your cloud resource configuration
When you want to remove all resources related to a project you finished
When you want to save money by deleting unused cloud resources
When you want to upgrade your application by changing its infrastructure
Config File - stack-template.yaml
stack-template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple stack with an S3 bucket
Resources:
  MyBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: example-stack-bucket-12345

This file defines a simple cloud stack with one S3 bucket named example-stack-bucket-12345. The Resources section lists what cloud resources to create. This template is used to create, update, or delete the stack.

Commands
This command creates a new stack named 'example-stack' using the template file. It sets up the resources defined in the template.
Terminal
aws cloudformation create-stack --stack-name example-stack --template-body file://stack-template.yaml
Expected OutputExpected
An error occurred (AlreadyExistsException) when calling the CreateStack operation: Stack [example-stack] already exists
--stack-name - Names the stack to create
--template-body - Specifies the template file to use
This command updates the existing stack 'example-stack' with any changes in the template file. Use this to change or add resources.
Terminal
aws cloudformation update-stack --stack-name example-stack --template-body file://stack-template.yaml
Expected OutputExpected
Waiting for stack update to complete... Successfully updated stack example-stack
--stack-name - Specifies which stack to update
--template-body - Template file with updated resource definitions
This command deletes the stack named 'example-stack' and all its resources. Use this to clean up when you no longer need the stack.
Terminal
aws cloudformation delete-stack --stack-name example-stack
Expected OutputExpected
No output (command runs silently)
--stack-name - Specifies which stack to delete
This command checks the status of the stack 'example-stack' to confirm if it still exists or was deleted.
Terminal
aws cloudformation describe-stacks --stack-name example-stack
Expected OutputExpected
An error occurred (ValidationError) when calling the DescribeStacks operation: Stack with id example-stack does not exist
--stack-name - Specifies which stack to describe
Key Concept

If you remember nothing else from this pattern, remember: update stacks to change resources and delete stacks to remove them completely.

Common Mistakes
Trying to create a stack with a name that already exists
AWS CloudFormation does not allow duplicate stack names, so creation fails
Use update-stack to change an existing stack or delete the old stack before creating a new one
Not waiting for the update or delete operation to finish before running other commands
Commands may fail or show outdated information if the stack is still changing
Wait for the operation to complete or use wait commands to check status before proceeding
Deleting a stack without confirming you no longer need its resources
You lose all resources and data in the stack permanently
Double-check the stack contents and backup any important data before deleting
Summary
Use 'aws cloudformation update-stack' to change resources in an existing stack.
Use 'aws cloudformation delete-stack' to remove all resources and the stack itself.
Check stack status with 'aws cloudformation describe-stacks' to confirm changes or deletion.