Template Structure: Resources, Parameters, Outputs, Conditions
A CloudFormation template is a JSON or YAML file that defines AWS infrastructure.
Resources is the only required section. Every AWS resource goes here. Parameters make templates reusable by accepting input values at deploy time. Outputs export values for use in other stacks. Conditions control resource creation based on parameter values.
Example template sections:
Resources define actual AWS resources like VPCs, EC2 instances, and security groups. Parameters accept values like environment name and instance type. Outputs expose values like VPC IDs and endpoint URLs. Conditions enable or disable resources based on parameter values.
Use intrinsic functions like Ref, Sub, Join, and GetAtt to reference resources and construct dynamic values. These functions let you create templates that adapt to different environments and inputs.
Stack Sets for Multi-Account Deployments
Stack Sets deploy CloudFormation templates across multiple AWS accounts and regions from a single template. They are essential for enterprise governance.
Service-managed permissions use AWS Organizations to automatically deploy to member accounts. When you add a new account to an OU, Stack Sets automatically deploys the template.
Self-managed permissions require creating IAM roles in target accounts. Use this when you do not have Organizations access.
Deployment preferences control rollout speed with MaxConcurrentPercentage and FailureTolerancePercentage settings. Stack Sets are ideal for deploying baseline security controls, logging configurations, and networking standards across all accounts.
Change Sets and Drift Detection
Change Sets let you preview changes before applying them. They show which resources will be created, modified, or deleted. Review the Change Set carefully, especially for Delete actions, before executing.
Drift detection identifies resources that have been modified outside of CloudFormation. Drifted resources show the actual versus expected configuration. You can revert drift by updating the stack, or import the drifted values into CloudFormation to reconcile.
Best practice: always use Change Sets for production deployments and run drift detection regularly to ensure your infrastructure matches your templates.
Nested Stacks and Custom Resources
Nested stacks are CloudFormation stacks created from within another stack. They help organize complex templates into manageable pieces. The parent stack manages the nested stack lifecycle.
Custom resources extend CloudFormation to manage resources it does not natively support. They use Lambda functions or SNS topics to create, update, or delete resources. Common uses include creating Route 53 records, configuring third-party services, or populating initial data.
Macros transform templates before deployment. They let you create custom syntax that expands into standard CloudFormation, reducing repetition and enabling domain-specific abstractions.
Quiz
1. What is the only required section in a CloudFormation template?
2. What does a CloudFormation Change Set show?
3. What is the benefit of service-managed Stack Sets?
Flashcards
Question
What are the main sections of a CloudFormation template?
Click to reveal answer
Answer
Resources (required), Parameters (inputs), Outputs (exports), Conditions (conditional logic).
Question
What is a CloudFormation Stack Set?
Click to reveal answer
Answer
Deploys a template across multiple AWS accounts and regions from a single operation.
Question
What is a nested stack?
Click to reveal answer
Answer
A stack created from within another stack to organize complex templates into manageable pieces.
Question
What is a CloudFormation custom resource?
Click to reveal answer
Answer
An extension using Lambda or SNS to manage resources not natively supported by CloudFormation.
Revision Notes
Key Takeaways
- 1. Resources is the only required template section
- 2. Stack Sets enable multi-account, multi-region deployment
- 3. Change Sets preview changes before execution
- 4. Drift detection finds manual modifications to resources
Interview Tips
- • Explain the difference between parameters, mappings, and conditions
- • Describe deploying a template across 50 accounts using Stack Sets
- • Walk through the Change Set workflow for production deployments
Cheat Sheet
Resources (required), Parameters (inputs), Outputs (exports), Conditions (logic). Stack Sets: multi-account deployment. Change Sets: preview before execute. Drift: detect manual changes. Nested Stacks: break templates into pieces. Custom Resources: extend with Lambda.