Created
September 1, 2018 13:48
-
-
Save IggyBlob/dc892851cd768c5e29d7b9a17720e4d0 to your computer and use it in GitHub Desktop.
Go Lambda Invoker - Triggers the execution of a locally-run AWS Lambda function written in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/aws/aws-lambda-go/lambda/messages" | |
"github.com/aws/aws-lambda-go/lambdacontext" | |
"log" | |
"net/rpc" | |
) | |
var clientApplication = lambdacontext.ClientApplication{ | |
InstallationID: "", | |
AppTitle: "", | |
AppVersionCode: "", | |
AppPackageName: "", | |
} | |
var clientContext = lambdacontext.ClientContext{ | |
Client: clientApplication, | |
Env: map[string]string{}, | |
Custom: map[string]string{}, | |
} | |
// lambdaServerPort specifies the port on with the local Lambda RPC endpoint listens. | |
// Before running your Lambda function locally, please ensure that the environment variable | |
// _LAMBDA_SERVER_PORT is exported and set to the same value as lambdaServerPort below. | |
// The AWS Lambda SDK uses the value of _LAMBDA_SERVER_PORT to spin up the RPC listener. | |
const lambdaServerPort = "4242" | |
func main() { | |
client, err := rpc.Dial("tcp", "localhost:"+lambdaServerPort) | |
if err != nil { | |
log.Fatal("Connection error: ", err) | |
} | |
clientContextJSON, _ := json.Marshal(clientContext) | |
request := messages.InvokeRequest{ | |
Payload: []byte("{}"), | |
RequestId: "0", | |
XAmznTraceId: "0", | |
Deadline: messages.InvokeRequest_Timestamp{10, 0}, | |
InvokedFunctionArn: "", | |
CognitoIdentityId: "0", | |
CognitoIdentityPoolId: "0", | |
ClientContext: clientContextJSON, | |
} | |
var response messages.InvokeResponse | |
client.Call("Function.Invoke", &request, &response) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if response.Error != nil { | |
log.Fatal(response.Error.Message) | |
} | |
fmt.Println("Function.Invoke = ", string(response.Payload)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment