【问题标题】:Convert String to CommaDelimitedList Cloudformation Template将字符串转换为 CommaDelimitedList Cloudformation 模板
【发布时间】:2019-05-21 23:58:43
【问题描述】:

我有以下 Cloudformation 模板:

AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  serviceRoleArn:
    Type: String
    Description: The role that's used when the task is executed.
  AWSInstanceID:
    Type: String
  awsSSMMaintenanceWindowTargetId:
    Type: String
  awsSSMMaintenanceWindowId:
    Type: String
  automationSSMTaskRole:
    Type: String
  automationSSMTaskType:
    Type: String
  automationSSMTaskDescription:
    Type: String
  automationSSMTaskARN:
    Type: String

Resources:
  startInstanceTask:
    Type: 'AWS::SSM::MaintenanceWindowTask'
    Properties:
      MaxErrors: "2"
      Description: !Ref "automationSSMTaskDescription"
      ServiceRoleArn:
        Ref: serviceRoleArn
      Priority: 1
      MaxConcurrency: "1"
      Targets:
      - Values:
        - !Ref "awsSSMMaintenanceWindowTargetId"
        Key: WindowTargetIds
      Name: !Ref "automationSSMTaskType"
      TaskArn: !Ref "automationSSMTaskARN"
      WindowId: !Ref "awsSSMMaintenanceWindowId"
      TaskType: "AUTOMATION"
      TaskInvocationParameters:
        MaintenanceWindowAutomationParameters:
          DocumentVersion: "$DEFAULT"
          Parameters:
            InstanceId:
              - !Ref AWSInstanceID
            AutomationAssumeRole:
              - Ref: automationSSMTaskRole

但是,AWSInstanceID 正在转换为:

"InstanceId": ["i-0375357htn1a8ad40,i-0d0f0f724tytf4d37,i-0e61cc61hthf8c2b2"]

但这不是我想要的格式。如何获得以下输出?

"InstanceId": [
    "i-0375357htn1a8ad40",
    "i-0d0f0f724tytf4d37",
    "i-0e61cc61hthf8c2b2"
]

我想从 String 转换为 CommaDelimitedList。

【问题讨论】:

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


    【解决方案1】:

    CloudFormation 有一个名为Fn::Split 的内在函数。复制自用户指南:

    以下示例在每个竖线 (|) 处拆分一个字符串。函数返回 ["a", "b", "c"]。

    !Split [ "|" , "a|b|c" ]
    

    所以在你的情况下,我想它转化为

    !Split [ ",", !Ref AWSInstanceID ]
    

    或者,您也可以尝试将AWSInstanceID 指定为CommaDelimitedList 类型,例如

    Parameters:
      AWSInstanceID:
        Type: CommaDelimitedList
    

    【讨论】:

      【解决方案2】:

      来自Parameters - AWS CloudFormation

      List<AWS::EC2::Instance::Id>

      一组 Amazon EC2 实例 ID,例如 i-1e731a32、i-1e731a34。

      所以,试着改变你的参数:

      Type: String
      

      到:

      Type: List<AWS::EC2::Instance::Id>
      

      【讨论】:

      • 这不是我的问题,我想传入一个字符串并将其转换为逗号分隔列表。
      • 我的建议是,它可以从一个列表开始,然后就不需要转换任何东西了。
      • 不幸的是,它并不像看起来那么简单,因为我正在使用 terraform 传递参数并且无法通过 terraform 传递逗号分隔的参数
      • 在这种情况下,您可能需要使用 AWS Lambda 创建一个Custom Resource。见:Lambda-Backed Custom Resource
      猜你喜欢
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      相关资源
      最近更新 更多