3

How do I split a string and use the value for a property?

For example say I have the following string: SomeRule1,SomeRule2.

I want use this string to populate the exludedRules property of AWS::WAFv2::WebACL ManagedRuleGroupStatement. excludedRules is a list of ExcludedRule objects that contains a single Name property. How can I use the splitted string value for the Name property?

neuro
  • 14,948
  • 3
  • 36
  • 59
aaa01ggggg
  • 111
  • 3

2 Answers2

3

Sadly you can't do this automatically using plain CloudFormation just by having SomeRule1,SomeRule2, because ExcludedRule is not a simple list of strings. It is list of objects, in the form of:

  ExcludedRules: 
    - Name: SomeRule1
    - Name: SomeRule2

Generation of such a list of objects would require some looping mechanism which is not supported in CloudFormation. You have to explicitly list all these rules, one by one.

But if you really must automate such process, you could develop a CloudFormation macro which would give you the ability to loop and construct such structures. Custom resources can also be used to automate such operations.

Both the macro and the custom resource would require you to develop a special lambda function which would perform the looping based on your SomeRule1,SomeRule2 and construct valid ExcludedRules.

Marcin
  • 215,873
  • 14
  • 235
  • 294
2

Agree with other answer, can't dynamically add Rules since its not a simple List of Strings. But if we know exact no of rules. we can use split and select.

Lets take this parameter:

Parameters:
  Rules:
    Type: String
    Description: "list of rules seperate by comma"
    Default: "my-rule-1,my-rule-2"

We can use it like this:

          ExcludedRules:
            - Name: !Select
                - 0
                - !Split [",", !Ref Rules]
            - Name: !Select
                - 1
                - !Split [",", !Ref Rules]
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42