使用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节点时,会生成格式无效的参数覆盖项,导致部署时触发参数格式错误。
解决办法
有两种可行方案:
- 删除空的Environment配置块:如果不需要环境变量,直接去掉整个
Environment节点,修改后的Lambda配置片段如下:ExpressLambdaFunction: Type: AWS::Serverless::Function Properties: CodeUri: ./ Handler: lambda.handler MemorySize: 512 Runtime: nodejs14.x Timeout: 30 Events: # 事件配置保持不变 - 添加占位环境变量:如果需要保留
Environment结构,给Variables添加至少一个键值对,比如:Environment: Variables: PLACEHOLDER_VAR: "dummy"
修改配置后重新执行sam build和sam deploy --guided即可正常部署。
内容的提问来源于stack exchange,提问作者Joshua Foxworth
相关产品推荐
相关产品推荐

