Lab - Automation With CloudFormation
Lab - Automation With CloudFormation
Deploy an AWS CloudFormation stack with a defined Virtual Private Cloud (VPC), and
Security Group.
Configure an AWS CloudFormation stack with resources, such as an Amazon Simple
Storage Solution (S3) bucket and Amazon Elastic Compute Cloud (EC2).
Terminate an AWS CloudFormation and its respective resources.
Duration
Wait until you see the message "Lab status: ready", then click the X to close the
Start Lab panel.
This will open the AWS Management Console in a new browser tab. The system will
automatically log you in.
Tip: If a new browser tab does not open, there will typically be a banner or icon
at the top of your browser indicating that your browser is preventing the site from
opening pop-up windows. Click on the banner or icon and choose "Allow pop ups."
Arrange the AWS Management Console tab so that it displays along side these
instructions. Ideally, you will be able to see both browser tabs at the same time,
to make it easier to follow the lab steps.
Task 1 deployment
The Parameters section is used to prompt for inputs that can be used elsewhere in
the template. The template is asking for two IP address (CIDR) ranges for defining
the VPC.
The Resources section is used to define the infrastructure to be deployed. The
template is defining the VPC, and a Security Group.
The Outputs section is used to provide selective information about resources in
the stack. The template is providing the Default Security Group for the VPC that is
created.
The template is written in a format called YAML, which is commonly used for
configuration files. The format of the file is important, including the indents and
hyphens. CloudFormation templates can also be written in JSON.
In the Parameters section, you will see that CloudFormation is prompting for the IP
address ('CIDR') range for the VPC and Subnet. A default value has been specified
by the template, so there is no need to modify these values.
Click Next
The Options page can be used to specify additional parameters. You can browse the
page, but leave settings at their default values.
Click Next
The Review page displays a summary of all settings. Some of the resources are
defined with custom names, which can lead to naming conflicts. Therefore,
CloudFormation prompts for an acknowledgement that custom names are being used.
The listing shows (in reverse order) the activities performed by CloudFormation,
such as starting to create a resource and then completing the resource creation.
Any errors encountered during the creation of the stack will be listed in this tab.
The listing shows the resources that are being created. CloudFormation determines
the optimal order for resources to be created, such as creating the VPC before the
subnet.
Wait until the status changes to CREATE_COMPLETE. You can click Refresh
occasionally to update the display.
Optional: Go to the VPC console to see the Lab VPC that was created. Then, return
to the CloudFormation console.
Rather than following pre-defined steps, you will need to discover how to update
the template yourself!
You should edit the task1.yaml file you downloaded earlier to include an Amazon S3
bucket
Use this documentation page for assistance: Amazon S3 Template Snippets
Look at the YAML example
Your code should go under the Resources: header in the template file
You do not require any Properties for this bucket resource
Indents are important in YAML — use two spaces for each indent
The correct solution is actually only needs two lines — one for the identifier and
one for the Type
Once you have edited the template, continue with the following steps to update the
stack.
Click Update.
Choose Replace current template, then choose Upload a template file. Click Choose
file, then browse to and select the task1.yaml file that you modified.
Click Next
If you receive an error message, ask your instructor for assistance in debugging
the problem.
Wait for CloudFormation to calculate the changes. Towards the bottom of the page,
you should see something similar to this:
Preview Changes
This indicates that CloudFormation will Add an Amazon S3 bucket. All other
resources defined in the template will be unchanged. This demonstrates that it is
fast and easy to add additional resources to an existing stack, since those
resources do not need to be redeployed.
Click Update stack
The bucket will now be displayed in the list of resources. CloudFormation will have
assigned it a random name so that it does not conflict with any existing buckets.
If the bucket was not correctly created, please ask your instructor for
assistance.
Optional: Go to the S3 console to see the bucket that was created. Then, return to
the CloudFormation console.
Whereas the bucket definition was rather simple (just two lines), defining an
Amazon EC2 instance is more complex because it needs to use associated resources,
such as an AMI, security group and subnet.
First, however, you will add a special parameter that is used to provide a value
for the Amazon Machine Image (AMI).
AmazonLinuxAMIID:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
This parameter uses the AWS Systems Manager Parameter Store to retrieve the latest
AMI (specified in the Default parameter, which in this case is Amazon Linux 2) for
the stack's region. This makes it easy to deploy stacks in different regions
without having to manually specify an AMI ID for every region.
For more details of this method, see: AWS Compute Blog: Query for the latest Amazon
Linux AMI IDs using AWS Systems Manager Parameter Store.
When writing CloudFormation templates, you can refer to other resources in the
template by using the !Ref keyword. For example, here is a portion of the
task1.yaml template that defines a VPC, then references the VPC within the Route
Table definition:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Note that it uses !Ref VPC to refer to the VPC resource. You will use this
technique when defining the EC2 instance.
Use the tips below to update the template to add an Amazon EC2 instance with the
following Properties:
Preview Changes
If you are experiencing difficulties in editing the template, please ask your
instructor for assistance.
Optional: Go to the EC2 console to see the App Server that was created. Then,
return to the CloudFormation console.
The stack will show DELETE_IN_PROGRESS. After a few minutes, the stack will
disappear.
Optional: Verify that the Amazon S3 bucket, Amazon EC2 instance and the VPC have
been deleted.