如何在用户数据Bash代码中获取CloudFormation资源值 [英] How to get the CloudFormation resource value in user data bash code

查看:105
本文介绍了如何在用户数据Bash代码中获取CloudFormation资源值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用YAML CloudFormation编写的启动配置中将ec2实例安装到efs。我不知道如何获取在CloudFormation模板中创建的File-system-id。

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.

是否可以通过bash代码获取AWS资源的值?
或者这是我们可以使用CloudFormation将ec2实例挂载到efs的任何方式。

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


推荐答案

获取 AWS :: EFS :: FileSystem

!Ref EFSFileSystem

您可以像使用它一样

        - 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.

这篇关于如何在用户数据Bash代码中获取CloudFormation资源值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆