【问题标题】:Reference Parameter Value in UserData in AWS Cloudformation参考 AWS Cloudformation 中 UserData 中的参数值
【发布时间】:2017-11-30 05:27:11
【问题描述】:

我在参数部分有这个,

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

我需要在我的 UserData 中引用这个值。我在两者之间使用映射。

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}”吗?

【问题讨论】:

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


    【解决方案1】:

    您在两者之间使用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}
    

    【讨论】:

    • 没有特别的原因!我被现有的 CF 脚本迷住了。这行得通!谢谢(y)
    【解决方案2】:

    Fn::JoinRef 的组合呢

    UserData:
            Fn::Base64:
              Fn::Join:
                - ''
                - - '#!/bin/bash\n'
                  - 'print: echo'
                  - !Ref 'PlatformSelect' 
                  - '>>test.txt\n'
    

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 1970-01-01
      • 2015-03-04
      • 2018-01-29
      • 1970-01-01
      • 2021-04-27
      • 2018-12-19
      • 2020-11-17
      • 1970-01-01
      相关资源
      最近更新 更多