Skip to content

Instantly share code, notes, and snippets.

@angelodlfrtr
Created April 15, 2019 14:51
Show Gist options
  • Select an option

  • Save angelodlfrtr/967f8b1b747d83c6f6fca53685404b10 to your computer and use it in GitHub Desktop.

Select an option

Save angelodlfrtr/967f8b1b747d83c6f6fca53685404b10 to your computer and use it in GitHub Desktop.
Local invoke go aws lambda function
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/djhworld/go-lambda-invoke/golambdainvoke"
"github.com/fatih/color"
"github.com/k0kubun/pp"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"time"
)
func main() {
functionPath := flag.String("path", ".", "The function path")
eventSourceFile := flag.String("event", "./events/base.json", "The event as json source file")
rpcPort := flag.Int("port", 8899, "The RPC port")
// Parse flags
flag.Parse()
color.Magenta(">> -----------------------------------------")
color.Magenta(">> Test GO lambda function -----------------")
color.Magenta(">> -----------------------------------------")
// Get absolute path for lambda func
realFunctionPath, err := filepath.Abs(*functionPath)
realEventSourceFile, err := filepath.Abs(*eventSourceFile)
if err != nil {
color.Red("!! Invalid path")
color.Red(err.Error())
return
}
fmt.Println("")
color.Magenta(">> -----------------------------------------")
color.Magenta(">> Function root : %s", realFunctionPath)
color.Magenta(">> Event source file (JSON) : %s", realEventSourceFile)
color.Magenta(">> -----------------------------------------")
fmt.Println("")
// Read event source file
eventFileData, readFileErr := ioutil.ReadFile(realEventSourceFile)
if readFileErr != nil {
color.Red("!! Unable to read event source file")
color.Red(readFileErr.Error())
return
}
// Parse event json file
var eventJson map[string]interface{}
json.Unmarshal(eventFileData, &eventJson)
// Build function via go binary
goBuildCmd := exec.Command(
"go",
"build",
"-o",
"/tmp/_tmp_go_testlambdafunc",
fmt.Sprintf("%s/main.go", realFunctionPath),
)
if err = goBuildCmd.Run(); err != nil {
color.Red("!! Unable to build target function")
color.Red(err.Error())
return
}
color.Green(">> Successfully built main.go")
// Run function
functionExecCmd := exec.Command("/tmp/_tmp_go_testlambdafunc")
functionExecCmd.Env = os.Environ()
functionExecCmd.Env = append(functionExecCmd.Env, fmt.Sprintf("_LAMBDA_SERVER_PORT=%d", *rpcPort))
if err := functionExecCmd.Start(); err != nil {
color.Red(err.Error())
return
}
time.Sleep(1 * time.Second)
color.Green(">> Function running")
rpcResponse, err := golambdainvoke.Run(golambdainvoke.Input{
Port: *rpcPort,
Payload: eventJson,
})
if err != nil {
color.Red("!! Error on rpc call")
color.Red(err.Error())
functionExecCmd.Process.Kill()
return
}
var rpcResponseParsed map[string]interface{}
json.Unmarshal(rpcResponse, &rpcResponseParsed)
color.Magenta(">> -----------------------------------------")
fmt.Println("")
pp.Println(rpcResponse)
fmt.Println("")
color.Magenta(">> -----------------------------------------")
color.Magenta(">> -----------------------------------------")
fmt.Println("")
pp.Println(rpcResponseParsed)
fmt.Println("")
color.Magenta(">> -----------------------------------------")
functionExecCmd.Process.Kill()
color.Green(">> Function exited")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment