【发布时间】:2018-11-27 14:12:40
【问题描述】:
我正在使用需要读取 AWS CloudFormation YAML 模板的 PyYAML 库编写自定义 Python 应用程序。
我知道这些模板是有效的 CloudFormation 模板,因为我使用 validate-template 对其进行了测试:
▶ aws cloudformation validate-template --template-body file://cloudformation.yml
但是,当我尝试使用 PyYAML 库读取它们时,我收到如下错误:
yaml.scanner.ScannerError: 此处不允许映射值
和
无法确定标签“!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 文件?
【问题讨论】:
标签: amazon-web-services yaml amazon-cloudformation pyyaml