-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathalb_handler.py
35 lines (27 loc) · 1004 Bytes
/
alb_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from aws_lambda_powertools.event_handler import (
ALBResolver,
CORSConfig,
Response,
content_types,
)
cors_config = CORSConfig(allow_origin="https://www.example.org", extra_origins=["https://dev.example.org"])
app = ALBResolver(cors=cors_config)
# The reason we use post is that whoever is writing tests can easily assert on the
# content being sent (body, headers, cookies, content-type) to reduce cognitive load.
@app.post("/todos")
def todos():
payload = app.current_event.json_body
body = payload.get("body", "Hello World")
status_code = payload.get("status_code", 200)
headers = payload.get("headers", {})
cookies = payload.get("cookies", [])
content_type = headers.get("Content-Type", content_types.TEXT_PLAIN)
return Response(
status_code=status_code,
content_type=content_type,
body=body,
cookies=cookies,
headers=headers,
)
def lambda_handler(event, context):
return app.resolve(event, context)