Launching Website Using Terraform Code (AWS , EFS)

Samkit Shah
3 min readJul 15, 2020

In my previous articles we’ve launched website using terraform code and AWS and storage = EBS. In this article we are going to launch website using EFS (Elastic File Storage) . If you haven’t seen my previous article click here.

To implement this everything would remain the same we just have to change the replace our volume with EFS. Let’s see how’s it done.

Created a Provider :
A provider is responsible for understanding API interactions and exposing resources. I’m using IAM user “samkit” and region where I want to run is mention below.

provider “aws” {
region = “ap-south-1”
profile = “samkit”

Created a Security group:

A security group acts as a virtual firewall for your instance to control incoming and outgoing traffic. Edited which traffic webserver or protocol can access my website.

resource “aws_security_group” “tsg” {
name = “myfirewall”
description = “Allow inbound traffic”
vpc_id = “vpc-41f0ed29”
ingress {
description = “HTTP”
from_port = 80
to_port = 80
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
ingress {
description = “SSH”
from_port = 22
to_port = 22
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”]
}
}

Created an Instance:

An instance is a virtual server in the AWS cloud. With Amazon EC2, you can set up and configure the operating system and applications that run on your instance. Output prints my instance IP Address.

resource “aws_instance” “os1” {
ami = “ami-0447a12f28fddb066”
instance_type = “t2.micro”
key_name = “key1”
security_groups = [“myfirewall”]
tags = {
Name = “OsFromTerraform”
}
}
output “OsId” {
value = aws_instance.os1.id
}

Create EFS Volume:

One advantage of using EFS over EBS is that we can attach one EFS to multiple Instances.

resource "aws_efs_file_system" "foo" {
creation_token = "my-product"

tags = {
Name = "MyProduct"
}
}

data "aws_efs_mount_target" "by_id" {
file_system_id = aws_file_system.foo.id
subnet_id = aws_instance.os1.subnet_id
security_groups = ["${aws_security_group.tsg.id}"]
}
resource “null_resource” “nullremote3” {
depends_on = [
aws_efs_mount_target.by_id,
]
connection {
type = “ssh”
user = “ec2-user”
private_key = file(“C:/Users/91811/Downloads/key1.pem”)
host = aws_instance.os1.public_ip
}
provisioner “remote-exec” {
inline = [
“sudo yum install httpd php git -y”,
“sudo systemctl restart httpd”,
“sudo systemctl enable httpd”,
]
}
provisioner “remote-exec” {
inline = [
“sudo mkfs.ext4 /dev/xvdd”,
“sudo mount /dev/xvdd /var/www/html”,
“sudo rm -rf /var/www/html/*”,
“sudo git clone https://github.com/samkit-jpss/MultiCloud.git /var/www/html/”
]
}
}
output “myos_ip” {
value = aws_instance.os1.public_ip
}

After when you are done with your your HCL code , save the file and in command prompt type and run :

terraform validate 
terraform apply -auto-approve

Finally our website has been launched

Thanks for reading !

--

--

Samkit Shah
Samkit Shah

Written by Samkit Shah

Machine Learning | Deep Learning | DevOps | MLOps | Cloud Computing | BigData

No responses yet