Created
March 31, 2022 19:12
-
-
Save percybolmer/f3d9574e7034aab3699c0758e30cd50c to your computer and use it in GitHub Desktop.
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" | |
"net/http" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
) | |
// Event is the Input Payload representation | |
type Event struct { | |
Name string `json:"name"` | |
} | |
// Our Lambda function now Accepts a APIGatewayProxyRequest and outputs a Response instead | |
func lambdaHandler(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
// This is our orignial event | |
var input Event | |
// We can find the users Payload inside event.Body as a String, Marshal it into our wanted Event format. | |
if err := json.Unmarshal([]byte(event.Body), &input); err != nil { | |
return events.APIGatewayProxyResponse{ | |
Body: err.Error(), | |
StatusCode: http.StatusInternalServerError, | |
}, nil | |
} | |
output := fmt.Sprintf("Hello From Lambda %s", input.Name) | |
return events.APIGatewayProxyResponse{ | |
Body: output, | |
StatusCode: 200, | |
}, nil | |
} | |
func main() { | |
// Start the lambda handler | |
lambda.Start(lambdaHandler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment