How to use !FindInMap in !Sub | userdata section

假如想象 提交于 2019-12-04 09:23:56

I've been having fun and games with this as well. Although the documentation says that Fn::FindInMap is supported in Fn::Sub, there's no example of use and I've tried all sorts of combinations of quotes and colons without success, but I finally seem to have hit upon a functional solution using mappings. The following should work:

Fn::Base64: !Sub
  - |+
    #!/bin/bash -v
    /command ${Url}
  - Url:
      Fn::FindInMap:
        - UrlMap
        - !Ref AWS::Region
        - !Ref EnvironmentType

The pipe at the start of arg0 tells YAML to preserve newlines, and the plus tells it to keep a newline afterwards. Arg1 tells it to substitute the result of the Fn::FindInMap for the Url in arg0.

The following shorter version should also work:

Fn::Base64: !Sub
  - |+
    #!/bin/bash -v
    /command ${Url}
  - Url:
      Fn::FindInMap: [UrlMap, Ref: "AWS::Region", Ref: EnvironmentType]

But you should test that. Note the commas, quotes, and reversion to Ref:s rather than !Refs. This probably tells us something about how the files are being pre-processed, but I'm not sure what that is.

I'm sure that this solution is obvious to experienced YAMLers, but I was only just starting to get my head around JSON when all this YAMLy goodness was added to CloudFormation.

Aman Mahajan

This works fine for me

UserData :
  Fn::Base64 : !Sub
  - |
    #!/bin/bash -v
    export some_variable = ${url}
  - url : !FindInMap [Mapping, !Ref AWS::Region, !Ref EnvironmentType]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!