I like to be able to build an AWS Lambda in golang and be able to test it locally.
The AWS Lambda sets some environment variables. Use those to check if you’re in the Lambda runtime.
package main
import (
"context"
"fmt"
"os"
"github.com/aws/aws-lambda-go/lambda"
)
type MyEvent struct {}
var isLambda bool
func init() {
runtime := os.Getenv("AWS_LAMBDA_RUNTIME_API")
if runtime != "" {
isLambda = true
}
}
func run(ctx context.Context, event MyEvent) error {
return nil
}
func main() {
if isLambda {
lambda.Start(run)
} else {
run(context.Background(), MyEvent{})
}
}