This repository contains a set of AWS CloudFormation templates designed to practice and demonstrate the implementation of Nested Stacks. The goal is to build a modular, reusable, and organized infrastructure by decoupling components into specialized templates.
Using Nested Stacks allows for better management of complex architectures. In this little project, a "Parent" (or "Root") stack is used to orchestrate the deployment of multiple "Child" stacks, passing parameters between them and centralizing the lifecycle of the entire infrastructure.
The project follows a modular structure:
- Root Stack: The main entry point that defines
AWS::CloudFormation::Stackresources. - Child Stacks: Individual templates for specific resources (e.g., Networking, Security Groups, EC2 instances, or S3 Buckets).
.
├── components/ # Directory for child templates
│ └── cloudwatch.yaml # WIP
│ ├── ec2.yaml # Compute layer (EC2, SSH key, SG rules)
│ ├── loadbalancer.yaml # Traffic layer (LB, WAF)
│ └── network.yaml # Compute resources (EC2, ASG, etc.)
├── root-stack.yaml # Main template to deploy all nested stacks
└── parameters/ # (Optional) Environment-specific configurations
- An AWS Account.
- AWS CLI configured with 🫵🏼 appropriate permissions.
- (Optional) An S3 Bucket to host the child templates (CloudFormation requires nested templates to be accessible via an S3 URL or file path during deployment).
There are two options to deploy the resources:
- Create a S3 Bucket.
- Upload the child templates to S3 Bucket.
aws s3 cp components/ s3://your-bucket-name/cloudformation/components/ --recursive- Update the
TemplateURLparameter in theroot-stack.yamlfile.
Nested stacks require the TemplateURL to point to a valid location.
- Deploy the Root Stack:
aws cloudformation create-stack \
--stack-name nestedPractice \
--template-body file://root-stack.yaml \
--parameters ParameterKey=BucketURL,ParameterValue=[https://your-bucket-name.s3.amazonaws.com/cloudformation/](https://your-bucket-name.s3.amazonaws.com/cloudformation/) \
--capabilities CAPABILITY_IAMOR
You can run the deploy.sh script and it will create a S3 Bucket and deploy/destroy the resources.
chmod +x ./deploy.sh
export AWS_REGION=<REGION>
./deploy.sh <RESOURCES_NAME>