Skip to content

docs(api_gateway): new event handler for API Gateway and ALB #418

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,27 @@ def __init__(self, proxy_type: Enum = ProxyEventType.APIGatewayProxyEvent, cors:
self._cors = cors
self._cors_methods: Set[str] = {"OPTIONS"}

def get(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def get(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Get route decorator with GET `method`"""
return self.route(rule, "GET", cors, compress, cache_control)

def post(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def post(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Post route decorator with POST `method`"""
return self.route(rule, "POST", cors, compress, cache_control)

def put(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def put(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Put route decorator with PUT `method`"""
return self.route(rule, "PUT", cors, compress, cache_control)

def delete(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def delete(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Delete route decorator with DELETE `method`"""
return self.route(rule, "DELETE", cors, compress, cache_control)

def patch(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def patch(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Patch route decorator with PATCH `method`"""
return self.route(rule, "PATCH", cors, compress, cache_control)

def route(self, rule: str, method: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def route(self, rule: str, method: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Route decorator includes parameter `method`"""

def register_resolver(func: Callable):
Expand Down
66 changes: 65 additions & 1 deletion docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ This is the sample infrastructure we are using for the initial examples in this
AllowOrigin: "'https://example.com'"
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
MaxAge: "'300'"
BinaryMediaTypes: # see Binary responses
BinaryMediaTypes: # see Binary responses section
- '*~1*' # converts to */* for any binary type
Function:
Timeout: 5
Expand Down Expand Up @@ -186,6 +186,19 @@ Here's an example where we have two separate functions to resolve two paths: `/h
}
```

=== "response.json"

```json
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"message\":\"hello universe\"}",
"isBase64Encoded": false
}
```

#### HTTP API

When using API Gateway HTTP API to front your Lambda functions, you can instruct `ApiGatewayResolver` to conform with their contract via `proxy_type` param:
Expand Down Expand Up @@ -267,6 +280,17 @@ You can use `/path/{dynamic_value}` when configuring dynamic URL paths. This all
return app.resolve(event, context)
```

=== "sample_request.json"

```json
{
"resource": "/hello/{name}",
"path": "/hello/lessa",
"httpMethod": "GET",
...
}
```

You can also nest paths as configured earlier in [our sample infrastructure](#required-resources): `/{message}/{name}`.

=== "app.py"
Expand All @@ -292,6 +316,17 @@ You can also nest paths as configured earlier in [our sample infrastructure](#re
return app.resolve(event, context)
```

=== "sample_request.json"

```json
{
"resource": "/{message}/{name}",
"path": "/hi/michael",
"httpMethod": "GET",
...
}
```

### Accessing request details

By integrating with [Data classes utilities](../../utilities/data_classes.md){target="_blank"}, you have access to request details, Lambda context and also some convenient methods.
Expand Down Expand Up @@ -384,6 +419,35 @@ This will ensure that CORS headers are always returned as part of the response w
return app.resolve(event, context)
```

=== "response.json"

```json
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "https://www.example.com",
"Access-Control-Allow-Headers": "Authorization,Content-Type,X-Amz-Date,X-Amz-Security-Token,X-Api-Key"
},
"body": "{\"message\":\"hello lessa\"}",
"isBase64Encoded": false
}
```

=== "response_no_cors.json"

```json
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"message\":\"hello lessa\"}",
"isBase64Encoded": false
}
```


!!! tip "Optionally disable class on a per path basis with `cors=False` parameter"

#### Pre-flight
Expand Down