」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在 Go 中解析從 AWS CodePipeline 傳送到 AWS Lambda 的 UserParameters

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

發佈於2024-11-01
瀏覽:169

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
版本聲明 本文轉載於:https://dev.to/prithvijj/parse-userparameters-sent-from-aws-codepipeline-to-aws-lambda-in-go-ffe?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3