CloudFormation Template Practical
Build on your understanding of CloudFormation by running templates.
Understanding CloudFormation templates is crucial for the exam. In this practical, we’ll review and run some templates to deploy AWS infrastructure.
Practical
In the widget below, we’ll perform the following steps to demonstrate our knowledge of AWS CloudFormation:
- Create a security group using the
security_group.yaml
file and export the security group ID as a CloudFormation export. - Create an EC2 instance using the
ec2_instance.yaml
file. This file automatically imports the ID of the security group created above from CloudFormation exports.
Note: You need to know all the YAML CloudFormation codes used in the practical below. You may need to enter Q in the terminal if it gets stuck on long outputs.
ec2_instance_stack_name=`cat ec2_instance_stack_name.txt` security_group_stack_name=`cat security_group_stack_name.txt` echo "Deleting CloudFormation stacks" aws cloudformation delete-stack --stack-name ${ec2_instance_stack_name} aws cloudformation wait stack-delete-complete --stack-name ${ec2_instance_stack_name} aws cloudformation delete-stack --stack-name ${security_group_stack_name} aws cloudformation wait stack-delete-complete --stack-name ${security_group_stack_name} echo "Resources destroyed."
Note: Please use the
dos2unix destroy.sh && sh destroy.sh
command to destroy resources. Notice that in thedestroy.sh
file, we don’t delete individual resources (like EC2 instances or security groups). We delete the CloudFormation stacks, which automatically deletes all the resources they created.
The following image shows ...