MCPcopy Create free account
hub / github.com/awsdocs/aws-doc-sdk-examples / LambdaWrapper

Class LambdaWrapper

python/example_code/lambda/lambda_basics.py:22–302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20# snippet-start:[python.example_code.python.LambdaWrapper.full]
21# snippet-start:[python.example_code.python.LambdaWrapper.decl]
22class LambdaWrapper:
23 def __init__(self, lambda_client, iam_resource):
24 self.lambda_client = lambda_client
25 self.iam_resource = iam_resource
26
27 # snippet-end:[python.example_code.python.LambdaWrapper.decl]
28
29 @staticmethod
30 def create_deployment_package(source_file, destination_file):
31 """
32 Creates a Lambda deployment package in .zip format in an in-memory buffer. This
33 buffer can be passed directly to Lambda when creating the function.
34
35 :param source_file: The name of the file that contains the Lambda handler
36 function.
37 :param destination_file: The name to give the file when it's deployed to Lambda.
38 :return: The deployment package.
39 """
40 buffer = io.BytesIO()
41 with zipfile.ZipFile(buffer, "w") as zipped:
42 zipped.write(source_file, destination_file)
43 buffer.seek(0)
44 return buffer.read()
45
46 def get_iam_role(self, iam_role_name):
47 """
48 Get an AWS Identity and Access Management (IAM) role.
49
50 :param iam_role_name: The name of the role to retrieve.
51 :return: The IAM role.
52 """
53 role = None
54 try:
55 temp_role = self.iam_resource.Role(iam_role_name)
56 temp_role.load()
57 role = temp_role
58 logger.info("Got IAM role %s", role.name)
59 except ClientError as err:
60 if err.response["Error"]["Code"] == "NoSuchEntity":
61 logger.info("IAM role %s does not exist.", iam_role_name)
62 else:
63 logger.error(
64 "Couldn't get IAM role %s. Here's why: %s: %s",
65 iam_role_name,
66 err.response["Error"]["Code"],
67 err.response["Error"]["Message"],
68 )
69 raise
70 return role
71
72 def create_iam_role_for_lambda(self, iam_role_name):
73 """
74 Creates an IAM role that grants the Lambda function basic permissions. If a
75 role with the specified name already exists, it is used for the demo.
76
77 :param iam_role_name: The name of the role to create.
78 :return: The role and a value that indicates whether the role is newly created.
79 """

Callers 12

usage_demoFunction · 0.90
usage_demoFunction · 0.90
run_scenarioFunction · 0.90
test_create_functionFunction · 0.90
test_get_functionFunction · 0.90
test_delete_functionFunction · 0.90
test_invoke_functionFunction · 0.90
test_list_functionsFunction · 0.90

Calls

no outgoing calls

Tested by 9

test_create_functionFunction · 0.72
test_get_functionFunction · 0.72
test_delete_functionFunction · 0.72
test_invoke_functionFunction · 0.72
test_list_functionsFunction · 0.72