-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event_source): allow multiple CORS origins (#2279)
Co-authored-by: Leandro Damascena <leandro.damascena@gmail.com>
- Loading branch information
1 parent
27d197c
commit 042e83a
Showing
13 changed files
with
414 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/event_handler_rest/src/setting_cors_extra_origins.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import requests | ||
from requests import Response | ||
|
||
from aws_lambda_powertools import Logger, Tracer | ||
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, CORSConfig | ||
from aws_lambda_powertools.logging import correlation_paths | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
tracer = Tracer() | ||
logger = Logger() | ||
# CORS will match when Origin is https://www.example.com OR https://dev.example.com | ||
cors_config = CORSConfig(allow_origin="https://www.example.com", extra_origins=["https://dev.example.com"], max_age=300) | ||
app = APIGatewayRestResolver(cors=cors_config) | ||
|
||
|
||
@app.get("/todos") | ||
@tracer.capture_method | ||
def get_todos(): | ||
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos") | ||
todos.raise_for_status() | ||
|
||
# for brevity, we'll limit to the first 10 only | ||
return {"todos": todos.json()[:10]} | ||
|
||
|
||
@app.get("/todos/<todo_id>") | ||
@tracer.capture_method | ||
def get_todo_by_id(todo_id: str): # value come as str | ||
todos: Response = requests.get(f"https://jsonplaceholder.typicode.com/todos/{todo_id}") | ||
todos.raise_for_status() | ||
|
||
return {"todos": todos.json()} | ||
|
||
|
||
@app.get("/healthcheck", cors=False) # optionally removes CORS for a given route | ||
@tracer.capture_method | ||
def am_i_alive(): | ||
return {"am_i_alive": "yes"} | ||
|
||
|
||
# You can continue to use other utilities just as before | ||
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST) | ||
@tracer.capture_lambda_handler | ||
def lambda_handler(event: dict, context: LambdaContext) -> dict: | ||
return app.resolve(event, context) |
10 changes: 10 additions & 0 deletions
10
examples/event_handler_rest/src/setting_cors_extra_origins_output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"statusCode": 200, | ||
"multiValueHeaders": { | ||
"Content-Type": ["application/json"], | ||
"Access-Control-Allow-Origin": ["https://www.example.com","https://dev.example.com"], | ||
"Access-Control-Allow-Headers": ["Authorization,Content-Type,X-Amz-Date,X-Amz-Security-Token,X-Api-Key"] | ||
}, | ||
"body": "{\"todos\":[{\"userId\":1,\"id\":1,\"title\":\"delectus aut autem\",\"completed\":false},{\"userId\":1,\"id\":2,\"title\":\"quis ut nam facilis et officia qui\",\"completed\":false},{\"userId\":1,\"id\":3,\"title\":\"fugiat veniam minus\",\"completed\":false},{\"userId\":1,\"id\":4,\"title\":\"et porro tempora\",\"completed\":true},{\"userId\":1,\"id\":5,\"title\":\"laboriosam mollitia et enim quasi adipisci quia provident illum\",\"completed\":false},{\"userId\":1,\"id\":6,\"title\":\"qui ullam ratione quibusdam voluptatem quia omnis\",\"completed\":false},{\"userId\":1,\"id\":7,\"title\":\"illo expedita consequatur quia in\",\"completed\":false},{\"userId\":1,\"id\":8,\"title\":\"quo adipisci enim quam ut ab\",\"completed\":true},{\"userId\":1,\"id\":9,\"title\":\"molestiae perspiciatis ipsa\",\"completed\":false},{\"userId\":1,\"id\":10,\"title\":\"illo est ratione doloremque quia maiores aut\",\"completed\":true}]}", | ||
"isBase64Encoded": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
tests/e2e/event_handler/handlers/lambda_function_url_handler.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.