登录
首页 >  Golang >  Go问答

为什么我的 Lambda 函数在 CodePipeline 中调用时无法接收事件数据?

来源:stackoverflow

时间:2024-02-21 15:54:23 138浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《为什么我的 Lambda 函数在 CodePipeline 中调用时无法接收事件数据?》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

我有一个为部署 go 应用程序而构建的 codepipeline,现在我尝试使用 golang-migrate 使其应用数据库迁移。我有一个用 go 编写的 lambda 函数来应用迁移,但是当从 codepipeline 调用它时,它没有收到带有修订位置、用户参数等的事件数据。为了简单起见,我删除了迁移代码并将其替换为用于简单地将事件数据写入 cloudwatch 的代码:

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(ctx context.context, job events.codepipelinejob) (string, error) {    
    fmt.printf("%#v", job)    
    return "success", nil
}

func main() {
    lambda.start(handler)
}

当codepipeline运行时,它会触发该函数,我从函数中的fmt.printf语句在cloudwatch中找到以下日志:

events.codepipelinejob{id:"", accountid:"", data:events.codepipelinedata{actionconfiguration:events.codepipelineactionconfiguration{配置:events.codepipelineconfiguration{functionname:"", userparameters:""}}, inputartifacts:[ ]events.codepipelineinputartifact(nil)、outputartifacts:[]events.codepipelineoutputartifact(nil)、artifactcredentials:events.codepipelineartifactcredentials{secretaccesskey:"", sessiontoken:"", accesskeyid:""}, continuationtoken:""}}

作业参数是一个空对象,并且没有像我期望的那样绑定到 codepipeline 事件数据。我所做的所有研究表明它应该接收此处定义的 codepipelinejob 事件:

https://github.com/aws/aws-lambda-go/blob/master/events/codepipeline_job.go

我已确认,如果我使用 python lambda 函数,则会收到预期的 json 事件格式:

import json
import logging

logger = logging.getlogger()
logger.setlevel(logging.info)

def main(event, context):    
    logger.info("event: " + str(event))

    return "success"

当我更改 codepipeline 以调用 python 函数时,包含正确数据的事件将成功写入 cloudwatch。它看起来类似于:

{
   "CodePipeline.job":{
      "id":"11111111-abcd-1111-abcd-111111abcdef",
      "accountId":"111111111111",
      "data":{
         "actionConfiguration":{
            "configuration":{
               "FunctionName":"MyLambdaFunctionForAWSCodePipeline",
               "UserParameters":"some-input-such-as-a-URL"
            }
         },
         "inputArtifacts":[
            {
               "location":{
                  "s3Location":{
                     "bucketName":"the name of the bucket configured as the pipeline artifact store in Amazon S3, for example codepipeline-us-east-2-1234567890",
                     "objectKey":"the name of the application, for example CodePipelineDemoApplication.zip"
                  },
                  "type":"S3"
               },
               "revision":null,
               "name":"ArtifactName"
            }
         ],
         "outputArtifacts":[            
         ],
         "continuationToken":"A continuation token if continuing job",
         "encryptionKey":{
            "id":"arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab",
            "type":"KMS"
         }
      }
   }
}

所以,我的问题是,为什么我的 python lambda 函数会写入事件,而我的 go 函数不会?我定义参数的方式有问题吗?


解决方案


我为输入参数使用了错误的事件类型。当我将参数类型更改为 events.codepipelineevent 时,该函数开始正确记录事件详细信息。输入事件中的根 json 元素名为 codepipeline.job。 events.codepipelineevent 结构定义如下:

codepipelineevent struct {
    codepipelinejob codepipelinejob `json:"codepipeline.job"`
}

一旦我将参数切换到正确的类型,它就开始正确匹配根元素并提取值。这是更新后的功能:

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func Handler(ctx context.Context, event events.CodePipelineEvent) (string, error) {
    fmt.Printf("%#v", event)
    return "Success", nil
}

func main() {
    lambda.Start(Handler)
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《为什么我的 Lambda 函数在 CodePipeline 中调用时无法接收事件数据?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>