You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyPlate宏批量预置资源时遇YAML格式错误求助

Fixing the YAML Format Error in PyPlate CloudFormation Template

The YAML format error happens because your PyPlate script was generating quoted string literals instead of valid YAML key-value pairs for the S3 buckets, and it wasn't handling indentation correctly—both are critical for YAML's strict structure.

Corrected CloudFormation Template

AWSTemplateFormatVersion: "2010-09-09"
Transform: [PyPlate]
Description: A stack that provisions a bunch of s3 buckets based on param names
Parameters:
  BucketNames:
    Type: CommaDelimitedList
    Description: All bucket names that should be created
    Default: BucketA,BucketB,BucketC
Resources:
  TestBucket:
    Type: "AWS::S3::Bucket"
#!PyPlate
output = []
bucket_names = params['BucketNames']
# Match the indentation level of existing Resources entries (2 spaces)
resource_indent = '  '
# For each bucket name, generate valid YAML key-value structure
for name in bucket_names:
    # Add the bucket name as a top-level key under Resources
    output.append(f"{resource_indent}{name}:")
    # Indent one level deeper for the Type property (total 4 spaces)
    output.append(f"{resource_indent}  Type: AWS::S3::Bucket")
# Print the generated YAML lines to insert into the template
print('\n'.join(output))

Why the Original Template Failed

  1. Invalid YAML Structure: Your original script created strings like "BucketA": {"Type": "AWS::S3::Bucket"}—YAML interprets this as a single string value, not a dictionary key-value pair that CloudFormation expects for a resource.
  2. Missing Proper Indentation: YAML relies entirely on consistent indentation to define hierarchy. The generated bucket entries needed to match the indentation of the existing TestBucket resource (2 spaces under Resources), with the Type property indented an additional 2 spaces to sit under the bucket name.

Key Fixes Explained

  • Indentation Alignment: We explicitly set resource_indent to match the indentation of TestBucket, so generated resources fit seamlessly under the Resources section.
  • Valid YAML Construction: Instead of concatenating quoted strings, we build proper YAML lines: first the bucket name as a key, then the Type property indented one level deeper to maintain the resource's hierarchy.
  • Clean Line Breaks: Joining the output list with \n ensures each resource entry is on its own line, keeping the YAML readable and structurally valid.

内容的提问来源于stack exchange,提问作者Igor L.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 23:17:28