Skip to content

Cannot work with Serverless-webpack #202

@fisher-he

Description

@fisher-he

Runtime: Nodejs 14x
Serverless: 2.41.2
Localstack: 1.3.1
Serverless-localstack: 1.0.1
Serverless-webpack:5.5.0
Webpack:5.39.0

serverless.yml:

...
plugins:
  - serverless-webpack
  - serverless-localstack

custom:
  webpack:
    packager: yarn
    includeModules: false
    keepOutputDirectory: true
  localstack:
    debug: true
    autostart: true
    stages:
      - local
    lambda:
      mountCode: true
functions:
  parseDeviceLog:
    handler: device-logs/parseDeviceLog.default
    memorySize: 512
    timeout: 120

docker-compose.yml:

version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack:1.3.1
    ports:
      - "127.0.0.1:4566:4566" # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559" # external services port range
    environment:
      - DEBUG=1
      - LAMBDA_EXECUTOR=local
      - LAMBDA_REMOTE_DOCKER=0
      - DOCKER_HOST=unix:///var/run/docker.sock
      - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-}
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_ACCESS_KEY_ID=test
      - AWS_SECRET_ACCESS_KEY=test
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/Users/fhe/workplace/serverless/my-ops:/var/lib/my-ops"

webpack.config.js:

const path = require('path');
const slsw = require('serverless-webpack');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = {
	entry: slsw.lib.entries,
	target: 'node',
	output: {
		libraryTarget: 'commonjs',
		path: path.join(__dirname, '.webpack'),
		filename: '[name].js',
		sourceMapFilename: '[file].map'
	},
	mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
	devtool: slsw.lib.webpack.isLocal ? 'eval-cheap-module-source-map' : 'cheap-module-source-map',
	optimization: {
		minimizer: [new TerserPlugin({ terserOptions: { mangle: false } })],
		minimize: false
	},
	externals: ['aws-sdk']
	module: {
		rules: [
			{
				test: /\.(js|ts)$/,
				exclude: /(node_modules)/,
				use: [
					{
						loader: 'babel-loader'
					}
				]
			}
		]
	},
	resolve: {
		alias: {
			'core-js': path.join(__dirname, 'node_modules/core-js'),
			'regenerator-runtime': path.join(__dirname, 'node_modules/regenerator-runtime'),
			mockData: path.resolve(__dirname, 'mockData'),
			'mb-ops': path.resolve(__dirname, './')
		},
		extensions: ['.ts', '.js']
	}
};

When I try to deploy my serverless project to LocalStack(sls deploy -s local) , it will get a no such file or directory error.
see logs:

localstack_main  | botocore.exceptions.ClientError: An error occurred (InternalFailure) when calling the CreateFunction operation (reached max retries: 4): Unknown error: [Errno 2] No such file or directory: '/Users/fhe/workplace/serverless/my-ops/.webpack/service/device-logs/parseDeviceLog.js' Traceback (most recent call last):
localstack_main  |   File "/opt/code/localstack/localstack/utils/files.py", line 118, in ensure_readable
localstack_main  |     with open(file_path, "rb"):
localstack_main  | FileNotFoundError: [Errno 2] No such file or directory: '/Users/fhe/workplace/serverless/mb-ops/.webpack/service/device-logs/parseDeviceLog.js'

But after packaging, the correct path of webpack is /Users/fhe/workplace/serverless/my-ops/.webpack/parseDeviceLog/device-logs/parseDeviceLog.js. It's easy to see that the difference between them is that the actual file directory after webpack packaging should be the function name instead of service.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions