aws-cloudformation / cloudformation-coverage-roadmap

The AWS CloudFormation Public Coverage Roadmap
https://aws.amazon.com/cloudformation/
Creative Commons Attribution Share Alike 4.0 International
1.1k stars 53 forks source link

Allow Fn::Sub to expand intrinsic functions in the first parameter (currently only supports string) #1948

Open staizen-stephen opened 4 months ago

staizen-stephen commented 4 months ago

Name of the resource

Other

Resource name

CloudFormation intrinsic function Fn::Sub

Description

Fn::Sub accepts two parameters, a first string-only parameter and a second object containing named substitutions.

The second argument allows the use of other CloudFormation intrinsic functions, such as !ImportValue. However, the first does not. For example, the following breaks syntax:

- !Sub
  - !Sub ""
  - a: "b"

When building complex values, for example a JSON object with certain parts of the structure dependent on conditions, disallowing the expansion (as now) forces a more complex approach, such as:

- Fn::Join:
  - ""
  - - "{"
    -  Fn::If
      - SomeCondition
      - !Sub 
        - |
          "a": "${lookup}",
        - lookup: !ImportValue "my-lookup"
      - ""
    - !Sub
      - |
        "b": "${value2}",
        "c": "a fixed value"
      - value2: !If [ SomeOtherCondition, enabled, disabled ]
    - "}"

As there isn't an equivalent for Terraform's local variables, there aren't really many options in these cases. You are forced to choose between:

  1. Creating multiple resources (which is not always appropriate)
  2. Complex branching logic with a lot of repetition
  3. Textual joins as demonstrated above

Nesting substitutions or applying other transformations on the first string parameter could simplify these. It's not clear or obvious why the first string parameter does not support expansion using intrinsic functions.

Other Details

No response