AWS released a new feature within its CloudFormation service, enabling users to move resources between stacks. According to the company, this feature will improve modularity, organization, and alignment with changing business requirements.
Stack refactoring is a strategic approach to restructuring CloudFormation stacks that involves moving resources from one stack to another or renaming a resource with a new logical ID within the same stack. This functionality is particularly valuable for developers that aim to improve efficiency and clarity, break down large, monolithic stacks into smaller, more manageable ones, reorganize resources to better align with application architectures or organizational structures, and enhance the readability of templates by renaming the logical IDs of resources. Additionally, refactoring stacks improves resource management and aids in cost optimization through precise scaling and allocation.
Kevin DeJong, a software development engineer – Infrastructure as Code at AWS, writes:
Stack refactoring in AWS CloudFormation represents a significant advancement in infrastructure management. It offers a safer and more efficient way to reorganize your cloud resources without disruption. This feature eliminates the traditional need to remove a resource with a retain policy and then import it when restructuring stacks, helping you reduce misconfiguration risk and save time.
Using the AWS CLI to perform the stack refactoring, it would look like this:
aws cloudformation create-stack --stack-name new-stack --template-body file://after.yaml
aws cloudformation create-stack --stack-name old-stack --template-body file://before.yaml
aws cloudformation update-stack --stack-name new-stack --use-previous-template
aws cloudformation update-stack --stack-name old-stack
In comparison, AWS CloudFormation tooling like Azure CLI and Bicep, along with Terraform, have stack refactoring and infrastructure management features, each offering unique features tailored to different environments:
- Azure CLI allows for comprehensive management of Azure deployment stacks across various scopes, enabling actions on unmanaged resources,
- while Bicep, a domain-specific language for Azure Resource Manager, simplifies the creation and management of deployment stacks with its readable syntax and module support.
- In contrast, Terraform by HashiCorp provides an infrastructure-as-code approach that supports multi-cloud environments and includes refactoring capabilities through features like the moved block and terraform state mv command.
Will Dady, a tech and coding enthusiast, posted on Bluesky:
Great to see AWS has now added a “stack refactoring” feature to CloudFormation! This should prevent the need to delete-then-import to move resources between stacks, which, in my experience, has always been a butt-clenching experience.
Lastly, AWS states that the refactoring stack feature is available in all AWS regions that offer AWS CloudFormation.