Deepanshu mishra
2 min readOct 9, 2020

--

Mounting EFS to /var/www/html for high availability, using terraform.

aws EFS for high availability

Terraform makes it easier to create,destroy and automating most of the operations.

In last article i hosted a web page with full end to end automation using terraform.

In this article i gonna mount EFS to the ‘html’ folder instead of creating EBS volume and mounting it.so, if our web-server goes down due to some unknown or known reason, or Az goes down we will still have two zones available as backup.

so, let’s get into the code.

we will be maintaining our terraform code for each purpose inside a separate file so things don’t get messed up in the last.

we will create a file named as ‘provider.tf

now create a key-pair and upload it to aws, to do so we need to type the following command in the terminal

> ssh-keygen -t rsa -f thekey

now to upload this key pair to aws we will be creating a file named keypair.tf with following code:

Now, we will a create a security group which will only allow port 22 for SSH, 80 for HTTP and 2049 for NFS.

For this create a file named securitygroup.tf with the following content:

Now we will create EFS setup in a file named efs.tf

with the following content:

Now at last we will launch ec2 instance and inside the instance will install httpd and git services, using remote provisioner and mount the EFS after installation. cloning git inside ‘/var/www/html’ . for this we will be creating a file named ec2.tf.

Now, we will create a CloudFront(CDN) distribution , create a file named as cloudfront.tf with following content:

Now , create a s3 bucket to upload content to it using terraform code , for this create a file named s3.tf with the below content .

Now at last we will choose any image(in jpg format) and name it as image.jpg
after this run the below commands:

$ terraform init
$ terraform plan
$ terraform apply — auto-approve

Everything is ready now if we have done each step correct then pasting an IP in browser ,which is returned by the terraform code will give the webpage .😉

--

--