serverless-localstack icon indicating copy to clipboard operation
serverless-localstack copied to clipboard

serverless-localstack plugin adding "deploy:deploy lambda handler mounted code location" hook when deactivated

Open ghost opened this issue 3 years ago • 2 comments

Ran into a bug where the serverless-localstack plugin was overwriting the handler location of my lambdas when it was supposed to be deactivated. This caused my all my api endpoints to return {"message": "Internal server error"} on each invocation

I confirmed this was the case by adding this debug call to isActive(): this.debug('serverless-localstack is active: ' + (noStageUsed || includedInStages ? 'true' : 'false'));

and adding a call to this.isActive at the start of patchTypeScriptPluginMountedCodeLocation` to then be:

if (!this.isActive() || !this.shouldMountCode() || !this.detectTypescriptPluginType()) {
      return;
    }

and that fixed my deployment issues.

My temporary workaround is to have an identical serverless template that doesn't have the serverless-localstack plugin used to then deploy to AWS, but ideally I'd like to have the single serverless template.

Example Code

My serverless template:

####
# Central Auth API infrastructure
####

service: my-sls-service

variablesResolutionMode: 20210326
provider:
  name: aws
  runtime: nodejs14.x
  stage: ${opt:stage, env:STAGE, 'dev'}
  region: ${env:AWS_REGION, 'us-east-1'}
  stackName: central-auth-api-${self:provider.stage}-${self:custom.DEVELOPER}
  environment:
    NODE_ENV: ${self:provider.stage}
  lambdaHashingVersion: '20201221'

custom:
  DEVELOPER: ${env:DEVELOPER, 'none'}
  localstack:
    debug: true
    stages:
      - local
    host: http://localhost  # optional - LocalStack host to connect to
    edgePort: 4570  # optional - LocalStack edge port to connect to
    autostart: false  # optional - Start LocalStack in Docker on Serverless deploy
    networks: # optional - attaches the list of networks to the localstack docker container after startup
      - host
      - overlay
      - central_auth_dev
    lambda:
      # This is supposed to mount the code into the lambda container, but unsure if it gets called when localstack.autostart = false
      mountCode: true
    docker:
      # Enable this flag to run "docker ..." commands as sudo
      sudo: false
  webpack:
    webpackConfig: 'webpack.config.js' # Name of webpack configuration file
    includeModules: false # Node modules configuration for packaging
    packager: 'npm' # Packager that will be used to package your external modules
    excludeFiles: src/**/*.spec.ts # Provide a glob for files to ignore
    keepOutputDirectory: true
  stages:
    local:


plugins:
  - serverless-webpack

package:
  # exclude:
  #   - config/.env.stg
  #   - config/.env.pro
  # include:
  #   - config/.env.dev

functions:
  migrations:
    handler: src/handlers/lambda-auth.lambdaHandler
    events:
      - http:
          path: ping
          method: get

Invocation commands that I tried which rewrote the lambda handlers but shouldn't have:

sls deploy
sls deploy --stage test
STAGE=test sls deploy

ghost avatar Oct 14 '21 20:10 ghost

I am also seeing this issue. When new Lambdas are created, the handler path is prefixed with .build. When the Lambda is ran, it results in an error:

{
  "errorType": "Runtime.HandlerNotFound",
  "errorMessage": ".build/src/handlers/<snip>/index.handler is undefined or not exported",
  "stack": [
    "Runtime.HandlerNotFound: .build/src/handlers/<snip>/index.handler is undefined or not exported",
    " at Object.module.exports.load (/var/runtime/UserFunction.js:246:11)",
    " at Object.<anonymous> (/var/runtime/index.js:43:30)",
    " at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
    " at Module.load (internal/modules/cjs/loader.js:950:32)",
    " at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
    " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
    " at internal/main/run_main_module.js:17:47"
  ]
}

lucaspiller avatar Mar 25 '22 15:03 lucaspiller

Hi @lucaspiller,

do you still see this error with the serverless-plugin version 1.1.2 and the latest localstack image?

steffyP avatar Oct 13 '23 14:10 steffyP