Combination Fn::Split and Fn::Select in Cloudformation template

Viewed 7733

I'm trying to combine the Fn::Select and the Fn::Split with an Fn::ImportValue function in a Stack template like that:

 Resources:
  ALBDashboard:
   Type: AWS::CloudWatch::Dashboard
   Properties:
    DashboardName: ALB-Dashboard
    DashboardBody: !Sub
    - |
        {
        "widgets": [
            {
                "type": "metric",
                "x": 0,
                "y": 21,
                "width": 9,
                "height": 3,
                "properties": {
                    "metrics": [
                        [ "AWS/ApplicationELB", "RequestCount", "TargetGroup", "targetgroup/GeneratorTG/ca775e3193d3b120", "LoadBalancer", "app/Dev-Invoicegen-ALB-Internet/8ac95b5b6900fa0c", "AvailabilityZone", "${AvailabilityZone1}", { "stat": "Sum" } ],
                        [ "...", "${AvailabilityZone2}", { "stat": "Sum" } ],
                        [ "...", "${AvailabilityZone3}", { "stat": "Sum" } ]
                    ],
                    "view": "singleValue",
                    "region": "${AWS::Region}",
                    "period": 300,
                    "title": "Request Count GeneratorTG 5 min - Sum"
                }
            },
  - TargetGroup:
       Fn::Select: [5, Fn::Split: [":", Fn::ImportValue: !Sub "${EnvironmentName}-WebTGARN" ]]

but keep getting the following error:

Template format error: YAML not well-formed

but according to this blog it seems correct:

https://garbe.io/blog/2017/07/17/cloudformation-hacks/

On the other hand if I try this, it works:

  -  TargetGroup1:
        Fn::Select:
        - 5
        - Fn::Split:
          - ":"
          - Fn::ImportValue: !Sub "${EnvironmentName}-WebTGARN"

Can someone tell me where the error is?

merci A

2 Answers

Here is something that works for me.

TemplatesS3BucketName: !Select [2, !Split ["/", !Select [0, !Split [".", !Ref TemplatesS3BucketURL]]]]

When working with YAML CFN templates, I tend to use the ! rather than Fn:: and have better luck.

When using long-form Cloudformation functions in yaml, you can't use nested functions on the same line. They have to be put on a new line.

I'm not sure what the exact issue is, but I think it's related to yaml being a bit picky about supporting unquoted terms with a trailing colon, as that is how it finds dict keys. Fn::Select: has that trailing colon, and !Select doesn't. Throw in that Cloudformation is also Not Quite Yaml, and it becomes murkier. Your working example doesn't nest Fn:: items on the same line, so it avoids that issue.

If you really, really want to have a 'one liner', you can try converting that one line into json - yaml is a superset of json, so valid json is valid yaml. I haven't tried it, but I think this will avoid the 'nested Fn:: in yaml' issue.

Related