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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront, Amazon OpenSearch, AWS DMS and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.

WRITTEN BY Kamlesh N
Comments