'NoneType' object has no attribute 'context' when executing chalice deploy
While executing:
chalice deploy --no-autogen-policy --stage beta
I get this error: 'NoneType' object has no attribute 'context'
I have narrowed it down to a single line of code in my application.
def param_value(name):
stage = app.current_request.context['stage']
return stage_param_value(stage, name)
If I comment out the line that retrieves the stage from the current_request and replace it with
stage = 'beta'
then chalice deploy executes successfully.
Sorry, I should have mentioned earlier, I am using chalice 0.9. I have seen the same behavior in Python 2.7 and 3.6.
I believe what's happening is that the app.current_request.context['stage'] is pulled from the current inflight request, so if param_value() is called outside of a request (say as part of module initialization), then current_request will be set to None. A few actions I see:
- Look into a better error message when
app.current_requestis being accessed outside of a request - Expose the current stage we're deploying to via some other mechanism. We know at deploy-time what the current stage is. We could consider setting something like
app.config.stage. - Look into better initialization hooks. There's an issue about this: https://github.com/awslabs/chalice/issues/238, but because there's no official way of initializing a lambda function outside of module level execution, there's not a good way to separate app initialization code that you don't want run as part of a deploy (assuming that's the case here).
That makes sense for my problem. I like the second option, that would solve it nicely for me.