serverless-python-requirements icon indicating copy to clipboard operation
serverless-python-requirements copied to clipboard

serverless-offline

Open alikian opened this issue 5 years ago • 4 comments

Is there any support for serverless-offline? it seems serverless-offline doesn't call this plug-in

alikian avatar Jan 23 '19 19:01 alikian

https://github.com/dherault/serverless-offline/issues/580

hugoduncan avatar Apr 10 '19 17:04 hugoduncan

I recommend creating & activating a virtualenv, installing your dependencies, then using sls offline

dschep avatar Apr 10 '19 17:04 dschep

When each function had separate dependencies I created a local plugin to make it work offline, the plugin modifies sys.path and makes sure even boto3 and friends are installed. For this to work npx sls requirements install needs to be run first.

#!/usr/bin/env node

'use strict'

const path = require('path')
const fs = require('fs')

class PythonOfflineFix {
  constructor(serverless, options) {
    this.hooks = {
      'before:offline:start:init': () => this.fixPaths(serverless)
    }
    if (options.stage === 'local') {
      serverless.service.custom.pythonRequirements.noDeploy = []
    }
  }

  async fixPaths(serverless) {
    for (const func of Object.values(serverless.service.functions)) {
      if (func.runtime.match(/python/)) {
        if (func.module) {
          func.handler = `${func.module}/${func.handler}`
          delete func.module
        }
        if (!func.environment) {
          func.environment = {}
        }
        func.environment.PYTHONPATH = await fs.promises.realpath(
          path.join('.serverless', path.dirname(func.handler), 'requirements')
        )
      }
    }
  }
}

module.exports = PythonOfflineFix

JamesKyburz avatar Nov 23 '21 09:11 JamesKyburz

@JamesKyburz I was almost forking the serverless-offline to modify that module path. Thank you!

rodrigonehring avatar Jan 28 '22 14:01 rodrigonehring