For queries and issues please write to us at support@whizlabs.com | My Account

Whizlabs Blog

Knowledge Hub for Project Managers & Tech Geeks

AWS Certification : How to Create and Use AWS CloudFormation Templates?

     -     Apr 6th, 2017   -     AWS Certifications   -     1 Comment

Are you preparing for AWS Certified Developer Associate certification exam? In this space, we are writing series of articles on topics which are covered in the developer associate certification exam. In this article, we are explaining one of the important concept AWS CloudFormation. You would expect lot of questions from this topic for developer associate exam. You can subscribe to us for receiving the further updates on this topic.

How to create templates in Cloud Formation?

Developer Associate certification exam is the most easiest exam in the associate level, it is good idea to start your certification from this exam. Also we would recommend you to pass the solutions architect certification exam which covers the concepts of all the services offered in AWS.

 

Developer Associate Syllabus

Here is the snapshot of blueprint for developer associate is covered in the developer associate exam.

aws-developer-associate-blueprint

Amazon CloudFormation

Amazon Cloudformation helps developers and other IT professional to design templates that can be used to create aws resources. It’s like infrastructure as code. AWS Cloudformation includes the following features

  1. Its simple to use. It has a web interface which allows one to easily manage and work with aws resources.
  2. It supports the creation of a wide range of aws resources, right from EC2 resources to Elastic Load Balancer.
  3. You can create templates which is infrastructure as code. These templates can be re-used to create resources for multiple environments.
  4. One can customize the template and creation of resources via parameters.
  5. There is a Visual editor which allows one to have a visual representation of resources. One can then drag and drop resources and create the template accordingly.

Structure of a CloudFormation Template

A cloudformation template has the following structure

{
	"AWSTemplateFormatVersion" : "version date",
	"Description" : "JSON string",
	"Metadata" : 
	{
		template metadata
	},
	"Parameters" : 
	{
		set of parameters
	},
	"Mappings" : 
	{
	    set of mappings
	},
	"Conditions" : 
	{
	    set of conditions
	},
	"Transform" : 
	{
	    set of transforms
	},
	"Resources" : 
	{
	    set of resources
	},
	"Outputs" : 
	{
		set of outputs
	}
}

The description of the different section of the template are given below

  • Format Version – This specifies the AWS CloudFormation template version that the template conforms to.
  • Description – A text string that describes the template.
  • Metadata – Objects that provide additional information about the template.
  • Parameters – Specifies values that you can pass in to your template at runtime.
  • Mappings – A mapping of keys and associated values that you can use to specify conditional parameter values, similar to a lookup table. You can match a key to a corresponding value by using the Fn::FindInMap intrinsic function in the Resources and Outputs section.
  • Conditions – Defines conditions that control whether certain resources are created or whether certain resource properties are assigned a value during stack creation or update.
  • Transform – For serverless applications (also referred to as Lambda-based applications), specifies the version of the AWS Serverless Application Model (AWS SAM) to use.
  • Resources – Specifies the stack resources and their properties, such as an Amazon Elastic Compute Cloud instance or an Amazon Simple Storage Service bucket.
  • Outputs – Describes the values that are returned whenever you view your stack’s properties.

One of the key aspects for the Developer exam is to know how to setup cloudformation templates. So let’s look at the basic ways to create cloudformation templates

Basic CloudFormation Template

In this example, we will look at how to automate the creation of an EC2 instance from a particular AMI ID

  • Creating a template – Log into your AWS Console. You will see CloudFormation under the Management tools section.
  • Management tools Click on Cloudformation. In the next screen click on Design template. This will allow you to create a custom template for cloudformation.

AWS Cloudformation- Template

  • The template designer will show up. You have the option to either create the template via the designer or to create it via the JSON editor.

AWS Cloudformation- Template designer

  • As an AWS Developer, you need to know the JSON structure for cloudformation templates. So let’s add the following code to the template designer.
{
"Resources" : {
   "MyEC2Instance" : {
       "Type" : "AWS::EC2::Instance",
           "Properties" : {"ImageId" : "ami-e5a51786"}
         }
     }
}

The resources section tells the cloudformation template that we want to work with an aws resource.

  • Next we are defining a variable for our resource which is called MyEC2Instance
  • Next we want the type of resource to be a AWS::EC2::Instance
  • And finally we want our instance to be of the image id ami-e5a51786. Note that this image id is in the Singapore region. It is also an AMI for a windows instance.

You can add the code to the template section.

AWS Cloudformation- code

Once done , you can click on the Validate template option in the left hand corner.  It’s the icon with the tick mark.

AWS Cloudformation- Validate Template

  • Next click on the cloud like button to create a stack from the code
AWS Cloudformation- Create Stack

AWS Cloudformation- Create Stack

  •  In the next screen, you can accept the default settings and click on the next button.

AWS Cloudformation- Template

  • Next you just need to give a name for the stack and click on the Next button.

AWS Cloudformation - Stack Name

In the next screen you have the ability to add a role or a tag. For now you can leave the default settings as it is and click on the Next button.

AWS Cloudformation - Add Role

  • In the final screen, you can click on the Create button

AWS Cloudformation - Create

  • If you now go to your EC2 dashboard, you will see one instance now running. This has been created by the Cloudformation template.

AWS Cloudformation - Running Instance

  • And now if you go back to the cloudformation template , you will see that the status is complete.

AWS Cloudformation - Status Completion

Note: – If any resource creation in the template fails, the entire template is rolled back.

 CloudFormation Functions

Cloudformation has support for functions known as intrinsic functions. Some of the key functions are given below

  • Fn::FindInMap – The intrinsic function Fn::FindInMap returns the value corresponding to keys in a two-level map that is declared in the Mappings section.
  • Fn::GetAtt – The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template.
  • Fn::GetAZs – The intrinsic function Fn::GetAZs returns an array that lists Availability Zones for a specified region.
  • Fn::Join – The intrinsic function Fn::Join appends a set of values into a single value, separated by the specified delimiter. If a delimiter is the empty string, the set of values are concatenated with no delimiter.

Let’s look at an example of using a function

  • Again let’s create a cloudformation stack, but this time let’s add the below code
{
   "Resources": {
      "MyEC2Instance": {
          "Type": "AWS::EC2::Instance",
              "Properties": {
                 "ImageId": "ami-e5a51786"
               }
           }
      },
   "Outputs": {
       "Availability": {
           "Description": "The Instance ID",
            "Value":
       { "Fn::GetAtt" : [ "MyEC2Instance", "AvailabilityZone" ]}
      }
   }
}

Now in the above stack, we are using the Fn::GetAtt function to get the AvailabilityZone as an output in which the resource is created.

AWS Cloudformation - Fn:GetAtt

  •  Now create a stack based on the above template with the same settings, but this time you can give a  different name for the stack.

Now when the template creation is done, and you see the output tab, you will see the AZ as an output in which the EC2 instance is created.

AWS Cloudformation - Output

Final Points to Remember About CloudFormation

  • Cloudfromation has a web interface which allows one to easily manage and work with aws resources.
  • It supports the creation of a wide range of aws resources, right from EC2 resources to Elastic Load Balancer.
  • You can create templates which is infrastructure as code. These templates can be re-used to create resources for multiple environments.
  • If any resource creation in the template fails, the entire template is rolled back.
  • Cloudformation has the support for functions known as intrinsic functions. Some of them are given below
    • Fn::FindInMap
    • Fn::GetAtt
    • Fn::GetAZs
    • Fn::Join

Summary

In this article we have explained about how to create and use AWS CloudFormation Templates. This is one of the most important service offered by Amazon Web Services (AWS) that will help the developers to model the templates for their AWS resources . You would expect lot of questions from this topic in your certification exam. 

If you are preparing for the AWS certifications exam and looking for any help, please send us a mail or call to our customer support team. 

 


There is 1 comment


  • 4 months ago

    Khadijah Celestine   /   Reply

    This test is next. Great Article! Thanks!


Your Comment

Your email address will not be published.




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>