AWS CloudFormation Fundamentals and Tutorial

In this article, I will cover the basics of CloudFormation and then demonstrate all of its benefits with an example.

The article consists of the following main sections:

What is AWS Cloud Formation?

AWS CloudFormation is a service that helps you model and configure your Amazon Web Services resources so you can spend less time managing them and more time using applications that run on AWS. You create a template that describes all the AWS resources you need (for example, Amazon EC2 or Amazon RDS DB instances), and AWS CloudFormation takes care of provisioning and configuring these resources for you.

Benefits of AWS Cloud Formation:

AWS Cloud Formation Basics

In CloudFormation, you work with templates and stacks.

Sample: A text file in JSON or YAML format that is a description of your AWS resources.

Stack: In CloudFormation, you manage resources as a single unit called a stack. All resources on the stack are defined by its template.

Therefore, the AWS CloudFormation workflow can be briefly described using the image shown here.

AWS Cloud Formation Basics

AWS Cloud Formation Basics

1. Create or use an existing template
2. Save locally or in an S3 bucket
3. Use AWS CloudFormation to create a stack based on your template. It will build and configure your stack resources.

Amazon CloudFormation documentation: docs.aws.amazon.com

Sample:

A template is a text file in JSON or YAML format that describes what resources are contained in the stack. It contains information about each resource, its configuration, and how it can be related or dependent on other resources.

A template can be developed in two ways:

1. UI Designer (interface designer): You can create a template resource map using the drag-and-drop interface and then edit them in detail using the built-in JSON/YAML editor.

2. Script (Script): Direct creation of a template script in JSON or YAML format.

As shown in the screenshot below, the designer and editor sit side by side on the console so you can visualize your changes/updates as you make them.

AWS Cloud Formation Basics

AWS Cloud Formation Basics

The main components of the template are listed below:

AWS Cloud Formation Example

AWS CloudFormation can be used to quickly set up and deploy very complex stacks, saving you hours of manual work and making it reusable. In this section, I’ll create a simple EC2 instance and a security group that I’ll then assign to the instance. A sample template for creating such a configuration (also shown in the figure below) is available at aws-cloudformation-basic-example.template.

Once the stack is deployed, all resources will be provided to it. You can access the instance via SSH and perform further configuration. As an option, you can also install apps, create files, and more. and perform any customization of EC2 instances using the CloudFormation template, as described in the sections below:

AWS Cloud Formation Basics

AWS Cloud Formation Basics

Configuring your instances:

CloudFormation can also be used to auto-configure instances after deploying them to the stack. You can run scripts, download and install applications, create files, and more.

To demonstrate this, here I provide an example of installing and running an httpd server on the EC2 instance deployed in the previous example.

To set up an EC2 instance, you can enter startup commands in the UserData section of the resource’s properties, as shown below. The complete template for this example is aws-cloudformation-http-install-userdata-only.template.

"Resources" : {
    "WebServerInstance": {
      "Type" : "AWS::EC2::Instance",
      "Properties": {
        "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, { "Fn::FindInMap" : ["AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
        "InstanceType"   : { "Ref" : "InstanceType" },
        "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
        "KeyName"        : { "Ref" : "KeyName" },
        "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
                       "#!/bin/bash\n",
                       "yum update -y\n",
                       "mkdir /tmp\n",
                       "mkdir /tmp/amol\n",
                       "yum install httpd php php-mysql stress -y\n",
                       "echo '<html><title>Hello Amol Title</title><body><h1>header H1</h1></body></html>' > /var/www/html/index.html\n",
                       "echo 'i am healthy' > /var/www/html/healthy.html\n",
                       "service httpd start\n",
                       "chkconfig httpd on\n"
        ]]}}
	}
  }
}

The best and recommended way to achieve the above functionality is to create and use Config Sets. You can create a configuration set for each operation you intend to perform on an instance. The advantage of this approach is that the configuration set information is stored in the stack metadata and can be accessed even after the stack has been created. You can create as many config sets as you want, and you don’t have to use them when you launch your instance.

For example, one can have configuration sets for installing/uninstalling WordPress, say: install_wordpress, uninstall_wordpress:

  • you can call install_wordpress only when the instance is launched, to set up and install WordPress when it is initialized on the stack.

  • If you want to re-install WordPress on this instance, you can refer to configuration sets uninstall_wordpress, install_wordpress (using a script cfn-initdescribed later) to perform a clean install after the instance is already running.

A config set can contain multiple configs, and you can call them in any order using different sets. Let’s say you have configurations named “install”, “configure”, “deploy”, “teardown”. Then you can use multiple configuration sets to achieve specific goals, as shown below:

"SetupConfigSet" : [ "install", "configure" ],
"InitialDeployConfigSet" : [ "install", "configure", "deploy" ],
"DeployOnlyConfigSet" : [ "deploy" ],
"TeardownConfigSet" : ["teardown"],

For demonstration purposes, in the example here, I will create two configuration sets with one config each. The complete template for this example is located at aws-cloudformation-httpd-install-using-configset.template.

Here I have created two configuration sets for our instance, namely ‘InstallHttpd’ and ‘UninstallHttpd’ as shown below:

First of all, in the section AWS::CloudFormation::Init define configuration sets. Each InstallHttpd and UninstallHttpd set contains one config.

"configSets" : {
            "InstallHttpd" : [ "install_httpd" ],
            "UninstallHttpd" : [ "uninstall_httpd" ]
          }

Now define the install_httpd configuration as shown below. This config has steps to install httpd server on computer. It also creates the main HTML file and ensures that the httpd server is started when the computer is turned on.

"install_httpd" : {
            "packages" : {
                "yum" : {
                    "httpd"      : [],
                    "php"        : [],
                    "php-mysql"  : [],
                    "stress"     : []
                }
            }, 
            "files" : {
                "/var/www/html/healthy.html" : {
                    "content" : "I am healthy"
                },
                "/var/www/html/index.html" : {
                    "content" : "<html><title>Hello Amol Title</title><body><h1>header H1</h1></body></html>"
                }
            }, 
            "commands" : {
                "httpd" : {
                    "command" : "chkconfig httpd on",
                    "ignoreErrors" : "false"
                }
            }, 
            "services" : {
              "sysvinit" : {
                "httpd"   : { "enabled" : "true", "ensureRunning" : "true" }
              }
            }
          }

Then define the uninstall_httpd config set as shown below. This configuration consists of sequential steps to remove the httpd server.

"uninstall_httpd" : {
            "commands" : {
                "httpd" : {
                    "command" : "rm -rf /var/www/html",
                    "ignoreErrors" : "false"
                }
            } 
          }

After the configs and configuration sets are defined, when launching the instance, we need to execute the script cfn-init to call the InstallHttpd configuration set, which will install and configure the httpd server on the instance. The cfn-init script can be used to execute any of the configuration sets during or after the instance is launched.

"UserData": {
	"Fn::Base64": {
		"Fn::Join": [
			"", 
			[
			"#!/bin/bash -xe\n",
			"yum update aws-cfn-bootstrap\n",
			"# Install the files and packages from the metadata\n",
			"/opt/aws/bin/cfn-init ",
			"    –stack", {"Ref":"AWS::StackName" },
			"    –resource WebServerInstance ",
			"    –configsets InstallHttpd ",
			"    –region", {"Ref":"AWS::Region" }, "\n",
		
			"# Signal the status from cfn-init\n",
			"/opt/aws/bin/cfn-signal -e $? ",
			"    –stack", {"Ref":"AWS::StackName" },
			"    –resource WebServerInstance ",
			"    –region", {"Ref":"AWS::Region" }, "\n"
			]
		]
	}
}

The complete template for this example is aws-cloudformation-httpd-install-using-configset.template.

After deploying the stack, your console will look like the screenshot below. As you can see, you have all the events, the inputs provided when the stack was created, the resources, and the template script. In the output, I’ve added a link to a web page hosted on the server, so if you click on it (or in this case, click on the generated URL), you’ll be taken to an HTML page.

AWS Cloud Formation Basics

If you use this template, it will install httpd and set up your web server on startup, once the stack has been created.

Now, if you want to remove httpd, you can SSH into the instance and run the command (as root):

/opt/aws/bin/cfn-init --stack [stack name/id] --resource WebServerInstance --configsets UninstallHttpd --region [region where stack is deployed]

To install and configure httpd again with the same startup configuration, run the command:

/opt/aws/bin/cfn-init --stack [stack name/id] --resource WebServerInstance --configsets InstallHttpd--region [region where stack is deployed]

You can see what is happening below:

AWS Cloud Formation Basics

AWS Cloud Formation Basics

That’s all for now. I hope you enjoyed this post and found it helpful! 🙂

In conclusion, I would like to recommend free webinar: “Mock interview with a student for the position of cloud solution architect”. During the webinar we will discuss:

  • Scary interview word and how to rearrange it to be afraid :).

  • Career path SA, CSA, teamlead, engineering manager, director of engineering.

  • What are the stages of an interview?

  • What methods are used to answer the questions.

  • Let’s discuss what Soft skills and Hard skills are.

  • Let’s simulate an interview.

Register for a free webinar

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *