【问题标题】:Cannot build and deploy Go Lambda using AWS CodePipeline - BundleType must be either YAML or JSON无法使用 AWS CodePipeline 构建和部署 Go Lambda - BundleType 必须是 YAML 或 JSON
【发布时间】:2019-10-09 09:01:25
【问题描述】:

我正在尝试使用 AWS CodePipeline 在 Go 中构建最简单的 Lambda 函数。尽管玩了大约 2 周,但我仍然没有成功部署它。

ma​​in.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 抛出以下错误:操作执行失败 - BundleType 必须是 YAML 或 JSON

看起来 CodeDeploy 找不到我的 appspec.yml 文件,尽管它是在我的构建规范的 artifacts 部分中定义的。我在这里做错了什么?

【问题讨论】:

    标签: amazon-web-services go aws-lambda aws-code-deploy aws-codepipeline


    【解决方案1】:

    在将 CodePipeline 与 CodeDeploy 连接以进行 Lambda 部署时,您所面临的问题是众所周知的,因为 CodeDeploy 正在寻找 Yaml 或 Json appspec 文件,而 CodePipeline 提供的工件是包含 appspec 的 zip 文件:

    目前,您可以将 CloudFormation 用作管道中 Lambda 函数的部署工具。部署 Lambda 函数的基本思路如下:

    1. 为您的 Lambda 函数创建一个 SAM 模板

    2. 基本的 SAM 模板如下所示:

      AWSTemplateFormatVersion: '2010-09-09'
      Transform: 'AWS::Serverless-2016-10-31'
      Resources:
          FunctionName:
              Type: 'AWS::Serverless::Function'
              Properties:
                  Handler: index.handler
                  Runtime: nodejs6.10
                  CodeUri: ./code
      
    3. 添加目录“code”并将lambda代码文件保存在该目录中

    4. 运行命令打包上传:

      $ aws cloudformation package --template-file template.yaml --output-template packaged.yaml --s3-bucket {your_S3_bucket}
      
    5. 部署包:

      $ aws cloudformation deploy --template-file packaged.yaml --stack-name stk1 --capabilities CAPABILITY_IAM
      

    您可以将模板代码 (Step1-2) 保留在 CodeCommit/Github 中,并在 CodeBuild Step 中执行 Step4。对于第 5 步,我建议通过 CodePipeline 中的 CloudFormation 操作来完成,该操作将“packaged.yaml”文件作为输入工件提供。

    上面的过程详细在这里:https://docs.aws.amazon.com/en_us/lambda/latest/dg/build-pipeline.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-26
      • 2019-04-07
      • 2018-08-06
      • 2016-09-20
      • 2021-04-08
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      相关资源
      最近更新 更多