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

使用SAM部署YAML文件失败:--parameter-overrides参数格式无效

SAM部署报错:参数格式无效问题解决

问题情况

有一个用于构建API Gateway和Lambda函数的YAML配置文件,用CloudFormation命令可正常运行,但使用SAM执行时,构建耗时约10分钟完成后部署失败,报错提示参数格式无效。

配置文件(YAML)

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Storygraf backend API

Globals:
  Function:
    Timeout: 3

Resources:
  ExpressApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev

  ExpressLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:

      CodeUri: ./
      Handler: lambda.handler
      MemorySize: 512
      Runtime: nodejs14.x
      Timeout: 30
      Events:
        ProxyApiRoot:
          Type: Api
          Properties:
            RestApiId: !Ref ExpressApi
            Path: /
            Method: ANY
            cors: true
        ProxyApiGreedy:
          Type: Api
          Properties:
            RestApiId: !Ref ExpressApi
            Path: /{proxy+}
            Method: ANY
            cors: true

执行的SAM命令

sam build
sam deploy --guided

报错信息

Error: Invalid value for '--parameter-overrides':   is not in valid format. It must look something like 'ParameterKey=KeyPairName,ParameterValue=MyKey ParameterKey=InstanceType,ParameterValue=t1.micro' or 'KeyPairName=MyKey InstanceType=t1.micro'

问题原因及解决办法

原因

报错根源在于Lambda函数配置里的Environment: Variables:是空的。CloudFormation对空变量块兼容性较好,但SAM的构建流程在处理空的Variables节点时,会生成格式无效的参数覆盖项,导致部署时触发参数格式错误。

解决办法

有两种可行方案:

  1. 删除空的Environment配置块:如果不需要环境变量,直接去掉整个Environment节点,修改后的Lambda配置片段如下:
    ExpressLambdaFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: ./
        Handler: lambda.handler
        MemorySize: 512
        Runtime: nodejs14.x
        Timeout: 30
        Events:
          # 事件配置保持不变
    
  2. 添加占位环境变量:如果需要保留Environment结构,给Variables添加至少一个键值对,比如:
    Environment:
      Variables:
        PLACEHOLDER_VAR: "dummy"
    

修改配置后重新执行sam build和sam deploy --guided即可正常部署。

内容的提问来源于stack exchange,提问作者Joshua Foxworth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 10:36:24