How to get the CloudFormation resource value in user data bash code

我是研究僧i 提交于 2019-12-11 17:23:46

问题


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:


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.



来源:https://stackoverflow.com/questions/52712540/how-to-get-the-cloudformation-resource-value-in-user-data-bash-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!