Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/lambda/files/mini_lambda.py

raw
1 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2
3 import json
4 import os
5
6
7 def handler(event, context):
8 """
9 The handler function is the function which gets called each time
10 the lambda is run.
11 """
12 # printing goes to the cloudwatch log allowing us to simply debug the lambda if we can find
13 # the log entry.
14 print("got event:\n" + json.dumps(event))
15
16 # if the name parameter isn't present this can throw an exception
17 # which will result in an amazon chosen failure from the lambda
18 # which can be completely fine.
19
20 name = event["name"]
21
22 # we can use environment variables as part of the configuration of the lambda
23 # which can change the behaviour of the lambda without needing a new upload
24
25 extra = os.environ.get("EXTRA_MESSAGE")
26 if extra is not None and len(extra) > 0:
27 greeting = f"hello {name}. {extra}"
28 else:
29 greeting = "hello " + name
30
31 return {"message": greeting}
32
33
34 def main():
35 """
36 This main function will normally never be called during normal
37 lambda use. It is here for testing the lambda program only.
38 """
39 event = {"name": "james"}
40 context = None
41 print(handler(event, context))
42
43
44 if __name__ == "__main__":
45 main()