AWS Cloudformation中UserData中的参考参数值

时间:2017-06-27 08:28:23

标签: amazon-web-services amazon-ec2 amazon-cloudformation

我在参数部分

下有这个
Parameters:
  PlatformSelect:
    Description: Cockpit platform Select.
    Type: String
    Default: qa-1
    AllowedValues: [qa-1, qa-2, staging, production]

我需要在UserData中引用此值。我正在使用Mappings。

Mappings:
  bootstrap:
    ubuntu:
      print: echo ${PlatformSelect} >>test.txt

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref ‘InstanceType’
      KeyName: !Ref ‘KeyName’
      Tags:
      - Key: Name
        Value: Test
      UserData:
        Fn::Base64:
          Fn::Join:
          - ‘’
          - - |
              #!/bin/bash
            - Fn::FindInMap:
              - bootstrap
              - ubuntu
              - print
            - |2+

这不起作用。不确定我推荐的方式在第一时间是错误的!

我之前应该使用'$ {AWS :: Parameters:PlatformSelect}'吗?

2 个答案:

答案 0 :(得分:2)

您之间是否有使用Mapping的原因?

您可以轻松使用!Sub代替

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      Tags:
        - Key: Name
          Value: Test
      UserData:
        Fn::Base64:
          !Sub |
            #!/bin/bash
            ${PlatformSelect}

答案 1 :(得分:2)

Fn::JoinRef

的组合怎么样?
UserData:
        Fn::Base64:
          Fn::Join:
            - ''
            - - '#!/bin/bash\n'
              - 'print: echo'
              - !Ref 'PlatformSelect' 
              - '>>test.txt\n'