|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "net/rpc" |
| 11 | + "os" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/aws/aws-lambda-go/lambda/messages" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + LAMBDA_SERVER_PORT = "9988" |
| 20 | + LAMBDA_INVOKE_PROC_ID = "Function.Invoke" |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + |
| 25 | + var eventfile, outputfile string |
| 26 | + flag.StringVar(&eventfile, "e", "", "The AWS event template [request] you want to send to the Lamba (must be .json file).") |
| 27 | + flag.StringVar(&outputfile, "o", "", "Print the [response] sent from the AWS Lambda server (local function running).") |
| 28 | + flag.Parse() |
| 29 | + if len(strings.TrimSpace(eventfile)) <= 0 { |
| 30 | + log.Fatalln("You must specify an event [request] to send to the LAMBDA") |
| 31 | + } |
| 32 | + |
| 33 | + // Dial up to the function over localhost:9988 (reserved port used for Lambas in the AWS SDK) |
| 34 | + client, err := rpc.Dial("tcp", fmt.Sprintf("localhost:%v", LAMBDA_SERVER_PORT)) |
| 35 | + if err != nil { |
| 36 | + log.Fatalln(err) |
| 37 | + } |
| 38 | + defer client.Close() |
| 39 | + |
| 40 | + // Translate the event file to raw bytes to send in the request payload to the Lambda |
| 41 | + openEventFile, err := os.Open(eventfile) |
| 42 | + if err != nil { |
| 43 | + log.Fatalln(err) |
| 44 | + } |
| 45 | + defer openEventFile.Close() |
| 46 | + eventFileBytes, err := ioutil.ReadAll(openEventFile) |
| 47 | + if err != nil { |
| 48 | + log.Fatalln(err) |
| 49 | + } |
| 50 | + |
| 51 | + // Send the request payload to the Lambda over RPC |
| 52 | + lambdaRequest := &messages.InvokeRequest{Payload: eventFileBytes} |
| 53 | + var lambdaResponse messages.InvokeResponse |
| 54 | + err = client.Call(LAMBDA_INVOKE_PROC_ID, lambdaRequest, &lambdaResponse) |
| 55 | + if err != nil { |
| 56 | + log.Fatalln(err) |
| 57 | + } |
| 58 | + if lambdaResponse.Error != nil { |
| 59 | + log.Fatalln(lambdaResponse.Error) |
| 60 | + } |
| 61 | + |
| 62 | + decodedResponse, err := decodeResponse(lambdaResponse.Payload) |
| 63 | + if err != nil { |
| 64 | + log.Fatalln(err) |
| 65 | + } |
| 66 | + printResponse(decodedResponse, outputfile) |
| 67 | +} |
| 68 | + |
| 69 | +func printResponse(responseString string, outputfile string) { |
| 70 | + if len(strings.TrimSpace(responseString)) <= 0 { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + log.Println("LAMBDA RESPONSE:\n", responseString) |
| 75 | + if len(strings.TrimSpace(outputfile)) != 0 { |
| 76 | + file, err := os.Create(outputfile) |
| 77 | + if err != nil { |
| 78 | + log.Printf("Error occurred when creating the output file provided: [%v]\n", err) |
| 79 | + } else { |
| 80 | + file.WriteString(responseString) |
| 81 | + } |
| 82 | + file.Close() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func decodeResponse(input []byte) (string, error) { |
| 87 | + |
| 88 | + marshalledInput, err := json.Marshal(input) |
| 89 | + if err != nil { |
| 90 | + log.Fatalf("could not marshal the lambda reponse payload [%v]\n", err.Error()) |
| 91 | + } |
| 92 | + |
| 93 | + unquotedString, err := strconv.Unquote(string(marshalledInput)) |
| 94 | + if err != nil { |
| 95 | + log.Fatalf("could not remove escaped quotes from the lambda reponse [%v]\n", err.Error()) |
| 96 | + } |
| 97 | + |
| 98 | + decoded, err := base64.StdEncoding.DecodeString(unquotedString) |
| 99 | + if err != nil { |
| 100 | + log.Fatalf("could not base64 decode the lambda response string [%v]\n", err.Error()) |
| 101 | + } |
| 102 | + return string(decoded), nil |
| 103 | +} |
0 commit comments