How do I use the Fn::Sub function in AWS CloudFormation with Fn::FindInMap, Fn::ImportValue, or other supported functions?

Last updated: 2020-08-26

I want to use the Fn::Sub function in AWS CloudFormation with Fn::FindInMap, Fn::ImportValue, or other supported functions.

Short description

You can use the Fn::Sub function to substitute supported functions or to substitute as a string for other functions.

Resolution

Use Fn::Sub with supported functions

In your AWS CloudFormation template, you can use Fn::Sub to substitute other supported functions, such as Fn::FindInMap.

If you're using a JSON template, substitute Var1Name and Var2Name with a Var1Value and Var2Value. For example:

{ "Fn::Sub" : [ String, { Var1Name: Var1Value, Var2Name: Var2Value } ] }

The following JSON example shows how you can substitute ${Domain} with a resolved value of Fn::FindInMap, if you're using a variable from Fn::FindInMap:

"Name" : { "Fn::Sub": [ "www.${Domain}", { "Domain": { "Fn::FindInMap" : [ "ABC", "DomainParameters", "DomainName"] }} ]}

If you're using a YAML template, you can use key-value pairs to substitute multiple variables. For example:

Fn::Sub:
  - String
  - Var1Name: Var1Value
    Var2Name: Var2Value

The following YAML example shows how you can substitute ${Domain} with { "Fn::FindInMap" : [ "ABC", "DomainParameters", "DomainName"] }}, if you're using a variable from Fn::FindInMap:

Name: !Sub
  - www.${Domain}
  - { Domain: !FindInMap ["ABC", "DomainParameters", "DomainName"] }

Use Fn::Sub as a string to substitute other functions

You can use Fn::Sub to substitute other functions, such as Ref or Fn::GetAtt.

For example, you can use Fn::Sub to substitute Ref:

JSON:

"Name": { "Fn::Sub": "www.${Domain}"}

YAML:

Name: !Sub www.${Domain}

Did this article help?


Do you need billing or technical support?