Skip to content

Use a less abstract Lambda handler example #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions doc_source/golang-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ The Lambda function *handler* is the method in your function code that processes

A Lambda function written in [Go](https://golang.org/) is authored as a Go executable\. In your Lambda function code, you need to include the [github\.com/aws/aws\-lambda\-go/lambda](https://github.com/aws/aws-lambda-go/tree/master/lambda) package, which implements the Lambda programming model for Go\. In addition, you need to implement handler function code and a `main()` function\.

Definitions of the structure of AWS events can be found in the [github\.com/aws/aws\-lambda\-go/events](https://github.com/aws/aws-lambda-go/tree/master/events) package\.

```
package main

import (
"fmt"
"context"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
)

type MyEvent struct {
Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
return fmt.Sprintf("Hello %s!", name.Name ), nil
func HandleAPIGatewayRequest(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{ StatusCode: 200, Body: "Hello, World!" }, nil
}

func main() {
lambda.Start(HandleRequest)
lambda.Start(HandleAPIGatewayRequest)
}
```

Expand All @@ -32,18 +31,19 @@ Note the following:
+ **context: **[AWS Lambda context object in Go](golang-context.md)\.
+ **fmt:** The Go [Formatting](https://golang.org/pkg/fmt/) object used to format the return value of your function\.
+ **github\.com/aws/aws\-lambda\-go/lambda:** As mentioned previously, implements the Lambda programming model for Go\.
+ **func HandleRequest\(ctx context\.Context, name MyEvent\) \(string, error\)**: This is your Lambda handler signature and includes the code which will be executed\. In addition, the parameters included denote the following:
+ **github\.com/aws/aws\-lambda\-go/events:** Provides type definitions for AWS service Lambda triggers.
+ **func HandleAPIGatewayRequest\(ctx context\.Context, req events.APIGatewayProxyRequest\) \(events.APIGatewayProxyResponse, error\)**: This is your Lambda handler signature and includes the code which will be executed\. In addition, the parameters included denote the following:
+ **ctx context\.Context**: Provides runtime information for your Lambda function invocation\. `ctx` is the variable you declare to leverage the information available via [AWS Lambda context object in Go](golang-context.md)\.
+ **name MyEvent**: An input type with a variable name of `name` whose value will be returned in the `return` statement\.
+ **string, error**: Returns two values: string for success and standard [error](https://golang.org/pkg/builtin/#error) information\. For more information on custom error handling, see [AWS Lambda function errors in Go](golang-exceptions.md)\.
+ **return fmt\.Sprintf\("Hello %s\!", name\), nil**: Simply returns a formatted "Hello" greeting with the name you supplied in the input event\. `nil` indicates there were no errors and the function executed successfully\.
+ **req events.APIGatewayProxyRequest**: An input type which represents an API Gateway request.
+ **events.APIGatewayProxyResponse, error**: Returns two values: an API Gateway response and standard [error](https://golang.org/pkg/builtin/#error) information\. For more information on custom error handling, see [AWS Lambda function errors in Go](golang-exceptions.md)\.
+ **return events\.APIGatewayProxyResponse{ StatusCode: 200, Body: "Hello, World!" }, nil**: Returns an API Gateway response containing the text "Hello, World"\. `nil` indicates there were no errors and the function executed successfully\.
+ **func main\(\)**: The entry point that runs your Lambda function code\. This is required\.

By adding `lambda.Start(HandleRequest)` between `func main(){}` code brackets, your Lambda function will be executed\. Per Go language standards, the opening bracket, `{` must be placed directly at end the of the `main` function signature\.

## Lambda function handler using structured types<a name="golang-handler-structs"></a>

In the example above, the input type was a simple string\. But you can also pass in structured events to your function handler:
In the example above, the input type was an API Gateway request\. But you can also pass in structured events to your function handler:

```
package main
Expand Down Expand Up @@ -176,4 +176,4 @@ func LambdaHandler() (int, error) {
func main() {
lambda.Start(LambdaHandler)
}
```
```