Home  >  Article  >  Backend Development  >  Parse UserParameters sent from AWS CodePipeline to AWS Lambda in Go

Parse UserParameters sent from AWS CodePipeline to AWS Lambda in Go

Susan Sarandon
Susan SarandonOriginal
2024-10-03 06:41:30316browse

Parse UserParameters sent from AWS CodePipeline to AWS Lambda in Go

Context

I was trying to setup the UserParameters configuration within the AWS CodePipeline Template being generated,

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

While testing it out on an AWS Lambda, written in Go, it took a bit longer than usual to find out the Function Definition for the Handler, to parse the AWS CodePipeline JSON Event that would be sent, For Example:

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

Solution

Use the github.com/aws/aws-lambda-go/events package link which contains the events.CodePipelineJobEvent that helps unmarshal the AWS CodePipeline JSON event being sent

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)
}

References

  • 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

The above is the detailed content of Parse UserParameters sent from AWS CodePipeline to AWS Lambda in Go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn