0
0
AwsHow-ToBeginner · 3 min read

How to Update Stack in AWS CloudFormation: Step-by-Step Guide

To update a stack in AWS CloudFormation, provide a new or modified template and specify the stack name using the UpdateStack action. You can update stacks via the AWS Management Console, AWS CLI, or SDK by submitting the updated template and any changed parameters.
📐

Syntax

The basic syntax to update a CloudFormation stack using AWS CLI is:

  • aws cloudformation update-stack --stack-name <stack-name> --template-body file://<template-file> --parameters ParameterKey=<key>,ParameterValue=<value>
  • --stack-name: The name or ID of the stack to update.
  • --template-body: The path to the updated CloudFormation template file.
  • --parameters: Optional key-value pairs to update stack parameters.
bash
aws cloudformation update-stack --stack-name MyStack --template-body file://updated-template.yaml --parameters ParameterKey=InstanceType,ParameterValue=t2.micro
Output
An update is initiated for stack 'MyStack'. You can monitor progress in the AWS Console or with 'aws cloudformation describe-stacks'.
💻

Example

This example updates a stack named MyWebApp by changing the instance type parameter in the template.

bash
aws cloudformation update-stack \
  --stack-name MyWebApp \
  --template-body file://webapp-template.yaml \
  --parameters ParameterKey=InstanceType,ParameterValue=t3.small
Output
An update is initiated for stack 'MyWebApp'. You can check the update status with 'aws cloudformation describe-stacks --stack-name MyWebApp'.
⚠️

Common Pitfalls

  • Template errors: The update fails if the new template has syntax errors or invalid resources.
  • Immutable properties: Some resource properties cannot be changed; attempting to update them causes failure.
  • Missing parameters: Omitting required parameters or providing wrong values can cause update errors.
  • Stack drift: Manual changes outside CloudFormation can cause unexpected update results.

Always validate your template with aws cloudformation validate-template before updating.

bash
aws cloudformation validate-template --template-body file://updated-template.yaml
Output
Template is valid.
📊

Quick Reference

Tips for updating CloudFormation stacks:

  • Always back up your current template and parameters before updating.
  • Use --capabilities CAPABILITY_NAMED_IAM if your template creates or modifies IAM roles.
  • Monitor stack events in the AWS Console or with describe-stack-events to track progress.
  • Use change sets to preview updates before applying them.

Key Takeaways

Use the AWS CLI or Console to update a stack by providing the updated template and parameters.
Validate your template before updating to avoid errors.
Some resource properties cannot be changed; check documentation to avoid update failures.
Use change sets to preview changes safely before applying updates.
Monitor stack events to track update progress and troubleshoot issues.