2020-07-09 17:27:30 -07:00
# 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
2023-10-18 10:35:05 -07:00
http_method = event . get ( " httpMethod " )
query_string = event . get ( " queryStringParameters " )
headers = event . get ( " headers " )
body = event . get ( " body " )
2020-07-09 17:27:30 -07:00
2023-10-18 10:35:05 -07:00
name = " D. E. Fault "
2020-07-09 17:27:30 -07:00
if query_string is not None :
2023-10-18 10:35:05 -07:00
name = query_string . get ( " name " , name )
day = " None day "
2020-07-09 17:27:30 -07:00
if headers is not None :
2023-10-18 10:35:05 -07:00
day = headers . get ( " day " , day )
adjective = " nice "
2020-07-09 17:27:30 -07:00
if body is not None :
2023-10-18 10:35:05 -07:00
adjective = json . loads ( body ) . get ( " adjective " , adjective )
2020-07-09 17:27:30 -07:00
2023-10-18 10:35:05 -07:00
if http_method == " GET " :
2020-07-09 17:27:30 -07:00
greeting = f " Got your GET, { name } . "
2023-10-18 10:35:05 -07:00
elif http_method == " POST " :
2020-07-09 17:27:30 -07:00
greeting = f " Nice POST, { name } . "
2023-10-18 10:35:05 -07:00
elif http_method == " PUT " :
2020-07-09 17:27:30 -07:00
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 = {
2023-10-18 10:35:05 -07:00
" statusCode " : response_code ,
" body " : json . dumps ( { " message " : greeting , " input " : event } ) ,
2020-07-09 17:27:30 -07:00
}
logger . info ( " Response: %s " , response )
return response