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

无法通过AWS CodePipeline构建部署Go Lambda:BundleType需为YAML或JSON

AWS CodePipeline部署Go Lambda时CodeDeploy报错:BundleType must be either YAML or JSON

问题描述

我尝试用AWS CodePipeline构建一个最简单的Go语言Lambda函数,折腾了两周还是没成功部署。部署时CodeDeploy抛出错误:

Action execution failed - BundleType must be either YAML or JSON

虽然我在buildspec的artifacts里定义了appspec.yml,但CodeDeploy似乎找不到这个文件,请问问题出在哪里?

相关代码

main.go

package main
import (
    "context"
    "github.com/aws/aws-lambda-go/lambda"
)
func HandleRequest(ctx context.Context) (string, error) {
    return "Hello from Go!", nil
}
func main() {
    lambda.Start(HandleRequest)
}

buildspec.yml

version: 0.2
env:
  variables:
    S3_BUCKET: dlp-queuetime
    PACKAGE: dlp-queuetime-fetcher
phases:
  install:
    runtime-versions:
      golang: 1.12
    commands:
      # AWS Codebuild Go images use /go for the $GOPATH so copy the src code into that dir structure
      - mkdir -p "/go/src/$(dirname ${PACKAGE})"
      - ln -s "${CODEBUILD_SRC_DIR}" "/go/src/${PACKAGE}"
      # Print all environment variables (handy for AWS CodeBuild logs)
      - env
      # Install Lambda Go
      - go get github.com/aws/aws-lambda-go/lambda
  pre_build:
    commands:
      # Make sure we're in the project directory within our GOPATH
      - cd "/go/src/${PACKAGE}"
      # Fetch all dependencies
      - go get -t ./...
  build:
    commands:
      # Build our Go app
      - go build -o main
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  type: zip
  files:
    - appspec.yml
    - main

appspec.yml

version: 0.0
Resources:
  - dlpQueueTimeFetcher:
      Type: AWS::Lambda::Function
      Properties:
        Name: "dlpQueueTimeFetcher"
        Alias: "v0"
        CurrentVersion: "1"
        TargetVersion: "2"

解决方案

这个报错的核心是CodeDeploy无法识别你的部署包结构或AppSpec文件,我帮你梳理几个关键问题和修复步骤:

1. 你用错了AppSpec的格式!

当前的appspec.yml是给EC2实例部署用的配置,完全不适合Lambda函数。Lambda的AppSpec有自己的专属结构,需要指定代码位置、运行时等信息。替换成下面的内容:

version: 0.0
Resources:
  - dlpQueueTimeFetcher:
      Type: AWS::Lambda::Function
      Properties:
        Name: "dlpQueueTimeFetcher"
        CodeUri: main  # 指向打包后的Go可执行文件
        Runtime: go1.x
        Handler: main  # Go Lambda的Handler就是编译后的可执行文件名

2. Buildspec的打包路径和编译环境不对

  • 必须编译Linux平台的二进制文件:Lambda运行在Linux环境下,你的build命令需要添加跨平台编译参数,否则打包后的文件在Lambda上无法执行。
  • 指定正确的打包目录:你的buildspec没有指定base-directory,可能导致CodeBuild打包的目录不对,AppSpec文件没被放到部署包的根目录。

修正后的buildspec.yml:

version: 0.2
env:
  variables:
    S3_BUCKET: dlp-queuetime
    PACKAGE: dlp-queuetime-fetcher
phases:
  install:
    runtime-versions:
      golang: 1.21  # 建议升级到较新的Go版本,1.12已经不再维护了
    commands:
      - mkdir -p "/go/src/$(dirname ${PACKAGE})"
      - ln -s "${CODEBUILD_SRC_DIR}" "/go/src/${PACKAGE}"
      - env
      - go get github.com/aws/aws-lambda-go/lambda
  pre_build:
    commands:
      - cd "/go/src/${PACKAGE}"
      - go get -t ./...
  build:
    commands:
      # 编译Linux平台的可执行文件
      - GOOS=linux GOARCH=amd64 go build -o main
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  type: zip
  files:
    - appspec.yml
    - main
  base-directory: "/go/src/${PACKAGE}"  # 确保从代码所在目录打包

3. 确认AppSpec文件的位置

确保appspec.yml文件放在你的CodeBuild项目拉取的源码根目录下,这样CodeBuild才能在打包时把它包含进去。如果文件路径不对,需要在buildspec的files里指定完整路径。

4. 检查CodePipeline的部署配置

在CodePipeline的部署阶段,确认你选择的是Lambda部署类型,而不是EC2/本地服务器部署。如果选错了类型,CodeDeploy会用错误的规则解析AppSpec,自然会报错。

按这些步骤调整后,应该就能解决这个BundleType的错误,成功部署Go Lambda了!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:15:53