Manali Jain
6 min readJun 13, 2020

Have to create/launch Application using Terraform

1. Create the key and security group which allows the port 80.

2. Launch EC2 instance.

3. In this EC2 instance use the key and security group which we have created in step 1.

4. Launch one Volume (EBS) and mount that volume into /var/www/html

5. The developer has uploaded the code into GitHub repo also the repo has some images.

6. Copy the Github repo code into /var/www/html

7. Create an S3 bucket, and copy/deploy the images from Github repo into the s3 bucket and change the permission to public readable.

8 Create a Cloudfront using s3 bucket(which contains images) and use the Cloudfront URL to update in code in /var/www/html

ABBREVIATIONS USED

  1. AWS — Amazon Web Service
  2. EC2 — Elastic Compute Cloud
  3. EBS — Elastic Block Store
  4. S3 — Simple Storage Service
  5. IaaS — Infrastructure as a service
  6. PaaS — Platform as a service
  7. SaaS — Software as a service

NOTE -

Before starting check the environment variables that the path is given for:

  1. OpenSSH
  2. AWS
  3. Terraform
  4. git
Environment variables

AWS — Amazon Web services is a Public Cloud that provides IaaS, PaaS, and SaaS.

TERRAFORM — Terraform is created by HashiCorp.It is an open-source infrastructure as a code service. It has its own language known as HashiCorp Configuration Language. Terraform is used to make an application that is integrated with multiple clouds(like AWS, Azure, Openstack, etc).

It provides Standardisation.

Terraform file extension is “file_name.tf”.

CREATE KEY PAIRS -

COMMAND — “ssh-keygen -f key_name”

This command will generate a public key and a private key. Run this command on CMD.

ssh key generator command
keys in folder

LOGIN TO AWS IAM ACCOUNT THROUGH CMD -

COMMAND- “aws configure — profile iam_user_name

IAM user is used for the security purpose means when you share your code with others then no need to give access key and secret key.

access AWS account

TERRAFORM CODE -

VARIABLE SYNTAX -

variable “unique_name1” {

default = “name”

}

OUTPUT SYNTAX-

output “unique_name1” {

value = resource_name.unique_name

}

CREATE KEY IN AWS -

PUBLIC KEY — It is seen by everyone just like a lock is seen by everyone.

PRIVATE KEY — It is like a key that unlocks a lock and this key is with only the owner means the owner is the one who can open the lock.

The private key is used to make a connection with the instance so that only the owner can access the instance.

  1. declare variables
  2. Provide the service provider name
  3. Create key-pair

Here “variable, provider and resource ” are keywords and “aws_key_pair” is a resource name, and “enter_key_name” is a unique name.

Generate Key Pair

SECURITY GROUPS -

It is similar to a firewall that allows only those IPs which are allowed by the admin.

We want that HTTP websites access our website so the port is used.

Security Groups

CREATE THE EC2(Elastic Compute Cloud) INSTANCE -

I have created a Linux operating system with the ami_id that AWS provided.

As the provisioners can’t be used alone so with the “aws_instance” resource it is used. Here “remote-exec” is used so that HTTPD, PHP, and GIT will be installed on the cloud instance.

Tags are used to name resources.

AWS instance

DOWNLOAD GITHUB CODE TO LOCAL DIRECTORY -

“git clone” is used to download the images from GitHub.

Download Github Images

CREATE EBS VOLUME AND ATTACH IT TO THE EC2 INSTANCE -

EBS (Elastic Block Store ) is a Block Storage. Here, 1GiB hard disk is attached to the EC2 instance.

Force_detach is used when the hard disk is busy in the mounting process and we want to remove it.

Create and Attach Volume

CREATE S3 BUCKET AND S3 BUCKET OBJECT -

S3(Simple Storage Service) is an Object Storage.acl is publicly readable. here images is that folder which has all the GitHub files which are downloaded earlier. S3 contains the ”open.png” image.

S3 Bucket

CREATE CloudFront DISTRIBUTION AND INTEGRATE IT WITH S3 -

Cloudfront creates the CDN which reduces the delay time. Here CloudFront is integrated with the S3.

CloudFront

FORMAT AND MOUNT THE ATTACHED HARDDISK -

Before copying or saving data to hard disk, it is formatted and mounted.Also before copying data of GitHub to /var/www/html/ folder see that /var/www/html folder is empty. The CloudFront path is copied to the path.txt file so that in the index.php code the code will be changed at runtime.

PHP FILE CODE-

Github code is in /var/www/html folder and path.txt is also in /var/www/html folder so the image path will be replaced by the URL.

TERRAFORM COMMANDS —

  1. Go inside the folder where the terraform file is saved and install the plugins that are required by the command “terraform init”.

2. Now use “terraform apply” to run the services.

3. See one file named “.terraform.tfstate.lock.info” is generated when the command run.

OUTPUT -

4. If you want to destroy the services you applied the run command “terraform destroy” and if you want terraform do not ask for yes or no then run “terraform destroy -auto-approve”.

TERRAFORM COMMANDS -

“terraform init” -> install the required plugins and initialize the terraform.

“terraform apply” -> run the program

“terraform destroy” -> delete all the things that are running.

“terraform apply — auto-approve” -> run the program without asking yes/no.

“terraform destroy — auto-approve” -> delete all the things that are running without asking yes/no.

THANK YOU FOR READING.

MY GITHUB CODE

Manali Jain
Manali Jain

Written by Manali Jain

You are the only one person who can Change Yourself…

No responses yet