amazon-web-services - 为什么我的 Lambda 函数在从 CodePipeline 调用时收不到事件数据?
问题描述
我有一个为部署 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 参数是一个空对象,并且没有像我期望的那样绑定到 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)
}
推荐阅读
- angular5 - 开放层没有通过geojson获得准确的区域
- qt - qml中如何导出字段信息
- ios - “动态”属性也必须是“@objc”,即使已经添加了“动态”,枚举变量也会发生错误
- python - 使用 Bash 脚本创建 Python virtualenv 时无法获得隔离环境
- python - 如何将静态位图帧从 GIF 馈送到 wxpython 形状的帧
- c# - ASP.NET MVC RedirectToAction 不刷新页面
- php - 在循环内执行 SQL SELECT 查询
- android - 查询sqlite导入的数据库时Android Studio“没有这样的表”错误
- python - 尝试在 pyTMX 中获取 TileImageByGid 时出现 AttributeError
- kubernetes - 有没有办法在 kubernetes 部署(或 statefulset)中为每个 pod 创建一个持久卷?