Skip to content
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

chalice deploy import error, with layers #1200

Open
dzhang opened this issue Aug 6, 2019 · 7 comments
Open

chalice deploy import error, with layers #1200

dzhang opened this issue Aug 6, 2019 · 7 comments
Labels

Comments

@dzhang
Copy link

@dzhang dzhang commented Aug 6, 2019

Hi

I got module not found error when deploying, I specify the function to use layers
{ "version": "2.0", "app_name": "myapp", "stages": { "dev": { "api_gateway_stage": "api", "manage_iam_role": false, "iam_role_arn": "arn", "lambda_functions": { "myfunc": { "layers": ["layer1", "layer2"] } } } }, }

I did not specify the module in requirements.txt since I have the layers specified. I probably missing something, could not find much docs on layers for chalice.

Thanks
Dong

@jamesls
Copy link
Member

@jamesls jamesls commented Aug 8, 2019

You've got the syntax right. If you're using your own layer, I'd double check you've got the layer built correctly. I'll put together some docs for this.

@markalexander
Copy link

@markalexander markalexander commented Aug 9, 2019

I think the OP means they get an import error at local packaging time, rather than when invoking the lambda function.

MWE using the official AWS SciPy layer (which should get rid of wrongly-built-layer issues):

First, create a new project:

chalice new-project layer-test

Then edit some files:

app.py:

from chalice import Chalice
import scipy

app = Chalice(app_name='layer-test')


@app.route('/')
def index():
    return {'version': scipy.__version__}

.chalice/config.json:

{
  "version": "2.0",
  "app_name": "layer-test",
  "stages": {
    "prod": {
      "api_gateway_stage": "api",
      "lambda_functions": {
        "api_handler": {
          "layers": [
            "arn:aws:lambda:us-east-1:668099181075:layer:AWSLambda-Python36-SciPy1x:2"
          ]
        }
      }
    }
  }
}

Leave requirements.txt blank (otherwise it will get packaged, which we don't want).

Now do:

chalice package --stage prod /tmp/packaged

(or chalice deploy etc.). You'll get an import error because you don't have scipy installed.

I think the issue that you're having is that you need to pip install scipy (or whatever you are importing) while keeping it out of requirements.txt. Then it's available to package locally but doesn't end up in the .zip.

@dzhang
Copy link
Author

@dzhang dzhang commented Aug 9, 2019

Hi
yes, that is exactly my problem, I don't have the module installed locally, since it is in the layer, was hoping chalice can just deploy it. seems I still have to install all modules locally.

Thanks
Dong

@kennymatsudo
Copy link

@kennymatsudo kennymatsudo commented Apr 23, 2020

Hi all,

Ran into the same issue presented here but was able to work around this by wrapping my imports in a try/catch. This allowed Chalice to deploy and because my layer was defined in the config.json, my application ran successfully.

@jamesls
Copy link
Member

@jamesls jamesls commented Apr 23, 2020

There was another issue where people were running into this issue as well. Due to how Chalice works, we have to import your app.py file. A simple option would be to set some sort of environment variable you could check whenever we run any chalice CLI commands. The main benefit would be that it's more explicit than a try/except.

@vrinda1410
Copy link

@vrinda1410 vrinda1410 commented Apr 27, 2020

Hi @jamesls
Could you please elaborate the point mentioned above
"A simple option would be to set some sort of environment variable you could check whenever we run any chalice CLI commands. "

I am looking to solve this very problem in my project and want to explore all available options over try/catch workaround.

Thanks!

@jamesls
Copy link
Member

@jamesls jamesls commented Apr 28, 2020

I can't find the original comment (I believe it was from @stealthycoin), but whenever we run a commands like chalice deploy/package we could set an env var to let you know that we're only importing your app for introspection purposes, and it's not actually running on lambda. I don't remember the name proposed, but maybe CHALICE_CLI_MODE or something like that.

Another option is to make better use of the AWS_EXECUTION_ENV, which is always set when you run in Lambda, but right now we also set that var when we instantiate the app object. They'd both work the same though, something like:

if 'AWS_EXECUTION_ENV' in os.environ:
    # Running in Lambda, need to import modules.
    import package_from_layer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.