aws / aws/chalice

Built-in Authorizer defined on blueprint error when deployed with renamed files

Open
#1,806 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
11.1k
Forks
1k
Avg merge
1d 22h
Merged PRs (30d)
2

Description

Hi,
I've discovered a strange bug with lambda authorizers with blueprints and renaming files.

Steps to reproduce:
1) Create a new project (chalice version = 1.25.0)
2) Setup project structure
```
testBug
├── app.py
├── chalicelib
│   ├── __init__.py
│   └── blueprint.py
└── requirements.txt
```

Where `chalicelib.blueprint.py` is:
```
from chalice import Blueprint, AuthResponse

bp = Blueprint(__name__)

@bp.authorizer() # pretty much same as the documentation
def my_auth(auth_request):
token = auth_request.token
if token == 'allow':
return AuthResponse(routes=['*'], principal_id='user')
else:
return AuthResponse(routes=[], principal_id='user')

@bp.route('/foo', authorizer=my_auth)
def route():
return {"hello": "world"}
```

and `app.py`:
```
from chalice import Chalice
from chalicelib.blueprint import bp

app = Chalice(app_name='testBug')
app.register_blueprint(bp)
```

3) Test everything works in local - Works!
4) Deploy (`chalice deploy`)
5) Make request to APIGateway (with authorization token) - Works! I get status code 200 and `{"hello": "world"}` in the response. _Cool_, now let's work on some changes!
6) Rename the module `chalicelib.blueprint.py` to eg `chalicelib.changed_name.py`
7) Change the import statement in `app.py` from `from chalicelib.blueprint import bp` to `from chalicelib.changed_name import bp`
8) Test everything works in local - Works!
9) Redeploy (`chalice deploy`)
10) Make request to APIGateway (with authorization token) - `Server error 500` -
```
{
"message": null
}
```

When looking at CloudWatch logs for authorizer lambda `/aws/lambda/testBug-dev_my_auth` the latest deploy threw this log:
`[ERROR] Runtime.ImportModuleError: Unable to import module 'chalicelib.blueprint': No module named 'blueprint'`

So it seems like the authorizer code haven't been updated properly on api gateway level somehow!
I've checked up the through console the code for the authorizer and it is with the changed import.
I've waited up to see if the authorizer cache ttl have something to do with this and I still got this error.

Contributor guide

Open the contributing guide

Research direction

Reproduce the deployment using app.py and the chalicelib.blueprint.py module, then rename it as described and run chalice deploy. Start with the generated authorizer Lambda and the CloudWatch ImportModuleError for chalicelib.blueprint; done means API Gateway invokes the renamed authorizer successfully after redeployment without referencing the old module.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python
Domain
api, backend, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.