DEV Community

Cover image for Deploying Elastic Compute Cloud (EC2) Instances with shared Elastic File System (EFS) Storage.
Bala Audu Musa
Bala Audu Musa

Posted on

Deploying Elastic Compute Cloud (EC2) Instances with shared Elastic File System (EFS) Storage.

Introduction

Amazon Elastic File System (EFS) is a scalable and fully managed file storage service that allows multiple EC2 instances to access shared data. It is useful for applications that require shared storage, such as web hosting, content management, and data analytics.

In this guide, I will set up an EFS on an EC2 instance in AWS. I will create an EFS file system, configure security settings, mount it on an EC2 instance, and test the setup.

Let’s get started!
Image description

The first step is to sign into your AWS IAM User Account. Search for EFS in the search bar and click on EFS under Services as shown below.

Image description

b) Click on Create File System.

Image description

c) Click on Customize.

Image description

d) Give the file system a unique name. I named mine paradise.
Under the file system type, leave in the default Regional. Leave the Automatic backups ticks. Leave the Lifecyle managment in the default. Encryption box should left in the default systems as Ticked. KMS key was left in the default. Under Performance settings, Enhanced was selected in Throughput mode (which is basically the amount of data transferred from one storage to another) which was actually the default and click on NEXT.

Image description

Image description

Image description

e) Under Network, leave the Virtual Private Cloud (VPC) in the default. Also leave all the Mount targets in the default and click on NEXT.

Image description

Image description

f) Tick the Enforce in-transit encryption for all clients under Policy options and click on NEXT.

Image description

g) This page gives the summary of all our settings. Click on create.

Image description

Image description

h) Click on your file name.

Image description

i) Click on Attach.

Image description

j) At this point our EFS has been created. We must now create our EC2 Instances, in other words, virtual machine for us to mount it. Click on the 'X' to close.

i) Go and type EC2 in the search bar. Click on it when it appears under the Services options.

Image description

k) Click on view Dashboard.

Image description

l) Click on Launch instance to create.

Image description

m) Give the instance a name

Image description

n) Leave Application and OS Images (Amazon Machine Image) in the default settings.

o) Under Instance type,select t3.micro (Free tier eligible)

Image description

p) Under key pair, click the drop down and select proceed without a key pair

Image description

q) Leave network settings in default

Image description

r) Under Firewall (security groups), leave default Create security group. We are also told that our security group name will be 'launch-wizard-1'. Leave default as Allow SSH traffic from.

Image description

s) Leave other settings as default and click on launch instance.

Image description

t) You should now see your instance. Ensure it is running and must be 3/3 checks passed.

Image description

u) Scroll down the left pane to Network & Security and Click on Security Groups

Image description

v) You should now have two security group IDs ( a default and the one you created). Then click on the default security group ID.

Image description

w) We must now edit Inbound rules by clicking on edit inbound rules. These are set of rules that control incoming traffic to an AWS resource, such as an EC2 instance, an Amazon Relational Database Service (RDS) instance, or an Amazaon Virtual Private Cloud (VPC) subnet.

Image description

x) Click on Add rule. In the just added row, search your security group in the drop-up. Ensure you have the Network File Share (NFS) selected and click on save rules.

Image description

y) Click on Instances in the left pane to display your instance. Check the box and click on Connect.

Image description

z) Leave the Connection type in default i.e. Connect using EC2 Instance Connect and click on Connect.

Image description

ai) A terminal will then open for prompt commands.

Image description

bi) Inorder to be able to mount an Amazon EFS file system on an EC2 instance, you would first install the amazon-efs-utils package using this command typed into the terminal and click on the enter key on the keyboard: sudo dnf install -y amazon-efs-utils. Once installed, we can use the utilities to mount and manage Amazon EFS file systems on our Linux virtual machine.

ci) Then we need to make a directory called efs using the command in our linus machine: sudo mkdir efs and click ENTER.

di) Run the command, sudo mount -t efs -o tls fs-0652bf7312bcdd893:/ efs. The command sudo mount -t efs -o tls fs-0652bf7312bcdd893:/ efs is used to mount an Amazon Elastic File System (EFS) to a directory on our Linux system.

ei) Run the command, df -T to display information about the file, including its type.

fi) Run the command, cd efs/. The command cd efs/ is used in Linux to change the current working directory to a directory named efs.

gi) Run the command, sudo touch mine.txt. The command sudo touch mine.txt is used to create an empty file named mine.txt or update the timestamp of an existing file with the same name.

hi) Run the command ls. The ls command in Linux and Unix systems is used to list files and directories in the current working directory. mine.txt supposed show.

ii) Run the command sudo chown ec2-user efs/.This command changes the ownership of the efs/ directory to the ec2-user, allowing that user to access and manage the directory.

ji) Run the command ls -l to see if ownerhip has really changed.

Image description

ki) We have created directory. We now need to add a file to that directory using the command, sudo touch efs.txt.

li) Run ls to display all files in a directory.

mi) Run wget https://wordpress.org/lastest.tar.gz. wget is used to get a file from the internet.

ni) Run ls -l

oi) In order to ensure two instances are accessing the EFS, We need to create another instance now using the process of creating an instance above.
We are giving our new instance the name my ideal EFS 2. leaving all the other settings in default. Our new security grou group name is launch-wizard-2, our instance type was left as t3.micro. Select proceed without a key pair under key pair name. Click on Launch instance.

Image description

pi) Go back to instances and click. You should now see two instances running ensure both are 3/3 checks passed. Then scroll down to Network & Security and click on Security Groups.

Image description

qi) We should now see 3 security groups as depicted below. We now click on the ID of the default security group.

Image description

ri) We need to now edit inbound rules. Click on edit inbound rules.

Image description

si) Click on Add rules. Select Security type as NFS in the drop-up. Search the drop-up and also select the just created security group name in this case, launch-wizard-2 and click on save rule.

Image description

ti) The screen below should now be displayed. On the left pane click on instance.

Image description

ui) Tick the box of the instance just created and click on the highlighted connect.

Image description

vi) click on connect again.

Image description

wi) The terminal below will be created in another tab.

Image description

xi) Run the command sudo dnf install -y amazon-efs-utils
yi) create a directory mkdir efs. this is in order for consistency across the EFS. This also good for autoscaling and to avoid confusion.
zi) copy your mount code, paste and run on the terminal sudo mount -t efs -o tls fs-0652bf7312bcdd893:/ efs

aii) change ownership sudo chown ec2-user efs/
bii) check df -T
cii) Enter into the file created by changing directory cd efs
dii) note that we have not created any folders here. ls -l
cii) touch index.html
dii) ls -l. we see that it has been added.
eii) using the terminal of the EC2 we created initially, run the command cd efs, then ls -l. we can see that it has accessed the file we created in our EFS 2

Image description

fii) Run the command du -h to check the space we have used

Image description

from the forgoing we have been able to create two EC2 INSTANCES TO READ and write to the same EFS.

Top comments (0)