Optimize AWS CloudFormation Templates
Skill for production-grade AWS CloudFormation templates: parameterization, DeletionPolicy, multi-env Conditions, and rollback recovery.
Why it matters
Deploy production-grade, cost-effective, and maintainable infrastructure on AWS using CloudFormation. This skill focuses on template optimization, stack architecture, and troubleshooting deployment failures.
Outcomes
What it gets done
Review and optimize existing CloudFormation templates for maintainability and cost.
Design and implement nested or cross-stack CloudFormation architectures.
Troubleshoot and resolve CloudFormation stack creation or update failures.
Ensure adherence to CloudFormation best practices for production deployments.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-cloudformation-best-practices | bash Overview
Cloudformation Best Practices
A skill for production-grade AWS CloudFormation templates: parameterization, stateful-resource DeletionPolicy, multi-environment Conditions, CI linting, and rollback recovery. Use for writing, reviewing, or troubleshooting CloudFormation templates; not when the user prefers CDK/Terraform or the task is application code.
What it does
CloudFormation Best Practices is a skill for writing and reviewing production-grade AWS CloudFormation templates, optimizing them for maintainability and cost, and designing nested or cross-stack architectures. Its core instructions: prefer YAML over JSON for readability, parameterize environment-specific values and use Mappings for static lookups, apply DeletionPolicy: Retain on stateful resources (RDS, S3, DynamoDB), use Conditions to support multi-environment templates from one source, validate templates with aws cloudformation validate-template before deploying, and prefer !Sub over !Join for string interpolation.
When to use - and when NOT to
Use it for writing/reviewing CloudFormation templates, optimizing existing ones, designing nested/cross-stack architectures (like a AWS::EC2::VPC resource with an environment-conditional !Sub name tag), or troubleshooting stack creation/update failures and drift. It is not the right skill when the user prefers CDK or Terraform over raw CloudFormation, or when the task is application code rather than infrastructure.
Inputs and outputs
A worked example shows a parameterized VPC template using an environment-conditional pattern:
AWSTemplateFormatVersion: "2010-09-09"
Description: Production VPC with public and private subnets
Parameters:
Environment:
Type: String
AllowedValues: [dev, staging, prod]
VpcCidr:
Type: String
Default: "10.0.0.0/16"
Conditions:
IsProd: !Equals [!Ref Environment, prod]
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub "${Environment}-vpc"
Outputs:
VpcId:
Value: !Ref VPC
Export:
Name: !Sub "${Environment}-VpcId"
Integrations
Best practices call for Outputs with Export for cross-stack references, DeletionPolicy and UpdateReplacePolicy on every stateful resource, and cfn-lint/cfn-nag wired into CI pipelines - while avoiding hardcoded ARNs/account IDs (use !Sub with pseudo parameters instead) and monolithic single-template designs. For a stack stuck in UPDATE_ROLLBACK_FAILED, the fix is continue-update-rollback with --resources-to-skip for the failing resource, followed by addressing the underlying cause.
Who it's for
Infrastructure engineers writing or maintaining AWS CloudFormation templates for stateful resources like RDS, S3, and DynamoDB who want production-grade defaults - parameterization, deletion protection, multi-environment support, and CI-linted templates - rather than ad hoc YAML.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.