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

Support format version 2 for HTTP API's #124

Closed
adamchainz opened this issue Mar 13, 2020 · 6 comments · Fixed by #181
Closed

Support format version 2 for HTTP API's #124

adamchainz opened this issue Mar 13, 2020 · 6 comments · Fixed by #181

Comments

@adamchainz
Copy link
Owner

https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

Extended the example to deploy an HTTP API and dump its event, got one like this, plus error from trying to parse format version 1:

START RequestId: 863fef15-563d-498f-8d32-a4a5737f014f Version: $LATEST                                                                                                                                                                                                                                                                                                                                
{                                                                                                                                                                                                                                                                                                                                                                                                     
  "cookies": [                                                                                                                                                                                                                                                                                                                                                                                        
    "pN=7",                                                                                                                                                                                                                                                                                                                                                                                           
  ],                                                                                                                                                                                                                                                                                                                                                                                                  
  "headers": {                                                                                                                                                                                                                                                                                                                                                                                        
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",                                                                                                                                                                                                                                                                                                           
    "accept-encoding": "gzip, deflate, br",                                                                                                                                                                                                                                                                                                                                                           
    "accept-language": "en-GB,en;q=0.5",                                                                                                                                                                                                                                                                                                                                                              
    "content-length": "0",                                                                                                                                                                                                                                                                                                                                                                            
    "dnt": "1",                                                                                                                                                                                                                                                                                                                                                                                       
    "host": "s219lqqg4f.execute-api.eu-central-1.amazonaws.com",                                                                                                                                                                                                                                                                                                                                      
    "upgrade-insecure-requests": "1",                                                                                                                                                                                                                                                                                                                                                                 
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:73.0) Gecko/20100101 Firefox/73.0",                                                                                                                                                                                                                                                                                               
    "x-amzn-trace-id": "Root=1-5e6b67ad-2d4a5560cf6fb7a02780c3a0",                                                                                                                                                                                                                                                                                                                                    
    "x-forwarded-for": "91.109.238.159",                                                                                                                                                                                                                                                                                                                                                              
    "x-forwarded-port": "443",                                                                                                                                                                                                                                                                                                                                                                        
    "x-forwarded-proto": "https"                                                                                                                                                                                                                                                                                                                                                                      
  },                                                                                                                                                                                                                                                                                                                                                                                                  
  "isBase64Encoded": true,                                                                                                                                                                                                                                                                                                                                                                            
  "rawPath": "/",                                                                                                                                                                                                                                                                                                                                                                                     
  "rawQueryString": "",                                                                                                                                                                                                                                                                                                                                                                               
  "requestContext": {                                                                                                                                                                                                                                                                                                                                                                                 
    "accountId": "931198139388",                                                                                                                                                                                                                                                                                                                                                                      
    "apiId": "s219lqqg4f",                                                                                                                                                                                                                                                                                                                                                                            
    "domainName": "s219lqqg4f.execute-api.eu-central-1.amazonaws.com",                                                                                                                                                                                                                                                                                                                                
    "domainPrefix": "s219lqqg4f",                                                                                                                                                                                                                                                                                                                                                                     
    "http": {                                                                                                                                                                                                                                                                                                                                                                                         
      "method": "GET",                                                                                                                                                                                                                                                                                                                                                                                
      "path": "/",                                                                                                                                                                                                                                                                                                                                                                                    
      "protocol": "HTTP/1.1",                                                                                                                                                                                                                                                                                                                                                                         
      "sourceIp": "91.109.238.159",                                                                                                                                                                                                                                                                                                                                                                   
      "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:73.0) Gecko/20100101 Firefox/73.0"                                                                                                                                                                                                                                                                                               
    },                                                                                                                                                                                                                                                                                                                                                                                                
    "requestId": "JU0jFiJxliAEJHg=",                                                                                                                                                                                                                                                                                                                                                                  
    "routeId": null,                                                                                                                                                                                                                                                                                                                                                                                  
    "routeKey": "$default",                                                                                                                                                                                                                                                                                                                                                                           
    "stage": "$default",                                                                                                                                                                                                                                                                                                                                                                              
    "time": "13/Mar/2020:10:59:57 +0000",                                                                                                                                                                                                                                                                                                                                                             
    "timeEpoch": 1584097197293                                                                                                                                                                                                                                                                                                                                                                        
  },                                                                                                                                                                                                                                                                                                                                                                                                  
  "routeKey": "$default",                                                                                                                                                                                                                                                                                                                                                                             
  "version": "2.0"                                                                                                                                                                                                                                                                                                                                                                                    
}                                                                                                                                                                                                                                                                                                                                                                                                     
[ERROR] KeyError: 'httpMethod' Traceback (most recent call last):   File "/var/task/testapp/wsgi.py", line 16, in lambda_handler     return apig_wsgi_handler(event, context)   File "/var/task/apig_wsgi.py", line 37, in handler     environ = get_environ(event, context, binary_support=binary_support)   File "/var/task/apig_wsgi.py", line 50, in get_environ     method = event["httpMethod"] 
@jmfederico
Copy link

Manually changing the payload to version 1 fixes the problem, do not know how to set that up with Ansible.

@adamchainz
Copy link
Owner Author

@jmfederico do you mean you swap "version" in the incoming event? That seems dangerous as there have been changes, and apgi-wsgi thus won't parse all the request data.

@jmfederico
Copy link

@adamchainz I mean changing the version that is sent to the Lambda function from the HttpAPI.

It by default now uses version 2. One has to manually set the API to send the event usi no version 1.

I do not use Ansible so I am not sure with the setup that you have which version it sends by default, but my guess is it sends version 2.

It should be possible to use Ansible to ser the version to 1.

@adamchainz
Copy link
Owner Author

Right, that can be changed in CloudFormation - docs . We don't have a AWS::ApiGatewayV2::Integration in the cfn template right now, so one would need adding to do that.

But I don't care about fixing the example app.

@irgeek
Copy link
Contributor

irgeek commented Jun 23, 2020

You don't need to add a AWS::ApiGatewayV2::Integration resource -- there's a way to overriding certain configuration options for the managed HTTP API. If you add this to the template, it should switch the payload format.

  HttpApiOverrides:
    Type: AWS::ApiGatewayV2::ApiGatewayManagedOverrides
    Properties:
      ApiId: !Sub ${HttpApi}
      Integration:
        PayloadFormatVersion: 1.0

mneil pushed a commit to mneil/apig-wsgi that referenced this issue Sep 29, 2020
Check the event version before building the environment. Use
the unpack operator to copy the original event to the
v1 event object. Map missing keys needed by the environment
builder from the v2 object.

Values like request_context are passed unchanged. The multivalue
headers and query string parameters are mapped back into strings
then split back up into their original formats. It's inefficient
and could be improved but ultimately leaves with unchanged logic
for parsing the an identical end result.
@mneil
Copy link

mneil commented Sep 29, 2020

Thanks for writing this library. I've been using it on some lambdas and recently needed to switch to v2 to use the JWT authorizer. I think I can switch back to the v1 version, as suggested, but I'd really prefer to write less configuration if possible :). I think I added this functionality in #174 . I'm using this myself and would love to roll it back into your project so I can go back to including this from pypi. Let me know if you have any feedback or changes. Thanks again!

adamchainz added a commit that referenced this issue Oct 11, 2020
In prep for #124.
adamchainz added a commit that referenced this issue Oct 11, 2020
adamchainz added a commit that referenced this issue Oct 11, 2020
Use the latest versions of Python and dependencies, and use the v2 format for the HTTP API, for #124.
adamchainz added a commit that referenced this issue Oct 11, 2020
Use the latest versions of Python and dependencies, and use the v2 format for the HTTP API, for #124.
adamchainz pushed a commit that referenced this issue Oct 12, 2020
Check the event version before building the environment. Use
the unpack operator to copy the original event to the
v1 event object. Map missing keys needed by the environment
builder from the v2 object.

Values like request_context are passed unchanged. The multivalue
headers and query string parameters are mapped back into strings
then split back up into their original formats. It's inefficient
and could be improved but ultimately leaves with unchanged logic
for parsing the an identical end result.
adamchainz added a commit that referenced this issue Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants