Create and connect Linux Instances to an AWS Elastic File System (EFS)

Create and connect Linux Instances to an AWS Elastic File System (EFS)

Amazon Elastic File System (EFS) is a fully managed, highly available and durable storage management system designed to provide serverless, and fully elastic file storage that lets users share file data without the need to provision or manage storage capacity and performance.

In this guide, I will walk through the step-by-step process of creating an EFS, and Linux instances, and mount a file system in the instances. We then look at how files are replicated across the instances and how to remount a file system when an instance is restarted.

Step 1: Create a security group

  1. In the EC2 Dashboard, click Security Groups. On the Security Groups page, click Create Security Group
  2. Enter a Security group name and description and select the VPC. For Inbound rules, click the Add rule and

·???????? Type – SSH

·???????? Source – Anywhere-IPv4

·???????? CIDR blocks – 0.0.0.0/0

3. Click Create Security Group


4. In the created security group, click on Edit inbound rules in the Inbound Rules tab

5.?????? Click on Add rule and

·???????? Type – NFS

·???????? Source – Custom

·???????? Security Groups – Select the name of the created security group


6. Click Save rules

Step 2: Create Linux EC2 Instances

1.?????? In the EC2 Dashboard, in the Launch instance area, click Launch instance

·???????? Name and tags – Enter a name for the instance

·???????? Application and OS Images (Amazon Machine Image) – select Amazon Linux 2023 AMI

·???????? Instance Type – t2.micro

·???????? Key pair (login) – select an appropriate Key pair or create a new key pair

·???????? Network settings – click Edit, select VPC, subnet – us-east-1a, auto-assign public IP enable, select existing security group – select the security group created in step 1

2.?????? Click Launch Instance

3.?????? Repeat the same steps to create a second instance however, select us-east-1b for the subnet


Step 3. Create an EFS file system

1.?????? Open the EFS service and click Create file system

2.?????? Enter a name and select the VPC to place the EFS in


3.?????? Click Customize

4.?????? In the File system settings, under File system type, select

·???????? Regional – Offers the highest level of availability and durability

·???????? One Zone – Provides continuous availability within a single availability zone in a region

5.?????? Automatic backups – choose to either enable this or disable it (enabled by default)


6.?????? Lifecycle management – moves files into the Infrequent Access or Archive storage class

·???????? Transition into Infrequent Access (IA) – 30 day(s) since last access (default)

·???????? Transition into Archive – 90 day(s) since last access (default)

·???????? Transition into Standard – None (default)

7.?????? Encryption – enable encryption of data at rest


8.?????? Performance settings –

·???????? Throughput mode – Enhanced or Bursting

·???????? Elastic (Recommended)

·???????? Provisioned


9.?????? Click Next

10.?????? In the Network access settings

·???????? Network – Ensure the selected VPC is retained

·???????? Mount targets – Choose the availability zones where you want the mount target to reside and the security group created for the instances Remove those that are not needed.


11. In the Review and Create page, click Create


Step 4: Update, mount and connect to EFS from Linux Instances

1.?????? In the Linux instances, check for updates

·???????? Sudo yum -y update

2.?????? Make a directory in each of the instances using the same name

·???????? Mkdir HumanResources

?

3.?????? Install EFS utils in each of the instances

·???????? Sudo yum install -y amazon-efs-utils

4.?????? Mount the created directory using the EFS mount helper. Open the created file system, click on Attach, and Mount via DNS is selected by default. Copy the strings on the EFS mount helper, and return to the default page. Copy the DNS name in the general settings page. Combine the strings to look like this

?

5.?????? In each of the Linux instances, run the command in the image to mount the created directory.

6.?????? Change the directory into the mounted directory and create a folder or download a file


7.?????? Switch to the other instance to view the mounted directory and file that was downloaded

8.?????? On the EFS system page, on the Monitoring tab, view the file system metrics to confirm activity in the EFS

?

Step 5: Automatically mount the EFS on Linux Instance when restarted

To automatically mount the directory when an instance is started or rebooted, follow these steps

1.?????? Create an IAM role for the EC2 instance using the AWS service trusted entity type. Open IAM, click Roles and Create a role

2.?????? In Selected trusted entity, under Trusted entity type select AWS service, and under Use case select EC2 – Allows EC2 instances to call AWS services on your behalf


3.?????? Under Add permissions, in the search box, enter EC2, choose AmazonEC2FullAccess, click Next

4.?????? In the Role details, enter a role name, and click Create role

5.?????? In the EC2 instance dashboard, check the Instance you want to apply the IAM role to. Go to Actions ->, Security ->, Modify IAM role

6.?????? On the Modify IAM role page, in the drop-down, select the IAM role that was created and click Update IAM role


7.?????? In this instance, edit the fstab file using an editor

·???????? Sudo nano /etc/fstab

·???????? Run the command file-system-id:/ efs-mount-point efs _netdev,noresvport,tls,iam 0 0

file-system-id = The ID for your Amazon EFS file system

efs-mount-point = The mount point for the EFS file system on your EC2 instance

efs = The type of file system

·???????? Save and exit the fstab file

8.?????? Run the command sudo mount -fav


9.?????? Shut down the instance and start it again. When in the CLI, run the mount command. You should see the directory already mounted


Akintunde Ademola

| AWS Solutions Architect | Microsoft Certified Azure Associate | Scrum Master | Agile Leader | Certified DevOps Generalist | Certified cyber intelligence professional (CCIP) |

2 个月

Well said!

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了