Search⌘ K
AI Features

CloudFormation Template Practical

Explore how to create and deploy AWS infrastructure using CloudFormation templates. This lesson helps you understand the syntax and structure of YAML templates, how to define parameters, resources, and outputs, and the process to automate EC2 instance and security group provisioning with practical examples. Gain hands-on experience managing stacks and learn key troubleshooting tips.

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."
CloudFormation practical

Note: Please use the dos2unix destroy.sh && sh destroy.sh command to destroy resources. Notice that in the destroy.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 ...