Learn How To Integrate EFS Into AWS Using PHP

0
605
Integrate EFS

AWS has many storage services to offer, out of which S3 is well known and the most used one. S3 can be used as a CDN or an Archival storage service or simply as a file distribution service. S3 has been revolutionary in the online storage industry but it lack one thing, which is to be treated as an elastic file system. S3 has been designed from the ground up to serve and store files in a secured and distributed manner but not as a regular filesystem.

It won’t be wrong to say that this inability to use S3 as a mounted filesystem on the EC2 instances has been a major shortcoming.

To counter this S3 shortcoming, AWS has come up with a new solution which gives us the storage capacity of S3 along with the capability to add it as an additional space to the present EC2 instance as a Network Storage Device.

Implementing EFS is simple, all you need to do is:

Figure out the region closest to you that supports EFS. Not all regions support EFS for now.
Your EC2 instance need to be in the same region as the above created EFS.
We also need VPC to be setup for the same region as above two. VPC is auto created for new accounts. This VPC is also a requirement when creating new EC2 instance.

Once these steps are completed, all we need to do is access EC2 instance from the CLI and mount this newly created EFS drive to that instance. We use Sydney as our AWS region for code examples in this article.

The process of creating EFS can be programmatically managed using AWS SDK for PHP. So, once the EC2 instance is created, follow the following steps on how to create an EFS using PHP AWS SDK. Before running code to create EFS, we need to install AWS SDK for PHP. You can either download the latest AWS SDK from Github or simply use Composer to install AWS SDK. Following command is used to install AWS SDK using PHP:

Note: The following code examples are coded in PHP7. The complete AWS EFS SDK functionality can be found here.

Once done, create a php file named ‘efs.php’ with the following contents:

The above code initializes AWS EFS instance that we’ll further use for creating EFS instance along with it’s Mount Point.

In the following example, we create our very first EFS drive:

After the above code is executed, newly created EFS client information is printed on the screen. You need to save this information as we later need it to create Mount Points.

To create a mount point for this newly created EFS client, we execute the following code:

FileSystemId‘ is the ID of the EFS drive which we want to mount. This value is returned in the object data printed to create EFS drive at first point.

‘SubnetId’ is the ID of Subnet under the VPC which is also connected to our EC2 instance in the same region (for this example, ‘Sydney’). You can create multiple mount points as per different availability zones. For this example, we only create one.

Now you are all set, you simply need to login to the EC2 console and mount the above created EFS drive using the following commands:

The first command creates the folder ‘efs-mount-point’. The second command mounts our EFS drive to the folder. ‘mount-target-DNS’ should be replace with the DNS address of your EFS Drive’s Mount Point.

Once done, you can simply use this mounted folder as a regular folder on your EC2 drive.

LEAVE A REPLY

Please enter your comment!
Please enter your name here