Gaurav Kumar
3 min readJun 15, 2020

--

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

Task:

Creation of complete infrastructure for hosting a web page on AWS Cloud using Terraform.

Steps:

1. Create the key and security group which allow the port 80(for HTTP) and Port No 22 (for SSH).

2. Launch EC2 instance with key and security group which have created in first step.

3. Configure the O.S. so that it can be used to host a web page. Install Apache Web Server, PHP and start the required services.

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

5. Developer have uploaded the code into github repo also the repo has some images.

6. Clone the github repo code into /var/www/html.

7. Create 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.

Code:

provider “aws” {

region = “ap-south-1”

}

resource “aws_security_group” “my_sg” {

name = “my_sg”

description = “Allow TLS inbound traffic”

vpc_id = “vpc-7a4a5612”

ingress {

description = “HTTP”

from_port = 80

to_port = 80

protocol = “tcp”

cidr_blocks = [“0.0.0.0/0”]

}

egress {

from_port = 0

to_port = 0

protocol = “-1”

cidr_blocks = [“0.0.0.0/0”]

}

tags = {

Name = “my_sg”

}

}

resource “aws_instance” “myinstan” {

ami = “ami-005956c5f0f757d37”

instance_type = “t2.micro”

key_name = “mykey”

security_groups = [ “my_sg” ]

connection {

type = “ssh”

user = “ec2-user”

private_key = file(“C:/Users/gaurav/Desktop/mykey.pem”)

host = aws_instance.myinstan.public_ip

}

provisioner “remote-exec” {

inline = [

“sudo yum install httpd php git -y”,

“sudo systemctl restart httpd”,

“sudo systemctl enable httpd”,

]

}

tags = {

Name = “linuxos1”

}

}

resource “aws_ebs_volume” “ebs1” {

availability_zone = aws_instance.myinstan.availability_zone

size = 1

tags = {

Name = “ebs”

}

}

resource “aws_volume_attachment” “ebs_att” {

device_name = “/dev/sda2”

volume_id = aws_ebs_volume.ebs1.id

instance_id = aws_instance.myinstan.id

force_detach = true

}

output “my_ip” {

value = aws_instance.myinstan.public_ip

}

resource “null_resource” “nulllocal2” {

provisioner “local-exec” {

command = “echo ${aws_instance.web.public_ip} > publicip.txt”

}

}

resource “null_resource” “nullremote1” {

depends_on = [

aws_volume_attachment.ebs_att,

]

connection {

type = “ssh”

user = “ec2-user”

private_key = file(“C:/Users/gaurav/Desktop/mykey.pem”)

host = aws_instance.myinstan.public_ip

}

provisioner “remote-exec” {

inline = [

“sudo mkfs.ext4 /dev/xvdh”,

“sudo mount /dev/xvdh /var/www/html”,

“sudo rm -rf /var/www/html/*”,

“sudo git clone https://github.com/Gaurav3d46/Hybrid/blob/master/index.html"

]

}

}

resource “null_resource” “nulllocal1” {

depends_on = [

null_resource.nullremote1,

]

provisioner “local-exec” {

command = “chrome ${aws_instance.web.public_ip}”

}

Terminal commands:

# To initialize the plugins
Terraform init

# To validate the configuration file in the directory
Terraform validate

# To create the infrastructure
Terraform apply

#To destroy the infrastructure
Terraform destroy

Outputs:

my_ip = 35.154.53.24

--

--