Create AWS CloudFormation stack using sample template
Tags: aws, cloud, cloudskills, cloudnative
Published: 2022-01-02 13:31:10

This post will cover the following content:
1. What is AWS CloudFormation?
2. Why should you use it?
3. How to use it?

Let's get started...

## What is AWS CloudFormation?
AWS CloudFormation comes under the AWS M&G category and it helps in provisioning and configuring AWS resources for you.
You just need to create a CloudFormation template to describe what AWS resources you would like to configure along with the properties/settings, and CloudFormation will create them for you as you described.

## Why should you use it?
You should use AWS CloudFormation if you are looking to:

-  **Simplify infrastructure management**
Instead of creating and managing individual resources in AWS, you can just a CloudFormation stack to easily manage a collection of resources as a single unit in order to save your time and effort.

- **Quickly replicate your infrastructure**
You can reuse your CloudFormation template to provision the same resources in multiple regions without spending time in configuring those resources in each region individually.

- **Easily control and track changes to your infrastructure**
CloudFormation templates are nothing but text files which helps you to track changes to your infrastructure just like version controlling. You can easily roll back to your previous infrastructure using a version control system with templates.

## How to use it?
AWS CloudFormation is a regional service and you can use AWS CloudFormation via the browser console, command line tools, or APIs to create stack and resources.

### Below are the steps to get started:

- Go to the AWS CloudFormation Console 

- Click on `Create stack` in the top right corner

- Specify the Template. 

A template is a JSON or YAML file that contains configuration information about the AWS resources you want to include in the stack.

There are 3 options available for you to choose:

1. **Template is ready** - If you have your own template file, then choose this option.
2. **Use a sample template** - If you would like to use a sample template file provided by AWS, you should go with this option.
3. **Create template in Designer** - If you are new to AWS CloudFormation, you can use this option to create, view, and modify AWS CloudFormation templates using a drag-and-drop interface.

I am selecting a sample template `LAMP Stack` to begin with CloudFormation. 

**S3 URL**: This is the Amazon S3 bucket URL which is the location of your CloudFormation template file

> AWS CloudFormation Sample Template LAMP_Single_Instance: Create a LAMP stack using a single EC2 instance and a local MySQL database for storage. This template demonstrates using the AWS CloudFormation bootstrap scripts to install the packages and files necessary to deploy the Apache web server, PHP and MySQL at instance launch time. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.

This is how our template will look like in AWS CF Designer tool

![LAMP stack view in Designer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sb428falc9gks5vbr99f.png)

- Now, Specify stack details

**Stack name**: mention a stack name of your choice which can include letters (A-Z and a-z), numbers (0-9), and dashes (-).

**Parameters** : You can use the given parameters to input customer values while creating or updating the stack.
**DBName** : Database name for your MySQL DB 
**DBPassword** : Password for your MySQL DB 
**DBRootPassword** : Root password for MySQL DB 
**DBUser** : User name for your MySQL DB access 
**InstanceType** : Specify the EC2 instance type which you would like to use for your WebServer
**KeyName** : Specify an existing EC2 KeyPair to enable SSH access to the instance. 
Make sure you have an existing EC2 keypair created in the specified region. If you do not have one, please create it using the EC2 console and specify the newly created keypair here otherwise stack creation will fail.
**SSHLocation** : The IP address range that can be used to SSH to the EC2 instances (by default, it is 0.0.0.0/0)

- Next, Configure Stack Options

All the given parameters are optional. You can fill them as per your requirements.

**Tags** : key-value pairs to apply to resources in your stack
**Permissions** : Specify IAM role name/ARN. By default, CloudFormation uses permissions based on your user credentials.
**Stack failure Options** : Specify the behavior when stack creation fails
**Advanced Options** : You can use these options if you would like to define stack policies for designated resources, setup notification, enable stack termination protection, or configure rollback options. 

- Now comes, Review step. You can review all the details of your CloudFormation stack and if everything looks great, click `Create stack` at the bottom right corner to initiate stack creation.

_PS: At the bottom, there is a `Quick-create link` option available for you to use. You can create more stacks with the same basic configuration as the one you just created using this URL._

- Once the stack creation has completed, you should see the CloudFormation stack status as `CREATE_COMPLETE`

![stack details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ff8jjr021x2cy4x31qp.png)

**Resource tab** : You should find all the resources created by CF stack under this tab.

![Stack resource tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr1swm87kuavv0bwh3wz.png)

**Event tab** : You can check the CF stack event details under event tab
![Stack event tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x81sdthivmoiovam5ii9.png)

**Outputs** : Under this tab, you should see the website URL for your newly created LAMP stack. 

Hope you find this information helpful. Thank you.