-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.py
74 lines (60 loc) · 2.44 KB
/
lambda_function.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to implement an AWS Lambda function that handles calls from an Amazon API
Gateway REST API.
"""
import json
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
"""
Handles requests that are passed through an Amazon API Gateway REST API.
GET, POST, and PUT requests all result in success codes that echo back input
parameters in a message. DELETE requests result in a 405 response.
Several kinds of REST API parameters are demonstrated:
* Query string: 'name' can be sent in the query string,
for example: demoapi?name=Martha
* Custom header: 'day' can be sent as a custom header, for example: 'day: Thursday'
* Body: 'adjective' can be sent in the request body, encoded as JSON,
for example: {"adjective": "fantastic"}
:param event: The event dict sent by Amazon API Gateway that contains all of the
request data.
:param context: The context in which the function is called.
:return: A response that is sent to Amazon API Gateway, to be wrapped into
an HTTP response. The 'statusCode' field is the HTTP status code
and the 'body' field is the body of the response.
"""
logger.info("Request: %s", event)
response_code = 200
http_method = event['requestContext']['http']['method']
query_string = event.get('queryStringParameters')
headers = event.get('headers')
body = event.get('body')
name = 'D. E. Fault'
if query_string is not None:
name = query_string.get('name', name)
day = 'None day'
if headers is not None:
day = headers.get('day', day)
adjective = 'nice'
if body is not None:
adjective = json.loads(body).get('adjective', adjective)
if http_method == 'GET':
greeting = f"Got your GET, {name}."
elif http_method == 'POST':
greeting = f"Nice POST, {name}."
elif http_method == 'PUT':
greeting = f"I'll just PUT this here for you, {name}."
else:
greeting = f"Sorry, {name}, {http_method} isn't allowed."
response_code = 405
greeting += f" Have a {adjective} {day}!"
response = {
'statusCode': response_code,
'body': json.dumps({'message': greeting, 'input': event})
}
logger.info("Response: %s", response)
return response