【问题标题】:How to have Fn::Join nested inside of Fn::If?如何让 Fn::Join 嵌套在 Fn::If 中?
【发布时间】:2019-02-05 18:55:23
【问题描述】:

我有一个 bash 脚本,我想在我的实例上运行,但我只希望脚本的第二部分在值为 true 时运行。另外,我宁愿不在脚本中使用 if 语句。

Parameters:
   #TestParameter = TRUE
Resource:
   UserData:  
      Fn::Sub: |
          echo "This is a test example"
          #If TestParameter is true:
          echo "Only is parameter is true"

【问题讨论】:

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


    【解决方案1】:

    在我看来,最好在脚本中构造Sub参数:

    UserData:
      Fn::Sub: |
        echo "This is a test example"
        if ${TestParameter}; then
            echo "Only is parameter is true"
        fi
    

    但由于您不想在脚本中包含 if,因此您必须在 Cloudformation 模板中构建脚本的主体。在 CFN 中构建字符串总是很麻烦。试试这样的:

    UserData:
      Fn::Join: ["\n", ["echo 'This is a test example'",
                         ['Fn::If': [!Equals [!Ref TestParameter, "true"], "echo 'Parameter is true'", ""]]]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2021-11-11
      • 2019-12-30
      相关资源
      最近更新 更多