简体   繁体   中英

Invoking an AWS lambda function through step functions in Golang

I've got an AWS step function that invokes a lambda function written in Golang. For some reason it looks like the lambda function is not able to read the input to the step function.

The lambda function -

package main

import (
    "fmt"

    "github.com/aws/aws-lambda-go/lambda"
)

type InEvent struct {
    Name string `json:"name"`
}

type OutEvent struct {
    Greeting string `json:"greeting"`
}

func HandleRequest(name InEvent) (OutEvent, error) {
    var result OutEvent
    result.Greeting = fmt.Sprintf("Hello %s!", name.Name)
    return result, nil
}

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

The step function -

{
  "Comment": "A Hello World example of the Amazon States Language using Pass states",
  "StartAt": "Invoke Lambda function",
  "States": {
    "Invoke Lambda function": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName": "arn:aws:lambda:us-west-1:301438421794:function:SF1_1:$LATEST",
        "Payload": {
          "Input.$": "$"
        }
      },
      "End": true
    }
  }
}

Input to the step function -

{
  "name": "ABCDE"
}

Output of the step function -

{
  "ExecutedVersion": "$LATEST",
  "Payload": {
    "greeting": "Hello !"
  },
  "SdkHttpMetadata": {
    "AllHttpHeaders": {
      "X-Amz-Executed-Version": [
        "$LATEST"
      ],
      "x-amzn-Remapped-Content-Length": [
        "0"
      ],
      "Connection": [
        "keep-alive"
      ],
      "x-amzn-RequestId": [
        "5f297331-8b1a-49a0-9ad9-17a78ec1cfd0"
      ],
      "Content-Length": [
        "22"
      ],
      "Date": [
        "Fri, 25 Dec 2020 19:58:20 GMT"
      ],
      "X-Amzn-Trace-Id": [
        "root=1-5fe6445c-5cb6860d2559230940506a2f;sampled=0"
      ],
      "Content-Type": [
        "application/json"
      ]
    },
    "HttpHeaders": {
      "Connection": "keep-alive",
      "Content-Length": "22",
      "Content-Type": "application/json",
      "Date": "Fri, 25 Dec 2020 19:58:20 GMT",
      "X-Amz-Executed-Version": "$LATEST",
      "x-amzn-Remapped-Content-Length": "0",
      "x-amzn-RequestId": "5f297331-8b1a-49a0-9ad9-17a78ec1cfd0",
      "X-Amzn-Trace-Id": "root=1-5fe6445c-5cb6860d2559230940506a2f;sampled=0"
    },
    "HttpStatusCode": 200
  },
  "SdkResponseMetadata": {
    "RequestId": "5f297331-8b1a-49a0-9ad9-17a78ec1cfd0"
  },
  "StatusCode": 200
}

I expect Output.Payload.greeting to be "Hello ABCDE." but instead Output.Payload.greeting is "Hello !"

What went wrong here? Why is the name variable in the lambda function not storing the input correctly? Why does "ABCDE" turn into an empty string within the lambda function?

When I directly test the lambda function on the lambda console without using step functions, everything works fine.

The Payload part in your state machine definition is shaping the input passed to Lambda:

"Payload": {
    "Input.$": "$"
}

So the actual input of Lambda will be:

{ 
    Input: 
        { 
            name: 'ABCDE' 
    } 
}

So you need to consider that in your Golang code or change that Payload part in your state machine definition.

Be careful when sharing the code and example where you have your account id as well being exposed

Below is the working state machine with your golang code

    {
    "Comment": "A Hello World example of the Amazon States Language using Pass states",
    "StartAt": "Invoke Lambda function",
    "States": {
        "Invoke Lambda function": {
        "Type": "Task",
        "Resource": "arn:aws:states:::lambda:invoke",
        "Parameters": {
            "FunctionName": "arn:aws:lambda:us-east-1:301438421794:function:testfunction",
            "Payload": {
            "name.$": "$.name"
            }
        },
        "ResultPath": "$.lambdaOutput",
        "End": true
        }
    }
    }

Produces the output like below:

    {
    "name": "suresh",
    "lambdaOutput": {
        "ExecutedVersion": "$LATEST",
        "Payload": {
        "greeting": "Hello suresh!"
        },
        "SdkHttpMetadata": {
    ....
    }

You can further slice the output by amending like this

 "OutputPath": "$.lambdaOutput.Payload",

This will give you exact output

    {
    "greeting": "Hello suresh!"
    }

Take a lookhere . This is the best resource I can recommend to get started with step function.

For handling the data in between functions keep this handy. For handling the inputs and outputs this also is pretty useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM