Creating and Mounting EFS in ‘html’ Folder for high Availability using Terraform.

Terraform is the best Infrastructure As Code tool in the current market. In my previous post I Created a complete web page setup using terraform. The best part of terraform is minimal human interference so no more errors :-p.

In this post I will use the previous setup for setting up the web page. But instead of attaching a EBS volume I will go for attachment of EFS. Which will give High availability and unlimited storage. So in case our Web server crashes or the availability zone goes offline we still have two other zones which will give 100% availability.

So, without wasting time in theory, let’s jump into the setup.

First step first, Create the provisioner. Create a terraform file named provider.tf and add these lines,

The next step is two create the keypair and upload that to AWS. In terminal type,

To upload the key using terraform we need to write the below terraform code in our keypair.tf file.

Now, it’s time to create the security group which will allow port 22(SSH), 80(HTTP) and 2049(NFS). For simplicity I am opening these ports to all i.e. anywhere but in real time this need to be specific IP range for better security. Create a terraform file named sg.tf and add the below content.

Let’s create the EFS configuration in terraform file. Create a terraform file named efs.tf and update the below content.

Finally, let’s create the EC2 instance. In that instance we will first install ‘git’, ‘httpd’ service. And mount the EFS after installation. Cloning the ‘git’ into the /var/www/html folder. So, create a terraform file named ec2.tf and put the below content.

It’s time to create the CloudFront Distribution which is also known as Content Delivery Network. Create a terraform file for CloudFront and name that as cf.tf.

The create a terraform file for creating and uploading content to ‘S3’. Give the file name as s3.tf.

The final step is to download an image in jpg format and rename that to image.jpg.

Now our complete setup is ready. just run the below command and see the magic. :-)

The terraform apply will return an IP, Goto browser and paste that IP. If everything is perfect then it will open the web page.

All the terraform files can be found here,
https://github.com/saumik8763/efs-terraform