In this Lab, you will learn little more about CloudFormation top-level sections, including Format Version, Description, Metadata, Parameters and Resources.
By the end of this lab, you will be able to:
As you read through each section, there are code samples at the end. Copy these into your own template file.
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.
The Description section enables you to include comments about your template.
Description : CFN 101 Workshop - Lab 02 Resources.
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 enable you to input custom values to your template each time you create or update a stack.
AWS CloudFormation supports the following parameter types:
|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.'
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:
Make sure to use (x86) AMI ID, if the region supports both x86 and ARM architectures.
You can find a working solution for the London Region in
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.
This will create EC2 instance in your account. To check the cost of the deployed stack, click on Estimate cost on the review page.
In this exercise, use the AWS CLI to query the AWS Systems Manager Parameter Store the get the latest Amazon Linux AMI ID.
To complete this challenge, you have to have AWS CLI configured.
Check out the AWS Compute Blog to find out.
Copy the code below to your terminal. Make sure to change the
--region flag to use a region that you are deploying your CloudFormation to.
aws ssm get-parameters \ --names /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 \ --query "Parameters.Value" \ --region eu-west-2 \ --output text
Congratulations! You now have successfully learned how to deploy EC2 instance via CloudFormation.