adamchainz/apig-wsgi

API Gateway call results in [ERROR] KeyError: 'path'

Closed this issue · 3 comments

Python Version

3.8

Package Version

2.12.1

Description

I'm trying to call this really simple app with API Gateway (REST) Lambda integration:

#!/usr/bin/python3
import urllib3
from flask import Flask
from apig_wsgi import make_lambda_handler

app = Flask(__name__)
lambda_handler = make_lambda_handler(app)

@app.route('/eventprint', methods=['GET'])
def print_event():
    return {"status": 200, "message": "OK"}

if __name__ == "__main__":
    app.run(debug=True)

but I got the following error:

[ERROR] KeyError: 'path'
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/site-packages/apig_wsgi.py", line 45, in handler
    environ = get_environ_v1(
  File "/var/lang/lib/python3.8/site-packages/apig_wsgi.py", line 73, in get_environ_v1
    "PATH_INFO": urllib.parse.unquote(event["path"], encoding="iso-8859-1"),

The application works with ALB.
Has anybody run into this issue before? What am I doing wrong?

Can you compare your deployment setup with the example app?

# ALB
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Condition: ShouldDeployAlb
Properties:
Scheme: internet-facing
Subnets:
- !Ref SubnetId1
- !Ref SubnetId2
SecurityGroups: [!Ref SecurityGroup]
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Condition: ShouldDeployAlb
DependsOn: LambdaPermissionALB
Properties:
TargetType: lambda
Targets:
- Id: !GetAtt LambdaFunction.Arn
TargetGroupAttributes:
- Key: lambda.multi_value_headers.enabled
Value: True
HttpListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Condition: ShouldDeployAlb
Properties:
LoadBalancerArn: !Ref LoadBalancer
Port: 80
Protocol: HTTP
DefaultActions:
- TargetGroupArn: !Ref TargetGroup
Type: forward
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Condition: ShouldDeployAlb
Properties:
GroupDescription: Allow http on port 80
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0

For whatever reason your ALB seems not to be sending path. Perhaps there's a change in config that causes that.

Hi Adam,

Thank you for the quick response. The app works with ALB.
The problem happens when I call it from API Gateway.

OK, I looked into the API Gateway integration example.
I made it work with +proxy resource. Thanks again!