-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_service.py
95 lines (81 loc) · 3.38 KB
/
lambda_service.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import boto3
import json
AWS_REGION = "us-east-1" # Change this as needed
def create_iam_role_for_lambda(iam_client):
"""
Creates an IAM role for Lambda if no suitable roles are found.
"""
print("\nNo suitable IAM roles found. Creating a new IAM role for Lambda...")
role_name = "LambdaExecutionRole"
assume_role_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}
try:
response = iam_client.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(assume_role_policy_document),
Description="Role for Lambda execution"
)
print(f"IAM Role Created: {response['Role']['Arn']}")
return response['Role']['Arn']
except iam_client.exceptions.EntityAlreadyExistsException:
print("IAM Role already exists.")
role = iam_client.get_role(RoleName=role_name)
return role['Role']['Arn']
except Exception as e:
raise Exception(f"Failed to create IAM role: {e}")
def create_lambda_function():
try:
print("\n=== Configure Lambda Function ===")
# Get the Lambda client
lambda_client = boto3.client('lambda', region_name=AWS_REGION)
# Get the IAM client to fetch roles
iam_client = boto3.client('iam', region_name=AWS_REGION)
# Fetch available IAM roles
roles_response = iam_client.list_roles()
roles = [
role['Arn'] for role in roles_response['Roles']
if 'lambda.amazonaws.com' in role['AssumeRolePolicyDocument']['Statement'][0]['Principal'].get('Service', [])
]
if not roles:
# Create a new role if none exist
selected_role_arn = create_iam_role_for_lambda(iam_client)
else:
# Display IAM roles for user selection
print("\nAvailable IAM Roles:")
for idx, role_arn in enumerate(roles, start=1):
print(f"{idx}. {role_arn}")
# Prompt user to select a role
role_choice = int(input(f"\nEnter the number of the IAM role to use (1-{len(roles)}): "))
selected_role_arn = roles[role_choice - 1]
# Prompt for other function details
function_name = input("Enter Lambda Function Name: ")
zip_file_path = input("Enter Path to ZIP File: ")
# Read the ZIP file
with open(zip_file_path, 'rb') as f:
code = f.read()
# Create the Lambda function
print("\nCreating Lambda function. Please wait...")
response = lambda_client.create_function(
FunctionName=function_name,
Runtime='python3.9',
Role=selected_role_arn,
Handler='lambda_function.lambda_handler',
Code={'ZipFile': code}
)
return f"Lambda Function Created Successfully: {response['FunctionName']}"
except FileNotFoundError:
return "Error: ZIP file not found. Please check the file path."
except KeyError as e:
return f"Error: Missing key {str(e)} in the IAM role response."
except ValueError:
return "Invalid input. Please enter a valid number for role selection."
except Exception as e:
return f"An unexpected error occurred: {e}"