Tuesday, 21 May 2019

How to Mount the File System during launch of EC2 Server by Raj Gupta

Use the following procedure to launch two t2.micro instances. The user data script mounts the file system to both instances during launch and updates /etc/fstab to ensure that the file system is remounted after an instance reboot.



On the Configure Instance Details page, do the following:

  • For Number of instances, type 2. 
  • [Default VPC] If you have a default VPC, it is the default value for Network. Keep the default VPC and the default value for Subnet to use the default subnet in the Availability Zone that Amazon EC2 chooses for your instances. 
  • [Nondefault VPC] Select your VPC for Network and a public subnet from Subnet.
  • [Nondefault VPC] For Auto-assign Public IP, choose Enable. Otherwise, your instances do not get public IP addresses or public DNS names. 
  • Under Advanced Details, select As text, and paste the following script into User data. Update FILE_SYSTEM_ID with the ID of your file system. You can optionally update MOUNT_POINT with a directory for your mounted file system.

#!/bin/bash
yum update -y
yum install -y nfs-utils
FILE_SYSTEM_ID=fs-79f9579a
AVAILABILITY_ZONE=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone )
REGION=${AVAILABILITY_ZONE:0:-1}
MOUNT_POINT=/mnt/efs
mkdir -p ${MOUNT_POINT}
chown ec2-user:ec2-user ${MOUNT_POINT}
echo ${FILE_SYSTEM_ID}.efs.${REGION}.amazonaws.com:/ ${MOUNT_POINT} nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,_netdev 0 0 >> /etc/fstab
mount -a -t nfs4



Then create the EC2 as a normal process we are using.


Now Test the File System

You can connect to your instances and verify that the file system is mounted to the directory that you specified (for example, /mnt/efs).

From the terminal window for each instance, run the df -T command to verify that the EFS file system is mounted


[ec2-user@ip-172-31-91-16 ~]$ df -T
Filesystem                                Type            1K-blocks    Used        Available Use% Mounted on
devtmpfs                                  devtmpfs           494096      64           494032   1% /dev
tmpfs                                     tmpfs              504708       0           504708   0% /dev/shm
/dev/xvda1                                ext4              8189348 1125496          6963604  14% /
fs-79f9579a.efs.us-east-1.amazonaws.com:/ nfs4     9007199254739968       0 9007199254739968   0% /mnt/efs

(Optional) Create a file in the file system from one instance, and then verify that you can view the file from the other instance.

From the first instance, run the following command to create the file:
[ec2-user@ip-172-31-91-16 ~]$  sudo touch /mnt/efs/test-file.txt

From the second instance, run the following command to view the file:
[ec2-user@ip-172-31-85-61 ~]$ ls /mnt/efs
test-file.txt



No comments:

Post a Comment