2022-04-08 15:42:13 -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 input from direct
invocation.
"""
# snippet-start:[python.example_code.lambda.handler.arithmetic]
import logging
import os
logger = logging . getLogger ( )
# Define a list of Python lambda functions that are called by this AWS Lambda function.
ACTIONS = {
2023-10-18 10:35:05 -07:00
" plus " : lambda x , y : x + y ,
" minus " : lambda x , y : x - y ,
" times " : lambda x , y : x * y ,
" divided-by " : lambda x , y : x / y ,
}
2022-04-08 15:42:13 -07:00
def lambda_handler ( event , context ) :
"""
Accepts an action and two numbers, performs the specified action on the numbers,
and returns the result.
:param event: The event dict that contains the parameters sent when the function
is invoked.
:param context: The context in which the function is called.
:return: The result of the specified action.
"""
# Set the log level based on a variable configured in the Lambda environment.
2023-10-18 10:35:05 -07:00
logger . setLevel ( os . environ . get ( " LOG_LEVEL " , logging . INFO ) )
logger . debug ( " Event: %s " , event )
2022-04-08 15:42:13 -07:00
2023-10-18 10:35:05 -07:00
action = event . get ( " action " )
2022-04-08 15:42:13 -07:00
func = ACTIONS . get ( action )
2023-10-18 10:35:05 -07:00
x = event . get ( " x " )
y = event . get ( " y " )
2022-04-08 15:42:13 -07:00
result = None
try :
if func is not None and x is not None and y is not None :
result = func ( x , y )
logger . info ( " %s %s %s is %s " , x , action , y , result )
else :
logger . error ( " I can ' t calculate %s %s %s . " , x , action , y )
except ZeroDivisionError :
logger . warning ( " I can ' t divide %s by 0! " , x )
2023-10-18 10:35:05 -07:00
response = { " result " : result }
2022-04-08 15:42:13 -07:00
return response
2023-10-18 10:35:05 -07:00
2022-04-08 15:42:13 -07:00
# snippet-end:[python.example_code.lambda.handler.arithmetic]