我正在使用 PyYAML 库编写一个自定义 Python 应用程序,该应用程序需要读取 AWS CloudFormation YAML 模板。
我知道这些模板是有效的 CloudFormation 模板,因为我使用 validate-template 测试了它们:
▶ aws cloudformation validate-template --template-body file://cloudformation.yml
然而,当我尝试使用 PyYAML 库读取它们时,我收到如下错误:
yaml.scanner.ScannerError:此处不允许映射值
and
无法确定标签“!Sub”的构造函数
和别的。
举例来说,我尝试了这个 AWS 示例模板:
▶ curl -s \
https://raw.githubusercontent.com/awslabs/aws-cloudformation-templates/master/aws/services/CloudFormation/FindInMap_Inside_Sub.yaml \
-o FindInMap_Inside_Sub.yaml
进而:
▶ python
Python 2.7.15 (default, Nov 27 2018, 21:40:55)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
>>> yaml.load(open('FindInMap_Inside_Sub.yaml'))
这导致:
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!FindInMap'
in "FindInMap_Inside_Sub.yaml", line 89, column 45
如何使用 PyYAML 或其他库解析 CloudFormation YAML 文件?