Web Programming TutorialsLearn How To Integrate EFS Into AWS Using PHP

Learn How To Integrate EFS Into AWS Using PHP

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:

$ php composer require aws/aws-sdk-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:

    <?php # AWS EFS examples

    # initializing
    require 'vendor/autoload.php'; # include the SDK using the Composer autoloader
    # configurations
    $config = [
    'efs-access' => [
   		 'key' => '[Private Access Key]',
   		 'secret' => '[Private Secret Key]',
   		 'region' => 'ap-southeast-2', # we use sydney for this example
   		 'version' => 'latest'

    # initializing efs
    $efs_client = Aws\Efs\EfsClient::factory([
   	 'credentials' => [
   		 'key' => $config['efs-access']['key'],
   		 'secret' => $config['efs-access']['secret']
   	 'version' => $config['efs-access']['version'],
   	 'region' => $config['efs-access']['region']

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:

    # creating efs drive
    $efs_drive = $efs_client->createFileSystem([
   	 'CreationToken' => $config['efs-access']['secret']
    print_r($efs_drive); # newly created EFS information is printed here

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:

# mounting efs
$mount_result = $efs_client->createMountTarget([
    'FileSystemId' => 'fs-1def3e23', # we got this when the above code was executed to create the EFS drive
    'SubnetId' => 'subnet-f2cca784' # this comes from the VPC being used

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:

    $ mkdir ~/efs-mount-point
    $ sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 mount-target-DNS:/   ~/efs-mount-point

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.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -