gonzo
gonzo

Reputation: 619

How can I test AWS Lambda functions locally?

Explain to me please what is the best way to locally test the lambda function. I used sam local and this solution https://github.com/lambci/docker-lambda for testing, but for example, where I invoke one lambda from another error occurs. In general, I can't make stubs for methods since lambda runs in a container

Upvotes: 36

Views: 61308

Answers (4)

diogoa
diogoa

Reputation: 171

Adding here one more option, just because of the it's simplicity. You can download locally the AWS lambda runtime environment as described in AWS documentation

mkdir -p ~/.aws-lambda-rie && curl -Lo ~/.aws-lambda-rie/aws-lambda-rie \
https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie \
&& chmod +x ~/.aws-lambda-rie/aws-lambda-rie               

To test the lambda function locally run you can then run on a terminal session:

~/.aws-lambda-rie/aws-lambda-rie go run main.go

This will start a server listening on port 8080. To actually trigger the lambda function, you can finally run on another terminal session:

curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{"Name": "World"}'

Upvotes: 7

Bogdan Onu
Bogdan Onu

Reputation: 41

For local manual testing (not unit testing) with sam cli you can specify the environment varilables file with the -n, --env-vars PATHoption and use your real resource identifiers like you would normally do within your Cloud Formation template (refer to the official documentation for more informations).

This should let you invoke other functions directly and use a real DynamoDB table and all other AWS Cloud resources.

Note: if you use VSCode you can try this helper extension.

Upvotes: 4

mel3kings
mel3kings

Reputation: 9405

This is how I test local lambda functions without Serverless frameworks, I run an HTTP post on local (quite easy setup for Go)

  • decouple lambda logic like so:
func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    _, _ = pretty.Println("parsed:", request.Body)
    return events.APIGatewayProxyResponse{Body: "response is working", StatusCode: 200}, nil
}
  • main function then checks if it is a local instance then run local post endpoint, else start lambda
func main() {
    environment := loadEnv()
    if environment == "develop" {
        router.NewRouter()
        select {}
    } else {
        lambda.Start(lambdahandler.HandleRequest)
    }
}
  • in between you have an Adapter Pattern that converts your http request to whatever your lambda function accepts, for example:
func MapToApiGateway(w http.ResponseWriter, r *http.Request) (interface{}, error) {
    request := new(EmailResponderRequest)
    if err := json.NewDecoder(r.Body).Decode(request); err != nil {
        return err.Error(), err
    }
    apiGatewayRequest := mapHttpRequestToGatewayRequest(*request)
    events, err := lambdahandler.HandleRequest(nil, apiGatewayRequest)
    if err != nil {
        return err.Error(), err
    }
    return events, nil
}

Upvotes: 3

gayashanbc
gayashanbc

Reputation: 1014

There are a couple of options. Following two are some popular ones.

Upvotes: 30

Related Questions