When we want to create a new cloud formation stack, wouldn’t it will be better if we got to see/check what all resources it will create?
Yes, why not? It will be very helpful to get an idea beforehand.
Terraform also support this feature with `terraform plan` command. Please see the details here.
In AWS cloudformation it can be achieved by creating Change Sets. You can create and manage Change Sets using the AWS CloudFormation console, AWS CLI, or AWS CloudFormation API.
Change Set Overview:
Change Set Overview
So, let see the actual commands.
Create a cloud formation stack with AWS CLI:
aws cloudformation create-stack --stack-name mystack --template-body file://sample-template.json --parameters ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1\\,SubnetID2 --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM
Create Change set Before Creating stack:
aws cloudformation create-change-set --stack-name mystack --template-body file://sample-template.json --parameters --parameters ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1\\,SubnetID2 --change-set-name changeset-1 --change-set-type CREATE
To create a Change Set for a stack that doesn’t exist, for the change-set-type parameter, specify CREATE. –change-set-type CREATE – Will create the Change Set and it will show what all the resources it will create. You can view the changeset on AWS CloudFormation console or describe it through the describe-change-set action with AWSCLI.
And if you change anything on the existing stack and you want to see how the stack will get updated then specify –change-set-type UPDATE.
Create Change set for existing stack for the update:
aws cloudformation create-change-set --stack-name mystack --template-body file://sample-template.json --parameters --parameters ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1\\,SubnetID2 --change-set-name changeset-1 --change-set-type UPDATE
After the create-change-set call successfully completes, AWS CloudFormation starts creating the change set.
With AWS Console you can see the Change Sets under Change Sets tab.
Change Set view on AWS Console
If you want to execute the Change Set, means the changes shown are correct and you want to execute that changes:
Choose the Change Set that you want execute-> The AWS CloudFormation console directs you to the detail page of the Change Set -> Choose Execute.
Change Set Execute
To execute a change set (AWS CLI)
aws cloudformation execute-change-set --change-set-name changeset-1