我有一个为部署 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 运行时,它会触发该函数,并且我在 CloudWatch 中从函数中的 fmt.Printf 语句中找到以下日志:events.CodePipelineJob{ID:"", AccountID:"", Data:events.CodePipelineData{ActionConfiguration:events.CodePipelineActionConfiguration{Configuration:events.CodePipelineConfiguration{FunctionName:"", UserParameters:""}}, InputArtifacts:[]events。 CodePipelineInputArtifact(nil), OutPutArtifacts:[]events.CodePipelineOutputArtifact(nil), ArtifactCredentials:events.CodePipelineArtifactCredentials{SecretAccessKey:"", SessionToken:"", AccessKeyID:""}, ContinuationToken:""}}
job 参数是一个空对象,并且没有像我期望的那样绑定(bind)到 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)
}
关于amazon-web-services - 为什么我的 Lambda 函数在从 CodePipeline 调用时收不到事件数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63804605/