Developing WebServer using EFS as Storage as a Service

* What we have to do?

Published in
5 min readJul 14, 2020

--

1. Create a Security group which allows 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 has uploaded the code into GitHub repo also the repo has some images.

6. Copy the GitHub repo code into /var/www/html.

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

Note:-

All the steps mentioned above for the task you can get in my previous article(link just below) explained in detail expect step 4, of creating EFS volume and mount to /var/www/html earlier using EBS volume.

So, before going to solve part of creating the EFS volume let’s see what is AWS_EFS and why we are using instead of EBS.

EFS

EFS is the best choice for running any application that has a high workload, requires scalable storage, and must produce output quickly. It scales automatically, even to meet the most abrupt workload spikes. After the period of high-volume storage demand has passed, EFS will automatically scale back down. EFS can be mounted to different AWS services and accessed from all your virtual machines. Use it for running shared volumes, or for big data analysis. You’ll always pay for the storage you actually use, rather than provisioning storage in advance that’s ultimately wasted.

Amazon EFS Benefits

Performance that scales to support any workload: EFS offers the throughput changing workloads need. It can provide higher throughput in spurts that match sudden file system growth, even for workloads up to 500,000 IOPS or 10 GB per second.

Energetic elasticity: Automatically scale your file system storage up or down. Remove or add files and never disturb applications. Once you make your EFS file system you can add files without worrying about storage provisioning.

Accessible file storage: On-premises servers and EC2 instances can access shared file systems concurrently. EC2 instances can also access EFS file systems located in other AWS regions through VPC peering.

Comprehensive managed service: EFS is a complete managed service, meaning your firm will never have to patch, deploy, or maintain your file system.

Cost savings: The only storage you’ll pay for is exactly what you use, as there’s no advance provisioning, up-front fees, or commitments. Moreover, you can use Lifecycle Management to transfer files that have been unused for a month to a more cost-effective storage class, which can lower expenses up to 85%.

Tighter security and compliance: You can securely access the file system with your current security solution, or control access to EFS file systems using AWS Identity and Access Management (IAM), Amazon Virtual Private Cloud (Amazon VPC), or POSIX permissions. And, EFS can encrypt your data, whether it’s in transit or at rest. This gives you dependable security and makes regulatory compliance easier.

Why to use EFS instead of EBS?

While both EBS and EFS offer great features, these two storage solutions are actually built for two completely different uses. EBS volumes are limited to a single instance, and, more importantly, then can only be accessed by one instance at a time. With EFS, you can have hundreds or thousands of instances accessing the file system simultaneously. This makes AWS EFS a great fit for any use that requires a decent performing centralized shared storage — uses like media processing or shared code repositories.

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.

EFS scales performance along with capacity, and, while this can be very beneficial in some cases, it can also be a significant drawback. You might not have high enough utilization to reach the desired throughput of the file system. Because AWS EBS provides you with steady and predictable performance, EBS is almost always a better fit, unless you require that multiple instances access your storage at the same time.

* Solution:-

  • Creating EFS volume.
# — Creating EFS volumeresource “aws_efs_file_system” “efs” {
creation_token = “efs”
performance_mode = “generalPurpose”
throughput_mode = “bursting”
encrypted = “true”
tags = {
Name = “Efs”
}
}
EFS
  • Mounting this EFS volume to the /var/www/html.
# — Mounting the EFS volumeresource “aws_efs_mount_target” “efs-mount” {
depends_on = [
aws_instance.web_server,
aws_security_group.sg,
aws_efs_file_system.efs,
]

file_system_id = “${aws_efs_file_system.efs.id}”
subnet_id = “${aws_instance.web_server.subnet_id}”
security_groups = [“${aws_security_group.sg.id}”]


connection {
type = “ssh”
user = “ec2-user”
private_key = “${tls_private_key.key1.private_key_pem}”
host = “${aws_instance.web_server.public_ip}”
}
provisioner “remote-exec” {
inline = [
“sudo mount ${aws_efs_file_system.efs.id}:/ /var/www/html”,
“sudo echo ‘${aws_efs_file_system.efs.id}:/ /var/www/html efs defaults,_netdev 0 0’ >> /etc/fstab”,
“sudo rm -rf /var/www/html/*”,
“sudo git clone https://github.com/Akashdeep-47/cloud_task2.git /var/www/html/”
]
}
}

Here, I have used the default vpc and default subnet id.

* Final outcome:-

Result of task2

For the full code of this task, please refer to my Github repository….

Feel free to give some suggestions

A warm welcome for all forks and claps.

Thank You for giving your precious time for reading this article.

--

--