Get started with ChangeSets in AWS CloudFormation

AWS CloudFormation ChangeSets are a feature provided by Amazon Web Services (AWS) that allow you to preview the changes that will be applied to your AWS resources before actually executing those changes. ChangeSets provide a way to understand the impact of proposed changes and review them for potential issues or errors.

When you make changes to a CloudFormation stack, instead of immediately applying those changes, you can create a ChangeSet, which is essentially a summary of the changes. This ChangeSet includes information about the resources that will be modified, added, or deleted, along with the details of the changes themselves.

Creating a ChangeSet involves specifying the changes you want to make to your CloudFormation stack. This can be done by updating the template, modifying parameters, or altering resource properties. Once you have defined the changes, you can create the ChangeSet using the AWS Management Console, AWS CLI, or SDKs.

After the ChangeSet is created, you can review it to understand the impact of the proposed changes. You can examine the differences between the current stack and the modified stack, ensuring that the changes align with your expectations. The ChangeSet provides a detailed summary of all the changes, highlighting any potential issues or conflicts.

During the review process, you can also choose to execute or discard the ChangeSet. If you choose to execute it, CloudFormation will apply the changes to the stack based on the instructions provided in the ChangeSet. If you discard the ChangeSet, no changes will be made to the stack, and you can make further modifications before creating a new ChangeSet.

Using ChangeSets provides several benefits. They help you avoid unexpected changes and potential disruptions to your infrastructure. You can validate and verify changes without affecting the current stack, ensuring that the modifications are correct and will produce the desired outcome. Additionally, ChangeSets promote best practices by encouraging a review process that includes multiple stakeholders, enabling collaboration and reducing the risk of errors.

Overall, AWS CloudFormation ChangeSets offer a controlled and systematic approach to managing changes to your CloudFormation stacks. They allow you to preview and validate modifications before applying them, helping you maintain the stability and reliability of your infrastructure.

Create ChangeSets via AWS Console

To create a change set for a running stack, submit the changes that you want to make by providing a modified template, new input parameter values, or both. CloudFormation generates a change set by comparing your stack with the changes you submitted.

You can either modify a template before creating the change set or during change set creation.

  1. Create a new Stack by running ec2.yaml template via AWS CLI:

     aws cloudformation create-stack --stack-name MyEC2Stack --template-body file://ec2.yaml
    
     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           ImageId: ami-06b09bfacae1453cb  # Replace with your desired AMI ID
           InstanceType: t2.micro
    
     Outputs:
       InstanceId:
         Value: !Ref MyEC2Instance
         Description: ID of the deployed EC2 instance
    

  2. In the AWS CloudFormation console, in Stacks, choose the MyEC2Stack for which you want to create a change set.

  3. In the stack details pane, choose Stack actions, and then choose Create change set for current stack.

  4. On the Create change set page, specify the location of an updated template (adds Security Group for the previously created EC2 instance), or modify the template with ec2-update.yaml:

     Resources:
       MyEC2Instance:
         Type: AWS::EC2::Instance
         Properties:
           ImageId: ami-06b09bfacae1453cb  # Replace with your desired AMI ID
           InstanceType: t2.micro
           SecurityGroupIds:
             - sg-0d3c13a93e9e34a42  # Replace with your security group ID
    
     Outputs:
       InstanceId:
         Value: !Ref MyEC2Instance
         Description: ID of the deployed EC2 instance
    
  5. On the Review stack-name page, review the changes for this change set.

  6. Choose Create change set. Specify a name for the change set and optionally specify a description of the change set to identify its purpose. Then, choose Create change set.

    You're redirected to the Changes tab of the change set's details page. While CloudFormation generates the change set, the status of the change set is CREATE_IN_PROGRESS. After it has created the change set, CloudFormation sets the status to CREATE_COMPLETE. In the Changes section, CloudFormation lists all of the changes that it will make to your stack. For more information, see Viewing a change set.

Create ChangeSets via AWS CLI

Run the aws cloudformation create-change-set command.

You submit your changes as command options. You can specify new parameter values, a modified template, or both. For example, the following command creates a change set named SampleChangeSet for the SampleStack stack. The change set uses the current stack's template, but with a different value for the Purpose parameter:

aws cloudformation create-change-set --stack-name arn:aws:cloudformation:us-east-1:123456789012:stack/SampleStack/1a2345b6-0000-00a0-a123-00abc0abc000
--change-set-name SampleChangeSet --use-previous-template --parameters ParameterKey="InstanceType",UsePreviousValue=true ParameterKey="KeyPairName",UsePreviousValue=true ParameterKey="Purpose",ParameterValue="production"

References

  1. Creating a change set

  2. Understanding change sets workshop