Skip to content

Package individually example does not work #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
felipe-augusto opened this issue Jun 15, 2018 · 6 comments
Closed

Package individually example does not work #203

felipe-augusto opened this issue Jun 15, 2018 · 6 comments
Labels

Comments

@felipe-augusto
Copy link

felipe-augusto commented Jun 15, 2018

Hi, I followed the documentation on how to package individually, but it seems that is not working. Here's my serverless.yml, closely matching the example on README:

service: test

provider:
  name: aws
  runtime: python3.6
  memorySize: 128

plugins:
  - serverless-python-requirements
custom:
  pythonRequirements:
    dockerizePip: true
    zip: true

package:
  individually: true

functions:
  func1:
    handler: index.handler
    module: function1
    events:
      - http:
          path: /func1
          method: get
  func2:
    handler: index.handler
    module: function2
    events:
      - http:
          path: /func2
          method: get

And here's my folder structure:

├── function1
│   ├── index.py
│   └── requirements.txt
├── function2
│   ├── index.py
│   └── requirements.txt
├── package.json
├── serverless.yaml
└── yarn.lock

When I make a request to the func1 or func2 I get an internal server error with the following logs on both functions:

START RequestId: 6cf2a302-702d-11e8-abcd-2f24ab6d943e Version: $LATEST
Unable to import module 'index': No module named 'index'

END RequestId: 6cf2a302-702d-11e8-abcd-2f24ab6d943e

I wonder what I'm doing wrong. Thanks!

@felipe-augusto
Copy link
Author

Actually, it seem that the problem is trying to zip: true and package individually, has anyone managed to do it?

@felipe-augusto
Copy link
Author

I discovered why I got the following error:

Unable to import module 'index': No module named 'index'

When I use the following config,

package:
  individually: true

functions:
  func1:
    handler: index.handler
    module: function1
    package:
      include:
        - 'function1/*.py'

It generates the following structure on AWS Lambda:

image

And everything works as expected, but when using zip: true option, as below:

custom:
  pythonRequirements:
    zip: true

package:
  individually: true

functions:
  func1:
    handler: index.handler
    module: function1
    package:
      include:
        - 'function1/*.py'

It changes the folder structure to:

image

So, I had to update my config to match the folder structure:

functions:
  func1:
    handler: function1/index.handler
    module: function1
    package:
      include:
        - 'function1/*.py'

But, the code that unzip_requirements.py fails because the piece of code below refers to the wrong path, and I throws an error that import were not found.

os.environ.get('LAMBDA_TASK_ROOT', os.getcwd()), '.requirements.zip')
// Unable to import module 'function1/index': No module named 'langdetect'

@dschep dschep added the bug label Jun 18, 2018
@dschep
Copy link
Contributor

dschep commented Jun 18, 2018

aha. Good job tracking that down! Should be a relatively easy fix.

dschep pushed a commit that referenced this issue Jul 10, 2018
This PR fixes two issues I encountered while using `individually: true` option.

**1. Should package only the given function when invoked by `sls deploy function --function $FN_NAME`**

Suppose using `individually: true`. If you'd like to deploy a single function using `sls deploy function` command you'd expect the plugin to package only the module that contains the function specified. The current implementation, however, runs packaging for every module in `serverless.yml` definition, taking as much time as deploying all applications just to deploy a single function.

I simply added explicit `targetFuncs` property to filter target function if any specified in command line option.

**2. `moveModulesUp` should be called regardless of `zip` option**

When `individually: true`, each deployed function has its module as a root directory. This is achieved by calling `moveModulesUp`, but this method call is ignored when `zip` option is enabled. This certainly seems to be a wrong implementation (causing #203). If `injectModule` is ensured not to be called when `zip: true`, it is safe to call `moveModulesUp` whether `zip` is enabled or not.

The fix seems to work well on my case, but it lacks new unit tests (I couldn't figure out how to compose one.) If you let me known how to add a test, I'll update the PR as soon as I can.
@felipe-augusto
Copy link
Author

@dschep I think this can be closed, since #217 was merged, am I right?

@dschep
Copy link
Contributor

dschep commented Sep 12, 2018

Yup. Thanks for following up!

@dschep dschep closed this as completed Sep 12, 2018
@babus
Copy link

babus commented Aug 8, 2019

The value of module should in the format /path/toindex/ and not path.toindex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants