0

I tried to mount ec2 instance to efs in launch configuration which is written in the YAML CloudFormation. I don't know how to get File-system-id which is created in the CloudFormation template.

Is it possible to get the values of the AWS resource in bash code? Or is this any way that we can mount the ec2 instance to efs using CloudFormation.

LaunchConfig:
    Type: AWS::AutoScaling::LaunchConfiguration
    Properties:
      ImageId: ami-33f92051
      InstanceType: t2.micro
      AssociatePublicIpAddress: 'true'   #auto-assign public ip 
      KeyName: 
        Ref: KeyName
      SecurityGroups:
      - Ref: PublicEC2instancesSecurityGroupJing
      BlockDeviceMappings:
      - DeviceName: "/dev/xvda"
        Ebs:
          SnapshotId: 'snap-0821cc7c34fcb7b01'
          VolumeSize: 8
          DeleteOnTermination: 'false'
      UserData:
        Fn::Base64:
          Fn::Join:
          - "\n"
          - - "#!/bin/bash -xe"
            - sudo yum update -y
            - sudo yum -y install nfs-utils

            - file = 
            - Ref: EFSFileSystem
            - echo $file >> /var/www/html/index.html
            - cd /mnt
            - sudo mkdir efs
            - sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $file.efs.ap-southeast-2.amazonaws.com:/ /mnt/efs

EFSFileSystem:
    Type: AWS::EFS::FileSystem
    Properties:
      PerformanceMode: generalPurpose
      ThroughputMode: bursting

1 Answer 1

1

To get the logical ID of the AWS::EFS::FileSystem:

!Ref EFSFileSystem

You can use it like this in your template:

        - sudo mkdir efs
        - !Join
            ''
              'sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 '
              !Ref 'EFSFileSystem'
              '.efs.ap-southeast-2.amazonaws.com:/'
              '/mnt/efs'

Make sure that you keep the spaces between the quotes correct so that the command line is built exactly like you need.

Sign up to request clarification or add additional context in comments.

2 Comments

But it says there is a format error in line 4 when I create the stack. Should I put '-' before each line?
Hi, I don't know why the folder efs is not created so I can't see if the mount is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.