Lab 02: Resources

Overview

In this Lab, you will learn little bit more about CloudFormation top-level sections, including Format Version, Description, Metadata, Parameters and Resources.

Topics Covered

By the end of this lab, you will be able to:

  • Understand CloudFormation template structure and some of its sections.
  • Deploy an EC2 instance via CloudFormation.
  • Query SSM parameter store to get latest Amazon Linux AMI ID.

Start Lab

As you read through each sections, there are code samples at the end. Copy these into your own template file.

  1. Go to code/20-cloudformation-features/ directory.
  2. Open the 01-lab02-Resources.yaml file.
  3. Copy the code as you go through the topics below.

Format Version

The AWSTemplateFormatVersion section identifies the capabilities of the template. The latest template format version is 2010-09-09 and is currently the only valid value.

AWSTemplateFormatVersion: '2010-09-09'

Description

The Description section enables you to include comments about your template.

Description : CFN 101 Workshop - Lab 02 Resources.

Metadata

You can use the Metadata section to include arbitrary JSON or YAML objects. This section is useful for providing information to other tools that interact with your CloudFormation template. For example, when deploying CloudFormation templates via the AWS console, you can improve the experience of users deploying your templates by specify how to order, label and group parameters. This can be done with the AWS::CloudFormation::Interface key.

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: 'Amazon EC2 Configuration'
        Parameters:
          - InstanceType

    ParameterLabels:
      InstanceType:
        default: 'Type of EC2 Instance'

Parameters

Parameters enable you to input custom values to your template each time you create or update a stack.

AWS CloudFormation supports the following parameter types:

Type Description Example
String A literal string. “MyUserName”
Number An integer or float. “123”
List<Number> An array of integers or floats. “10,20,30”
CommaDelimitedList An array of literal strings. “test,dev,prod”
AWS-Specific Parameter Types AWS values such as Amazon VPC IDs. AWS::EC2::VPC::Id
SSM Parameter Types Parameters that correspond to existing parameters in Systems Manager Parameter Store. AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Parameters:
  InstanceType:
    Type: String
    Default: t2.micro
    AllowedValues:
      - t2.micro
      - t2.small
    Description: 'Enter t2.micro or t2.small. Default is t2.micro.'

Resources

The required Resources section declares the AWS resources that you want to include in the stack. Let’s add the EC2 resource to your stack.

Resources:
  WebServerInstance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: !Ref InstanceType
      ImageId: <replace with AMI ID ami-xxxxx>

The only required property of the EC2 resource type is ImageId. Let’s find the AMI ID via AWS console:

  1. Open AWS EC2 console
  2. Click Instances -> Launch Instance.
  3. Copy the Amazon Linux 2 AMI ami-xxxxxxxxx ID.

    Make sure to use (x86) AMI ID, if the region supports both x86 and ARM architectures.

    Expand to see the solution
  4. Once you have your AMI ID, copy and paste it to ImageId property.

You can find a working solution for the London Region in code/20-cloudformation-features/02-lab02-Resources-Solution.yaml file.

Now your EC2 template is ready to be deployed. Go back to AWS console and deploy the stack same way as you did in Lab 01: Template and Stack.

To complete this and future labs you will need Default VPC in the region you will be deploying CloudFormation templates to.
If you have deleted your default VPC, you can create a new one by following the AWS documentation for Creating a Default VPC.

  1. Open the AWS CloudFormation link in a new tab and log in to your AWS account.
  2. Click on Create stack (With new resources (Standard) if you have clicked at the top right corner).
  3. In Prepare template, choose Template is ready.
  4. In Template source, choose Upload a template file.
  5. Click on Choose file button and navigate to your workshop directory.
  6. Select the file 01-lab02-Resources.yaml.
  7. Click Next.
  8. Provide a Stack name. For example cfn-workshop-ec2.
    • The Stack name identifies the stack. Use a name to help you distinguish the purpose of this stack.
    • For Type of EC2 Instance select you preferred instance size, for example t2.micro.
    • Click Next.
  9. You can leave Configure stack options default, click Next.
  10. On the Review <stack_name> page, scroll down to the bottom and click on Create stack.

    This will create EC2 instance in your account. To check the cost of the deployed stack, click on Estimate cost on the review page.

  11. You can click the refresh button a few times until you see in the status CREATE_COMPLETE.

Challenge

In this exercise, use the AWS CLI to query the AWS Systems Manager Parameter Store the get latest Amazon Linux AMI ID.

To complete this challenge, you have to have AWS CLI configured.

Need a hint?
Want to see the solution?

Conclusion

Congratulations! You now have successfully learned how to deploy EC2 instance via CloudFormation.