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

从GitHub部署AWS CloudFormation模板提示格式错误,本地操作正常求原因

问题:GitHub执行AWS CloudFormation部署提示模板格式错误,但本地/控制台正常

我通过GitHub执行aws cloudformation deploy命令时,系统提示模板格式错误(malformed),但在本地CLI控制台执行相同命令,或者直接在AWS CloudFormation控制台创建堆栈时都能成功,请问可能是什么问题?

提供的CloudFormation模板:

AWSTemplateFormatVersion: "2010-09-09"
Description: AWS CloudFormation创建Lambda的模板。
Parameters:
  Ambiente:
    Type: String
    Description: 环境(例如开发、预生产或生产)。
    AllowedPattern: ^.*[^0-9]$
    ConstraintDescription: 必须以非数字字符结尾。
  LambdaName:  
    Type: String
    Description: 想要创建的Lambda名称。
    AllowedPattern: ^.*[^0-9]$
    ConstraintDescription: 必须以非数字字符结尾。
  Runtime:  
    Type: String
    Description: Node.js版本(例如nodejs14.x、nodejs16.x、nodejs18.x)
    AllowedPattern: ^.*[^0-9]$
    ConstraintDescription: 必须以非数字字符结尾。
   
Outputs:
  LambdaRoleARN:
    Description: Lambda执行角色。
    Value:
      Fn::GetAtt:
        - LambdaRole
        - Arn
    Export:
      Name:
        Fn::Sub: "${AWS::StackName}-ExRole"
  LambdaFunctionName:
    Value:
      Ref: LambdaFunction
  LambdaFunctionARN:
    Description: Lambda函数ARN。
    Value:
      Fn::GetAtt:
        - LambdaFunction
        - Arn
    Export:
      Name:
        Fn::Sub: arn:aws:lambda:sa-east-1:xxxxxxxxx:function:${LambdaName}
Resources:
  LambdaRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName:
        Fn::Sub: lambda-role-${LambdaName}
      AssumeRolePolicyDocument:
        Statement:
          - Action:
            - sts:AssumeRole
            Effect: Allow
            Principal:
              Service:
              - lambda.amazonaws.com
        Version: 2012-10-17
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSLambdaExecute
      Path: /
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName:
        Fn::Sub: lambda-${LambdaName}
      Description: nodejs14.x的Lambda函数。
      Handler: index.handler
      Runtime:
        Fn::Sub: ${Runtime}
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            // TODO implement
            const response = {
              statusCode: 200,
              body: JSON.stringify('Hello from Lambda!'),
            };
            return response;
          };
      MemorySize: 512
      Timeout: 60
      Role:
        Fn::GetAtt:
          - LambdaRole
          - Arn

执行的CLI命令:

aws cloudformation deploy --template-file plantilla_test.yaml --stack-name plantillalambdagithub --capabilities CAPABILITY_IAM

可能的原因及解决方法

  • 文件编码与换行符差异:GitHub Actions默认使用LF换行符,若本地模板文件是CRLF格式,提交后可能导致CloudFormation解析失败。建议将模板文件统一设置为LF换行符,提交前检查编码格式。
  • 模板路径错误:确认GitHub工作流中--template-file指定的文件路径是否正确,若工作目录切换或路径层级错误,系统可能因找不到文件误报格式错误。
  • 参数传递不完整:检查GitHub Actions中是否正确传递了模板所需的Ambiente、LambdaName、Runtime参数,参数缺失或格式不符合要求可能被判定为模板格式问题。
  • AWS CLI版本不一致:本地使用的AWS CLI版本与GitHub Actions环境中的版本可能存在差异,旧版本CLI对YAML语法兼容性较差。建议在工作流中指定使用最新稳定版AWS CLI。
  • 硬编码账号ID问题:模板Outputs部分的LambdaFunctionARN导出名称硬编码了账号IDxxxxxxxxx,在GitHub Actions环境中可能因权限或变量替换失败导致解析错误。建议替换为CloudFormation内置变量:
    Export:
      Name:
        Fn::Sub: arn:aws:lambda:sa-east-1:${AWS::AccountId}:function:${LambdaName}
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 05:14:56