简体   繁体   中英

Convert String to CommaDelimitedList Cloudformation Template

I have the following Cloudformation Template:

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

However, AWSInstanceID is converting to:

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

But this is not the format I want. How do I get the following output?

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

I would like to convert from String to CommaDelimitedList.

CloudFormation has an intrinsic function called Fn::Split . Copied from the user guide:

The following example splits a string at each vertical bar (|). The function returns ["a", "b", "c"].

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

So in your case, I guess it translates to

!Split [ ",", !Ref AWSInstanceID ]

Alternatively, you could also try and specify the AWSInstanceID as a CommaDelimitedList type, eg

Parameters:
  AWSInstanceID:
    Type: CommaDelimitedList

From Parameters - AWS CloudFormation :

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

An array of Amazon EC2 instance IDs, such as i-1e731a32, i-1e731a34.

So, try changing your Parameter from:

Type: String

to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM