AWS

4 Mins Read

AWS CloudFormation: Simplifying Stack Refactoring for Evolving Architectures

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
Get Started Now

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
Enroll Now

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 PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFrontAmazon OpenSearchAWS DMS and many more.

To get started, go through our Consultancy page and Managed Services PackageCloudThat’s offerings.

WRITTEN BY Kamlesh N

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!