首页  >  文章  >  后端开发  >  在 Go 中解析从 AWS CodePipeline 发送到 AWS Lambda 的 UserParameters

在 Go 中解析从 AWS CodePipeline 发送到 AWS Lambda 的 UserParameters

Susan Sarandon
Susan Sarandon原创
2024-10-03 06:41:30328浏览

Parse UserParameters sent from AWS CodePipeline to AWS Lambda in Go

语境

我尝试在生成的 AWS CodePipeline 模板中设置 UserParameters 配置,

Name: ...
Actions:
  - Name: Invoke-Lambda
    ActionTypeId:
      Category: Invoke
      Owner: AWS
      Provider: Lambda
      Version: '1'
    Configuration:
      FunctionName: exampleLambdaFunction
      UserParameters: '{"example":"user-parameters"}'

在用 Go 编写的 AWS Lambda 上对其进行测试时,需要比平时更长的时间才能找到处理程序的函数定义,以解析将发送的 AWS CodePipeline JSON 事件,例如:

{
    "CodePipeline.job": {
        "id": "11111111-abcd-1111-abcd-111111abcdef",
        "accountId": "111111111111",
        "data": {
            "actionConfiguration": {
                "configuration": {
                    "FunctionName": "exampleLambdaFunction",
                    "UserParameters": "{\"example\":\"user-parameters\"}"
                }
            },
            "inputArtifacts": [
               ...
            ],
            ...
        }
    }
}

解决方案

使用 github.com/aws/aws-lambda-go/events 包链接,其中包含 events.CodePipelineJobEvent,可帮助解组正在发送的 AWS CodePipeline JSON 事件

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.CodePipelineJobEvent) (string, error) {
    fmt.Printf("received codepipeline event function name: %+v\n", event.CodePipelineJob.Data.ActionConfiguration.Configuration.FunctionName)
    fmt.Printf("received codepipeline event user parameters: %+v\n", event.CodePipelineJob.Data.ActionConfiguration.Configuration.UserParameters)
    return "cool", nil
}

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

参考

  • https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-Lambda.html
  • https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-Lambda.html#action-reference-Lambda-event
  • https://github.com/aws/aws-lambda-go/blob/main/events/codepipeline_job.go

以上是在 Go 中解析从 AWS CodePipeline 发送到 AWS Lambda 的 UserParameters的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn