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

AWS SAM模板解析失败:块映射语法错误排查求助

AWS SAM模板解析错误:while parsing a block mapping 问题排查

问题场景

编写的AWS SAM模板在解析时出现Failed to parse template: while parsing a block mapping错误,错误指向Policies中BucketName下的!Sub标签位置。模板代码如下:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  example

Parameters:
  EnvironmentPrefix:
    Type: String
    Default: "test-"

Resources:
  S3JsonLoggerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/s3-json-logger.s3JsonLoggerHandler
      Runtime: nodejs18.x
      Architectures:
        - x86_64
      MemorySize: 128
      Timeout: 60
      Policies:
        - S3ReadPolicy:
            BucketName:
              !Fn::ImportValue:
                !Sub "${EnvironmentPrefix}example-bucket"
      Events:
        S3NewObjectEvent:
          Type: S3
          Properties:
            Bucket:
              !Fn::ImportValue:
                !Sub "${EnvironmentPrefix}example-bucket"
            Events: s3:ObjectCreated:*
            Filter:
              S3Key:
                Rules:
                  - Name: prefix
                    Value: "changelog/"

错误信息:

Error: Failed to parse template: while parsing a block mapping
  in "<unicode string>", line 23, column 13:
                BucketName:
                ^
expected <block end>, but found '<tag>'
  in "<unicode string>", line 25, column 17:
                    !Sub "${EnvironmentPrefix}example-b ...
                    ^

错误原因

这是YAML语法解析规则导致的错误:!Fn::ImportValue(或短格式!ImportValue)仅接受字符串类型的参数,但原模板中把!Sub标签作为嵌套的块映射项放在!Fn::ImportValue下方,YAML解析器无法识别这种结构,误以为是新的块映射,因此抛出解析失败的错误。

修复方案

将!Sub作为!ImportValue的直接参数,调整为以下两种合法写法之一,同时注意同步修正Events部分的Bucket字段(存在同样问题):

写法1:短格式标签嵌套(推荐)

Policies:
  - S3ReadPolicy:
      BucketName: !ImportValue !Sub "${EnvironmentPrefix}example-bucket"
Events:
  S3NewObjectEvent:
    Type: S3
    Properties:
      Bucket: !ImportValue !Sub "${EnvironmentPrefix}example-bucket"

写法2:完整函数语法

Policies:
  - S3ReadPolicy:
      BucketName:
        !Fn::ImportValue: !Sub "${EnvironmentPrefix}example-bucket"
Events:
  S3NewObjectEvent:
    Type: S3
    Properties:
      Bucket:
        !Fn::ImportValue: !Sub "${EnvironmentPrefix}example-bucket"

修正后的完整模板

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  example

Parameters:
  EnvironmentPrefix:
    Type: String
    Default: "test-"

Resources:
  S3JsonLoggerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src/handlers/s3-json-logger.s3JsonLoggerHandler
      Runtime: nodejs18.x
      Architectures:
        - x86_64
      MemorySize: 128
      Timeout: 60
      Policies:
        - S3ReadPolicy:
            BucketName: !ImportValue !Sub "${EnvironmentPrefix}example-bucket"
      Events:
        S3NewObjectEvent:
          Type: S3
          Properties:
            Bucket: !ImportValue !Sub "${EnvironmentPrefix}example-bucket"
            Events: s3:ObjectCreated:*
            Filter:
              S3Key:
                Rules:
                  - Name: prefix
                    Value: "changelog/"

内容的提问来源于stack exchange,提问作者Alex R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 10:55:22