JavaScriptを有効にしてください

Lambda(Go)での handler is nil のerror

 ·  ☕ 1 分で読めます

AWS Lambda関数をGoで書き、テスト実行した際に以下のエラーに遭遇しました。

{
  "errorMessage": "handler is nil",
  "errorType": "errorString"
}

なんだろうと思い、aws/aws-lambda-go/lambda を確認してみると、
main関数のlambda.Start()に渡す引数は関数ハンドルであることが必要です。
例えば、以下のような引数込みの関数として渡した場合、lambda.handlerreflectHandlerで、上記エラーが返される結果となります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func newHandler(handlerFunc interface{}, options ...Option) *handlerOptions {
    // ...
    h.handlerFunc = reflectHandler(handlerFunc, h)
    return h
}

func reflectHandler(f interface{}, h *handlerOptions) handlerFunc {
    if f == nil {
        return errorHandler(errors.New("handler is nil"))
    }
    // ...
}

関数ハンドルで渡す正しい方法は公式のサンプルにもある通り、以下Goodの様に記載すれば良さそう。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
func HandleRequest(ctx context.Context) error {
    // something
}

// Good
func main() {
    lambda.Start(HandleRequest)
}

// Bad
func main() {
    lambda.Start(HandleRequest(context.Background()))
}

また、lambda.Start()に渡す引数の関数ハンドルの関数は以下のシグネチャに従っている必要もあるので、併せて確認しておいたほうが良さそう。

  • func ()
  • func () error
  • func (TIn) error
  • func () (TOut, error)
  • func (context.Context) error
  • func (context.Context, TIn) error
  • func (context.Context) (TOut, error)
  • func (context.Context, TIn) (TOut, error)
共有

ichisuke
著者
ichisuke
Engineer