ホームページ  >  記事  >  バックエンド開発  >  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": [
               ...
            ],
            ...
        }
    }
}

解決

送信される AWS CodePipeline JSON イベントのアンマーシャリングに役立つ events.CodePipelineJobEvent が含まれる github.com/aws/aws-lambda-go/events パッケージ リンクを使用します

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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。