chalice + cfn-flip + sam local
I tried to make a chalice app and deploy it locally with sam local, which worked but every API call i make return "method unsupported: " as if sam-local wasn't giving the lambda the http method.
prerequisites: chalice (pip) cfn-flip (pip) aws-sam-local (npm) docker
steps: $ chalice new-project hello-world && cd hello-world $ chalice package package && cd package $ cfn-flip sam.json > template.yml $ sam local start-api
everything works fine, but every calls get that 405. the template.yml looks valid to my novice eyes
Any help appreciated!
after going into the chalice code, it appear that it's expecting the event inside the requestContext field
http_method = event['requestContext']['httpMethod']
and the event received:
{'httpMethod': 'GET', 'body': '', 'resource': '/', 'requestContext': {'resourceId': '', 'apiId': '', 'resourcePath': '/', 'httpMethod': '', 'requestId': '', 'accountId': '', 'identity': {'apiKey': '', 'userArn': '', 'cognitoAuthenticationType': '', 'caller': '', 'userAgent': '', 'user': '', 'cognitoIdentityPoolId': '', 'cognitoIdentityId': '', 'cognitoAuthenticationProvider': '', 'sourceIp': '127.0.0.1:63897', 'accountId': ''}}, 'queryStringParameters': {}, 'headers': {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.8', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Dnt': '1', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36'}, 'pathParameters': {}, 'stageVariables': None,'path': '/'}
notice that in the requestContext, the httpMethod is ''
Thanks for digging through this. Looks like SAM Local isn't passing the method in request context. Would you be able to send a PR to fix this?
Just add one line here https://github.com/awslabs/aws-sam-local/blob/8edcd3c46fa586fc307358f882005cdf278f282e/event.go#L83
Would love to contribute! I should have a PR by tomorrow.
Hello again, @sanathkr
I'm kinda new to public PRs, I cloned, created a branch and commited but don't have permission to push. I sadly don't have any time right now to look into this.
I also didn't see tests for event.go, so I assumed this would be the only change needed.
Here's the diff:
lepthy@lepthy-apple:~/Personal/aws-sam-local$ git diff develop
diff --git a/event.go b/event.go
index 405604a..1b77e94 100644
--- a/event.go
+++ b/event.go
@@ -82,6 +82,7 @@ func NewEvent(req *http.Request) (*Event, error) {
event.RequestContext.Identity.SourceIP = req.RemoteAddr
event.RequestContext.ResourcePath = req.URL.Path
+ event.RequestContext.HTTPMethod = req.Method
return event, nil
Cheers
This is something that is fixed by #143 : https://github.com/awslabs/aws-sam-local/pull/143/files#diff-9fb85d1ee9f1b0a59804bec41e1491feR85
I can see #143 was merged, but came across a similar issue today. Did this ever get resolved successfully?
chalice 1.1.0 and aws-sam-local build from develop today.
chalice new-project hello-world && cd hello-world
chalice package package && cd package
sam local start-api -t sam.json
curl -v http://localhost:3000/
2018/02/09 10:14:20 Invoking app.app (python3.6)
2018/02/09 10:14:20 Decompressing /home/kadrach/source/hello/out/deployment.zip
START RequestId: 66a9cddc-6053-4436-bce0-36c64e5c9a33 Version: $LATEST
'queryStringParameters': KeyError
Traceback (most recent call last):
File "/var/task/chalice/app.py", line 598, in __call__
self.current_request = Request(event['queryStringParameters'],
KeyError: 'queryStringParameters'
END RequestId: 66a9cddc-6053-4436-bce0-36c64e5c9a33
REPORT RequestId: 66a9cddc-6053-4436-bce0-36c64e5c9a33 Duration: 10 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 20 MB
2018/02/09 10:14:21 Function returned an invalid response (must include one of: body, headers or statusCode in the response object): %!s(<nil>)
Unchanged if using --prefix-routing option introduced in #143.
See also #220.
Tricked myself by having the wrong version of aws-sam-local on path. This issue is resolved. Apologies :)
Can you try the latest release - https://github.com/awslabs/aws-sam-cli/releases/tag/v0.3.0? I believe this is fixed. Feel free to re-open Issue if not.
Hi,
This is working but note currently sam-cli uses (only) Python2 and chalice uses (only) Python3.
Do not forget to cd package && pyenv local 2.7.xbefore sam local start-api -t sam.json
Cheers, Francisco