本文介绍了如何在!Sub中使用!FindInMap |用户数据部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在将CFT从JSON转换为Yaml。一切正常,直到Userdata部分。我很难使用userdata部分中的!Ref或!FindInMap之类的功能。

Currently I am converting CFT from JSON to Yaml. Everything works fine until Userdata section.I am having hard time to use any of functions like !Ref or !FindInMap in userdata section.

UserData:

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

如果有人可以共享任何代码段,这将非常有帮助。

It would be very helpful, If anyone can share any snippet of code.

推荐答案

与此同时,我也一直在玩游戏。尽管表示<$ Fn :: Sub 支持c $ c> Fn :: FindInMap ,没有使用示例,我尝试了各种组合引号和冒号都没有成功,但是我似乎终于找到了使用映射的功能解决方案。以下应该可以工作:

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

arg0开头的管道告诉YAML保留换行符,并且加号告诉它以后要保留换行符。
Arg1告诉它用 Fn :: FindInMap 的结果替换arg0中的Url。

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]

但是您应该测试一下。请注意逗号,引号和还原为 Ref: s而不是!Ref s。这可能告诉我们一些有关文件如何进行预处理的信息,但是我不确定那是什么。

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.

我确信此解决方案对于经验丰富的YAML专家,但是当所有这些YAMLy优势添加到CloudFormation中时,我才刚刚开始了解JSON。

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.

这篇关于如何在!Sub中使用!FindInMap |用户数据部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:43