Sub topic 10
Creating, updating and deleting stacks
This chapter assumes you can read a YAML template, know the template sections from earlier chapters, and have the AWS CLI configured with credentials that can create the resources in your template.
What a stack is
A stack is the set of AWS resources that CloudFormation creates from one template. You manage the resources as a unit. Creating the stack provisions them, updating it changes them, and deleting it removes them. CloudFormation records each resource's physical ID, so it can find the same resources again on later operations.
The stack lifecycle
A stack moves through states that end in COMPLETE or FAILED. The IN_PROGRESS states are temporary.
Creating and updating with deploy
aws cloudformation deploy creates the stack if it does not exist and updates it if it does. You use the same command for both.
aws cloudformation deploy \
--template-file template.yaml \
--stack-name demo \
--parameter-overrides EnvName=devBehind the scenes, deploy creates a change set and executes it. If the template and parameters are unchanged, it reports that there is nothing to update. If your template creates IAM resources, add --capabilities CAPABILITY_IAM. CloudFormation refuses to create them without that acknowledgement.
Updating a stack
To update, edit the template or the parameter values and run the same command again. CloudFormation compares the new template with the stored one, finds the differences, and changes only the affected resources. Some changes happen in place. Others interrupt the resource or replace it with a new one. A later chapter covers how to predict this, and the chapter after it covers change sets for previewing the result.
aws cloudformation deploy \
--template-file template.yaml \
--stack-name demo \
--parameter-overrides EnvName=prodChanging EnvName from dev to prod in the example template turns on bucket versioning and extends the retention period.
Watching progress and reading events
Each action on each resource is recorded as a stack event. Use the events to follow an operation or to find out why it failed.
aws cloudformation describe-stack-events --stack-name demo
aws cloudformation describe-stacks --stack-name demo \
--query "Stacks[0].StackStatus"Events are listed newest first. When something fails, look for the oldest *_FAILED event, because it usually holds the root cause. Later failures are often consequences of it.
Deleting a stack
aws cloudformation delete-stack --stack-name demoCloudFormation deletes the resources in the reverse of their creation order. Deletion can fail, for example when an S3 bucket is not empty or another stack imports one of your exports. The stack then enters DELETE_FAILED. Fix the cause, such as emptying the bucket, and run the delete again.
Common errors
No updates are to be performed: the template and parameters match what is deployed. This is not a failure.AlreadyExistsExceptionon create: a resource with the same fixed name already exists. Remove the fixed name or pick another.InsufficientCapabilitiesException: add the--capabilitiesflag the message names.
Exercise
You run aws cloudformation deploy for a new stack and the first create fails. The stack is now in ROLLBACK_COMPLETE. You fix the template and run the same command again. What happens, and what should you do?
Show answer
The command fails because a stack in ROLLBACKCOMPLETE cannot be updated. Run aws cloudformation delete-stack --stack-name <name>, wait for it to finish, then run deploy again to create the stack. Before that, read the oldest *FAILED event with describe-stack-events to confirm the fix addresses the real cause.