DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Install VPN on Linux?
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 1
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS

Trending

  • Dear Micromanager: Your Distrust Has a Job; It’s Just Not the One You’re Doing
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Exactly-Once Processing: Myth vs Reality
  • How to Format Articles for DZone
  1. DZone
  2. Coding
  3. Tools
  4. Creating EFS Using CloudFormation and Mounting it With EC2 Linux Instance

Creating EFS Using CloudFormation and Mounting it With EC2 Linux Instance

This tutorial goes step by step through the steps of creating and mounting an Elastic Files Storage instance using CloudFormatino and EC2 Linux.

By 
Ajay Sodhi user avatar
Ajay Sodhi
·
Jun. 11, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
13.8K Views

Join the DZone community and get the full member experience.

Join For Free

There are multiple ways of storing information on an instance, like EBS or EFS. EBS is Elastic Block Storage and can be considered as if you have a high capacity Storage Device attached to your computer. Whereas EFS is Elastic File Storage and can be considered as if you have attached an external storage device attached to your computer. It may depend on your application or use case you choose to use what among both, but for our case, that we are discussing today, we are going to use EFS with EC2 Linux instance.

Create EFS Using CloudFormation

Let's create EFS using CloudFormation. You can use the following template to create the resource.  Just pass the appropriate values when asked while creating the resource.

  • Just save the below template in a file with a file extension JSON.  
JSON
 




x


 
1
{
2
  "AWSTemplateFormatVersion": "2010-09-09",
3
  "Description": "This cloudformation will create encryped Elastic File System.",
4
  "Parameters": {
5
    "SystemName" : {
6
      "Type": "String",
7
      "Description" : "System for which this cloudformation is created.",
8
      "Default" : "myLinuxSystem"
9
    },
10
    "Environment" : {
11
      "Type": "String",
12
      "Description" : "Environment for which this cloudformation is getting created",
13
      "Default" : "dev"
14
    },  
15
    "RegionName" : {
16
      "Type": "String",
17
      "Description" : "Region Name in which resources are to be created.",
18
      "Default" : "eu"
19
    },
20
    "BackbonePrivateSubnetAZa": {
21
      "Type": "String",
22
      "Description" : "Private subnet in availability zone A"
23
    },
24
    "BackbonePrivateSubnetAZb": {
25
      "Type": "String",
26
      "Description" : "Private subnet in availability zone B"
27
    },
28
    "EC2InstanceLinkedEFSFileSystemTagName": {
29
      "Type": "String",
30
      "Description": "Tag name of Elastic File System.",
31
      "Default": "elastic-file-system"
32
    },
33
    "FileSecurityGroup": {
34
      "Type": "String",
35
      "Description": "File System Security Group"
36
    }
37
  },
38
  "Resources": {
39
    "EFSFileSystem": {
40
      "Type" : "AWS::EFS::FileSystem",
41
      "Properties" : {
42
        "FileSystemTags": [
43
          {
44
            "Key": "Name",
45
            "Value": { "Fn::Join": [ "-", [ { "Ref": "SystemName" }, { "Ref": "Environment" }, { "Ref": "RegionName" }, { "Ref": "EC2InstanceLinkedEFSFileSystemTagName" } ] ] }
46
          }
47
        ]
48
      }
49
    },
50
    "EFSMountTargetAZa": {
51
      "Type": "AWS::EFS::MountTarget",
52
      "Properties": {
53
        "FileSystemId": {"Ref": "EFSFileSystem"},
54
        "SubnetId": {"Ref": "BackbonePrivateSubnetAZa"},
55
        "SecurityGroups": [{"Ref": "FileSecurityGroup"}]
56
      }
57
    },
58
    "EFSMountTargetAZb": {
59
      "Type": "AWS::EFS::MountTarget",
60
      "Properties": {
61
        "FileSystemId": {"Ref": "EFSFileSystem"},
62
        "SubnetId": {"Ref": "BackbonePrivateSubnetAZb"},
63
        "SecurityGroups": [{"Ref": "FileSecurityGroup"}]
64
      }
65
    }
66
  },
67
  "Outputs" : {
68
    "ElasticFileSystem" : {
69
      "Description": "Elastic file system.",
70
      "Value" : {"Ref": "EFSFileSystem"}
71
    },
72
    "EFSMountTargetAZaId" : {
73
      "Description": "EFS mount target Id in availability zone A.",
74
      "Value" : {"Ref": "EFSMountTargetAZa"}
75
    },
76
    "EFSMountTargetAZbId" : {
77
      "Description": "EFS mount target Id in availability zone B.",
78
      "Value" : {"Ref": "EFSMountTargetAZb"}
79
    }
80
  } 
81
}



  • Open the AWS console and login with your credentials.
  • Open CloudFormation. You can traverse there by clicking on Services and then typing CloudFormation on the top right search bar.
  • Once there, click on the Create Stack (With new resource (standard)) button.
  • You will see a screen similar to one below:
  • Click on Upload template file and then Choose file.
  • Upload the template JSON file which you have saved on your system from previous steps.
  • Provide the values asked on the screen. There are few default values which are already provided in the template. Modify them as per your need.
  • Follow the wizard to create the required stack.
  • After some time, you will see the resources created.
  • You can go to Services -> EFS and see the resource created.
    See created resources

Attaching EFS with your EC2 instance.

  • Login to your EC2 instance.
  • Log in as superuser. You can use the following command as well:
  • Shell
     




    xxxxxxxxxx
    1


     
    1
    sudo su


  • Execute the following command to create a file directory that will be mounted to save the data on EFS.

  • Shell
     




    xxxxxxxxxx
    1


     
    1
    mkdir -p /mnt/myefsdata/
    2
    chown ec2-user:ec2-user /mnt/myefsdata/


Shell




xxxxxxxxxx
1


 
1
# mkdir -p /mnt/myefsdata/
2
# chown ec2-user:ec2-user /mnt/myefsdata/


  • Now you need to execute the following command after modifying, to mount the newly created EFS
  • Shell
     




    x




    1
    mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-xxxxxxxx.efs.eu-west-1.amazonaws.com:/ /mnt/myefsdata


  • is the DNS name of the EFS which you have created.
  • Execute the following command to know if the drive is mounted successfully:
  • Shell
     




    xxxxxxxxxx
    1


     
    1
    df -h


  • You will see the output similar to one below:
  • Shell
     




    x


     
    1
    Filesystem                                 Size  Used Avail Use% Mounted on
    2
    devtmpfs                                   3.8G   72K  3.8G   1% /dev
    3
    tmpfs                                      3.8G  4.0K  3.8G   1% /dev/shm
    4
    /dev/nvme0n1p1                              50G  6.7G   43G  14% /
    5
    fs-xxxxxxxx.efs.eu-west-1.amazonaws.com:/  8.0E   14G  8.0E   1% /mnt/myefsdata


  • Now anything that you will save under /mnt/myefsdata will be stored to EFS attached.

Congratulations.!! You Did It..!!

AWS Linux (operating system)

Opinions expressed by DZone contributors are their own.

Related

  • How to Install VPN on Linux?
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 1
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook