使用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
- 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. - Missing Proper Indentation: YAML relies entirely on consistent indentation to define hierarchy. The generated bucket entries needed to match the indentation of the existing
TestBucketresource (2 spaces underResources), with theTypeproperty indented an additional 2 spaces to sit under the bucket name.
Key Fixes Explained
- Indentation Alignment: We explicitly set
resource_indentto match the indentation ofTestBucket, so generated resources fit seamlessly under theResourcessection. - Valid YAML Construction: Instead of concatenating quoted strings, we build proper YAML lines: first the bucket name as a key, then the
Typeproperty indented one level deeper to maintain the resource's hierarchy. - Clean Line Breaks: Joining the output list with
\nensures each resource entry is on its own line, keeping the YAML readable and structurally valid.
内容的提问来源于stack exchange,提问作者Igor L.
相关产品推荐
相关产品推荐

