Mounting EFS on AWS instance using Terraform
What is Terraform?
A declarative coding tool, Terraform enables developers to use a high-level configuration language called HCL (HashiCorp Configuration Language) to describe the desired “end-state” cloud or on-premises infrastructure for running an application. It then generates a plan for reaching that end-state and executes the plan to provision the infrastructure.
What is AWS EFS?
Amazon Elastic File System (Amazon EFS) provides a simple, scalable, fully managed elastic NFS file system for use with AWS Cloud services and on-premises resources. It is built to scale on demand to petabytes without disrupting applications, growing and shrinking automatically as you add and remove files, eliminating the need to provision and manage capacity to accommodate growth.
Amazon EFS is designed to provide massively parallel shared access to thousands of Amazon EC2 instances, enabling your applications to achieve high levels of aggregate throughput and IOPS with consistent low latencies.
WHY EFS IS BETTER THAN EBS:
Amazon EFS offers scalable file storage, also optimized for EC2. It can be used as a common data source for any application or workload that runs on numerous instances. Using an EFS file system, you may configure instances to mount the file system. The main differences between EBS and EFS is that EBS is only accessible from a single EC2 instance in your particular AWS region, while EFS allows you to mount the file system across multiple regions and instances.
You can also use AWS EFS to serve web content, keep various backups, and reduce storage spending. While EFS does cost more than EBS ($0.30 per GB for EFS vs. $0.10 per GB for EBS), you only pay once per EFS file system. This means that if you attach a dozen instances to it, you will still pay the same amount as if you only had one instance attached to it. With EBS volumes, you pay for each volume. Therefore, to save money on storage, EFS can sometimes serve as a replacement for EBS.
Task Description:
1. Create Security group which allow the port 80.
2. Launch EC2 instance.
3. In this Ec2 instance use the existing key or provided key and security group which we have created in step 1.
4. Launch one Volume using the EFS service and attach it in your vpc, then mount that volume into /var/www/html
5. Developer have uploaded the code into github repo also the repo has some images.
6. Copy 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
Let’s begin, first we will set AWS provider.
Here I have created variables for access key and secret key in another file and used them here. We can also directly mention here itself as a string.
STEP1: Create Security group
Here I have create a Security Group in which inbound rules allow Http, ssh and NFS protocols while in outbound rule I have written all Traffic.
STEP2: Creating EFS storage as Backup and mount target
Here I have created a EFS storage which will give us our personalized file system and to use this storage we have to mount our file system.
Provide proper subnet id and dns name for mounting. Here I have mounted /var/www/html folder.
STEP3: Launch EC2 instance
Here I have launched a EC2 instance which can be accessed using private key. Also I have attached security group which we created earlier. Also, executing shell commands in our instance and installing our necessary packages to configure web server and then mounting F.S to our folder and downloading content for web server from GitHub.
STEP4: Create S3 bucket and uploading image
STEP5: Creating CloudFront Distribution
The reason of creating CouldFront Distribution is because it is a Fast content delivery network, delivers data securely and to reduce latency as much as possible so that content is immediately accessible to the client.
After creation of CloudFront, it will provide a CloudFront URL which we will use in our website code to get the advantages of CloudFront.
STEP6: FINAL OUTPUT
Before using CloudFront URL
After using CloudFront URL
To set up the infrastructure after writing the code, in terminal we have to run few commands:
1. terraform init : this command will install all the required plugins.
2. terraform plan : this command will virtually set up the desired infrastructure without actually doing it. This will give you an idea whether what you did is working or not.
3. terraform apply : this command scans the current directory for the configuration and applies the changes appropriately.
To remove the complete infrastructure we can use “terraform destroy” command.
GitHub Link: https://github.com/ther1chie/efs_task
THANK YOU.