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 input from direct
invocation.
"""
2022-04-08 15:42:13 -07:00
# snippet-start:[python.example_code.lambda.handler.increment]
2020-07-09 17:27:30 -07:00
import logging
logger = logging . getLogger ( )
logger . setLevel ( logging . INFO )
def lambda_handler ( event , context ) :
"""
2022-04-08 15:42:13 -07:00
Accepts an action and a single number, performs the specified action on the number,
and returns the result. The only allowable action is ' increment ' .
2020-07-09 17:27:30 -07:00
: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.
2022-04-08 15:42:13 -07:00
:return: The result of the action.
2020-07-09 17:27:30 -07:00
"""
2022-04-08 15:42:13 -07:00
result = None
2023-10-18 10:35:05 -07:00
action = event . get ( " action " )
if action == " increment " :
result = event . get ( " number " , 0 ) + 1
logger . info ( " Calculated result of %s " , result )
2022-04-08 15:42:13 -07:00
else :
logger . error ( " %s is not a valid action. " , action )
2020-07-09 17:27:30 -07:00
2023-10-18 10:35:05 -07:00
response = { " result " : result }
2020-07-09 17:27:30 -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.increment]