Skip to content

chalice + cfn-flip + sam local #141

Closed

Description

@ehault

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!

Activity

  1. ehault commented on Sep 26, 2017

    @ehault
    Author

    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 ''

  2. sanathkr commented on Sep 26, 2017

    @sanathkr

    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

  3. ehault commented on Sep 26, 2017

    @ehault
    Author

    Would love to contribute! I should have a PR by tomorrow.

  4. ehault commented on Sep 27, 2017

    @ehault
    Author

    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

  5. added
    contributors/welcome
    and removed on Jan 7, 2018
  6. kadrach commented on Feb 8, 2018

    @kadrach
    Member

    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.

  7. kadrach commented on Feb 9, 2018

    @kadrach
    Member

    See also #220.

  8. kadrach commented on Feb 9, 2018

    @kadrach
    Member

    Tricked myself by having the wrong version of aws-sam-local on path. This issue is resolved. Apologies :)

  9. sanathkr commented on May 8, 2018

    @sanathkr

    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.

  10. include commented on May 10, 2018

    @include

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributors/welcometype/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions