首頁  >  文章  >  後端開發  >  在 Go 中解析從 AWS CodePipeline 傳送到 AWS Lambda 的 UserParameters

在 Go 中解析從 AWS CodePipeline 傳送到 AWS Lambda 的 UserParameters

Susan Sarandon
Susan Sarandon原創
2024-10-03 06:41:30189瀏覽

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