Voiced by Amazon Polly |
AWS CloudFormation now offers stack refactoring, a feature streamlining the process of reorganizing resources within your stacks. This new capability allows you to move resources between stacks, split large stacks into smaller units, and rename resource logical IDs—all of which help align your infrastructure with changing architectural and operational requirements.
Previously, such changes required manual intervention, such as updating templates and importing resources into new stacks. Stack refactoring simplifies this by previewing changes before execution, allowing you to confirm that the updates meet your needs. Once confirmed, you can apply the refactor with an atomic workflow, making the process faster and safer and ensuring continuous adaptability as your system evolves. This feature is especially valuable in scenarios where you want to:
- Break up a large, monolithic stack into smaller, more manageable components
- Rearrange resources to better align with your application’s architecture or organizational needs
- Update the logical IDs of resources to enhance the readability of your templates
Let’s understand stack refactoring with the help of a small, hands-on example:
Scenario 1: Update the logical IDs of resources to enhance the readability of your templates
Consider the CloudFormation template shown in Figure 1, which creates an s3 bucket named mybucket-at-aws-135.
Let’s create a CloudFormation stack using AWS CLI:
View your resource in the CloudFormation console in the resource tab of the stack named “
Now, to rename the resource logical Id, create a JSON File as shown in Figure 4 and YAML file as shown in Figure 5 file with new logical resource id.
To update the logical IDs of resources and enhance the readability of your templates, refactor the stack using “create-stack-refactor,” as shown in Figure 6.
The above command will produce stack-refactor-id,
Use the stack-refactor-id to execute the stack refactoring using the command shown in Figure 7.
Scenario 2: Rearrange resources to better align with your application’s architecture or organizational needs or break up a monolithic stack into small manageable modules
Consider you have a stack deployed with compute (EC2) and storage (S3) resources, and you need to refactor the stack with compute resources (EC2) and Storage resources in a separate stack.
Initial Stack with resources created using CloudFormation template shown in Figure 10.
Template in Figure 10, deployed using below command.
To separate EC2 and S3 resources, move the S3 resource into a separate stack using CloudFormation refactoring, and create two CloudFormation templates, with names EC2-Template.yaml and S3-Template.yaml as shown in Figure 12 and 13.
To refactor the stack, use the commands shown Figure 14, parameter “–enable-stack-creation”, will create a new stack “MyS3Bukcet” and will move resource s3 to this stack.
On Executing command in Figure 14, will create a stack-factor-id, use that refactor id to execute the stack refactoring using command shown in Figure 16.
Earn Multiple AWS Certifications for the Price of Two
- AWS Authorized Instructor led Sessions
- AWS Official Curriculum
Conclusion
Stack refactoring in AWS CloudFormation marks a major improvement in managing infrastructure, providing a safer and more efficient method to reorganize cloud resources without causing disruption. This feature removes the need for the traditional approach of deleting a resource, applying a retain policy, and then re-importing it during stack restructuring. As a result, it helps minimize the risk of misconfigurations and saves valuable time. In this post, you’ve seen two scenarios: one to improve the readability of the template by changing the logical name of the resource and the second to break existing stack, move the storage resource (S3) to the new stack to separate and manage the same compute resource (EC2) and Storage Resource (S3) across different stacks, created more focused stack. Also note, stack refactoring being a new feature announced recently , it comes with some limitations and, as of now, works solely in the CLI and SDK.
Transform Your Career with AWS Certifications
- Advanced Skills
- AWS Official Curriculum
- 10+ Hand-on Labs
About CloudThat
CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

WRITTEN BY Kamlesh N
Comments