Skip to content

Instantly share code, notes, and snippets.

@p4tin
Created November 28, 2018 17:39
Show Gist options
  • Save p4tin/c96bd86ee468401658c3557ecf2c8ff0 to your computer and use it in GitHub Desktop.
Save p4tin/c96bd86ee468401658c3557ecf2c8ff0 to your computer and use it in GitHub Desktop.
Invoke Lamdba in your local for testing
package golambdainvoke
import (
"encoding/json"
"errors"
"fmt"
"net/rpc"
"time"
"github.com/aws/aws-lambda-go/lambda/messages"
lc "github.com/aws/aws-lambda-go/lambdacontext"
)
const functioninvokeRPC = "Function.Invoke"
//Run a Go based lambda, passing the configured payload
//note that 'payload' can be anything that can be encoded by encoding/json
func Run(port int, payload interface{}) ([]byte, error) {
return RunWithClientContext(port, payload, nil)
}
//RunWithClientContext a Go based lambda, passing the configured payload and ClientContext
//note that 'payload' can be anything that can be encoded by encoding/json
func RunWithClientContext(port int, payload interface{}, clientContext *lc.ClientContext) ([]byte, error) {
request, err := createInvokeRequest(payload, clientContext)
if err != nil {
return nil, err
}
// 2. Open a TCP connection to the lambda
client, err := rpc.Dial("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
// 3. Issue an RPC request for the Function.Invoke method
var response messages.InvokeResponse
if err = client.Call(functioninvokeRPC, request, &response); err != nil {
return nil, err
}
if response.Error != nil {
return nil, errors.New(response.Error.Message)
}
return response.Payload, nil
}
func createInvokeRequest(payload interface{}, clientContext *lc.ClientContext) (*messages.InvokeRequest, error) {
payloadEncoded, err := json.Marshal(payload)
if err != nil {
return nil, err
}
var clientContextEncoded []byte
if clientContext != nil {
b, err := json.Marshal(clientContext)
if err != nil {
return nil, err
}
clientContextEncoded = b
}
var t time.Time = time.Now()
return &messages.InvokeRequest{
Payload: payloadEncoded,
RequestId: "0",
XAmznTraceId: "",
Deadline: messages.InvokeRequest_Timestamp{
Seconds: int64(t.Unix()),
Nanos: int64(t.Nanosecond()),
},
InvokedFunctionArn: "",
CognitoIdentityId: "",
CognitoIdentityPoolId: "",
ClientContext: clientContextEncoded,
}, nil
}
package main
import (
"encoding/json"
"fmt"
...
)
func main() {
payload := `{ "payload": "test"}`
response, err := golambdainvoke.Run(8001, payload)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment