top of page

CloudFormation script for creating an Amazon Elastic File system

In this article, I have provided ClouldFormation YAML script to create an Amazon Elastic File System in a given VPC. I have also provided step-by-step procedure on how to upload and run the CFN stack.

 

Before we get into the CloudFormation Script, lets look at what is EFS, its benefits and what is required to create an EFS, in a summarized view.


What is Amazon Elastic File System (EFS)?


Amazon Elastic File System (Amazon EFS) provides a managed simple, serverless file system for use with AWS Cloud services and on-premises resources. It can scale on demand and shrink automatically as you add and remove files, eliminating the need to provision and manage capacity to accommodate growth. This is one of the storage services provided by Amazon.

 

Benefits of Elastic File System

  • EFS is simple, scalable and elastic.​

  • Multiple instances can access EFS at the same time.

  • No minimum charge. Pay as per the storage used.

 

What is required to create an EFS


To create an EFS, we need the following

  • A Virtual Private Cloud (VPC)

  • Availability Zone(s): You can create EFS either in a Single Zone or Regional (Multiple Zones)

For the purpose of this article, we will be creating a Multi-Zone EFS in specific Availability Zones. You can also create a regional EFS without specifying any AZ and it will create EFS across all the available AZs in the region for that VPC.


Note: It is also recommended to create a separate Security Group for EFS as a best practice.

 

CloudFormation Script for creating an EFS


Below is the complete script to create an Amazon EFS in a VPC

AWSTemplateFormatVersion: '2010-09-09'
Description: This cloudformation will create encryped Elastic File System.
Parameters:
  VPC:
    Type: AWS::EC2::VPC::Id
    Description: VPC where the EFS should be deployed to  
  SystemName:
    Type: String
    Description: System for which this cloudformation is created.
    Default: EFSSystem
  EnvironmentName:
    Type: String
    Description: Environment for which this cloudformation is getting created
    Default: MyQAEnvironment
  RegionName:
    Type: String
    Description: Region Name in which resources are to be created.
    Default: ap-southeast-1
  EC2InstanceLinkedEFSFileSystemTagName:
    Type: String
    Description: Tag name of Elastic File System.
    Default: elastic-file-system
Resources:
  EFSSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref VPC
      GroupDescription: EFS Security Group
      SecurityGroupIngress:
        # Allow access from anywhere
        - CidrIp: 0.0.0.0/0
          IpProtocol: tcp
          FromPort: "Provide the required port"
          ToPort: "Provide the required port"
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName}-EFS
  EFSFileSystem:
    Type: AWS::EFS::FileSystem
    Properties:
      BackupPolicy:
        Status: ENABLED
      PerformanceMode: maxIO
      Encrypted: true
      LifecyclePolicies:
        - TransitionToIA: AFTER_30_DAYS      
      FileSystemTags:
      - Key: Name
        Value: !Sub ${EnvironmentName}-EFS
  EFSMountTargetAZ1:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId:
        Ref: EFSFileSystem
      SubnetId: "Provide the Subnet ID"
      SecurityGroups:
      - Ref: EFSSecurityGroup
  EFSMountTargetAZ2:
    Type: AWS::EFS::MountTarget
    Properties:
      FileSystemId:
        Ref: EFSFileSystem
      SubnetId: "Provide the Subnet ID"
      SecurityGroups:
      - Ref: EFSSecurityGroup
Outputs:
  ElasticFileSystem:
    Description: Elastic file system.
    Value:
      Ref: EFSFileSystem
  EFSMountTargetAZ1Id:
    Description: EFS mount target Id in availability zone 1.
    Value:
      Ref: EFSMountTargetAZ1
  EFSMountTargetAZ2Id:
    Description: EFS mount target Id in availability zone 2.
    Value:
      Ref: EFSMountTargetAZ2
#===========================================================
 

How to upload and run the CloudFormation Script

  1. Login to your AWS account and navigate to Cloud Formation page

  2. Upload the YAML script either from S3 bucket or from your local machine, as mentioned below and click on Next

3. Provide stack details like stack name, VPC and Region as mentioned below and click on Next

4. On the Review stack page, click on the Create Stack

5. The stack runs successfully and creates EFS, as shown below.

6. Cross check the created EFS by navigating to EFS service. That's it! We have successfully created EFS.

 

I believe this article will be useful for AWS DevOps and SysOps professionals, Architects or even AWS cloud computing beginners. Please provide your valuable comments on this article and share with your known groups, if you find this helpful. Thank you.


Note: The YAML file has been parked in my Github repository, mentioned below.

https://github.com/praveenps1975/AWS-CloudFormation-Files

 

663 views0 comments
bottom of page