-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathlambda_handler_basic.py
More file actions
44 lines (34 loc) · 1.23 KB
/
lambda_handler_basic.py
File metadata and controls
44 lines (34 loc) · 1.23 KB
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
# 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.calculate]
import logging
import math
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Define a list of Python lambda functions that are called by this AWS Lambda function.
ACTIONS = {
'square': lambda x: x * x,
'square root': lambda x: math.sqrt(x),
'increment': lambda x: x + 1,
'decrement': lambda x: x - 1,
}
def lambda_handler(event, context):
"""
Accepts an action and a number, performs the specified action on the number,
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.
"""
logger.info('Event: %s', event)
result = ACTIONS[event['action']](event['number'])
logger.info('Calculated result of %s', result)
response = {'result': result}
return response
# snippet-end:[python.example_code.lambda.handler.calculate]