DEV Community

Anjana Prasad
Anjana Prasad

Posted on

Build an Simple webserver using cloud confirmation and CFN helper Scripts.

Build an EC2 instance using cloud confirmation and CFN helper Scripts.

In this article, the author has used cloud formation and CFN helper scripts to build a simple web server in AWS. Most of the time resources in AWS we create it manually Also, it’s a painful task to recreate resources in another region quickly in disaster.
Cloud Formation, we can declarative everything based on our requirement, can re-use the same code to build resources in another region quickly.

Let’s talk a bit about the CFN helper scripts, Cfn-init script helps to make complex ec2 configuration in readable and simple manner. You can pass these configurations into the meta data section of the Cloud Formation template. Major advantage of cfn-init is it will execute the template updates in-place.

cfn-init is used to install packages start services and pull metadata to create files.

Please note if you’re planning to use cfn-init you must define AWS:: CloudFormation::Init: in the metadata section.

Cfn-init logs are in /var/log/cfn-init.log. In Amazon Linux 2 AMI AWS-cfn has already been installed.

In this CloudFormation template, the author has used parameters for reusability. Simply parameters like variables so you can change the value whenever you want.

You can get the source code here.

• AWS Account.
• IAM user with necessary permission.

Image description
Figure 1.0

Following resource will created in your aws account once you execute this template.
• An ec2 instance with httpd web server.
• A security groups with 3 inbound rules. (Port 22,80)

There are 2 ways to execute this CloudFormation template.

Method 01.
Using AWS-CLI you can deploy the code.
Before you execute below command, please make sure you have installed aws-cli in your local system.

aws cloudformation deploy --template-file ec2-with-cfn.yml --stack-name example-stack01--parameter-overrides Avalability_zones =us-east-1d

Image description
Figure 2.0

Method 02.

Login to AWS Console and Deploy the code.

Step 01.
Log in to the AWS console and type CloudFormation in the search bar.

Step 02.
Click on create stack button and select the option with new resources (Standard).

Image description
Figure 3.0

Step 03.
Once you select the above option. It will redirect to the create stack page. On this page, Select the option Template is ready since we already have the code. In Specify template option click on the Upload a template file.

Image description
_ Figure 4.0_
As shown in figure 3.0. my cloud formation template has been uploaded to the AWS S3 bucket. In this page CloudFormation will validate the source code. Etc. indentation. Finally, click on the next button.

Step 04.
Specify stack details. In this page, you can change your pre-defined parameter values as shown in the below figure.

Image description
_ Figure 5.0_

Step 05.
Configure stack options. You don’t want to change any stack configuration for deploy this simple stack. Therefore, keep its as its and click on next button.

Step 06.
You can review your stack configuration before you deploy it to your account. Once you verify everything, click on create stack button. It will start to deploy the template in your account.

Step 07.
In this step, ss you can see could formation template is creation resources. In the event section, you can see the run time events. If any error triggers, template deployment will fail. I’ll explain each tab briefly here.
• Stack info – Can get stack-related information. E.g., status, StackID, and Created date.
• Events tab – Could get stack deployment information. These event messages are very helpful when you are doing troubleshooting.
• Resources tab – You can get physical resource IDs and list of resource created in single stack.
• Output – If you have used the output function in your cloud formation template. This tab will list the output results.
• Parameter tab – It will list down all the parameters used by the CF template.
• Template tab – Could see the full cloud formation template in YAML format.
• Change set – You can apply new changes to the existing CloudFormation stack by using the change set function.

Image description
Figure 6.0

Step 08.
Click on the resource tab and click on the Physical ID of the EC2 instance. Get the public IP address of the EC2 an instance and paste it into your web browser. It will appear on our simple web server home page.

In this tutorial we discussed about CloudFormation and CFN helpers, Further, we learned how to deploy CloudFormation using console and cli in safe manner. Feel free to add your valuable comment below, if you find this tutorial useful.

Discussion (0)