Serverless-webpack: "bad handler" error when using `.ts` files for handlers

Created on 13 Jan 2019  路  1Comment  路  Source: serverless-heaven/serverless-webpack

Description

My entry point is a .ts file -- here is the corresponding code in serverless.yml:

functions:
  sheets:
    handler: ./scripts/backend/handler.ts

When I deploy and invoke the lambda function on AWS, I get the error:

{
  "errorMessage": "Bad handler ./scripts/backend/handler.ts"
}

It seems like the problem is that my webpack configuration converts the .ts files to .js files, so therefore when executing the lambda function it can't find the .ts file anymore. How do I fix this?

Here is my webpack config:

const slsw = require('serverless-webpack');
const webpack = require('webpack');

module.exports = {
    entry: slsw.lib.entries,
    target: 'node',
    mode: slsw.lib.webpack.isLocal ? "development" : "production",
    module: {
        rules: [
            {
                test: [/\.tsx?$/],
                exclude: [/node_modules/, /\.test.tsx?$/],
                use:
                    [
                        {
                            'loader': 'ts-loader'
                        }
                    ]
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            STAGE: `"${slsw.lib.options.stage}"`
        })
    ],
    resolve: {
        modules: ['node_modules', 'scripts'],
        extensions: ['.ts', '.tsx', '.json', '.js', '.jsx']
    },
};

Additional Data

  • Serverless-Webpack Version you're using: 5.2.0
  • Webpack version you're using: 4.16.5
  • Serverless Framework Version you're using: 1.36.0
  • Operating System: Mac OS X

Most helpful comment

Found the problem -- I should have added the function name in the handler attribute, i.e. ./scripts/backend/handler.hello.

>All comments

Found the problem -- I should have added the function name in the handler attribute, i.e. ./scripts/backend/handler.hello.

Was this page helpful?
0 / 5 - 0 ratings