-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathlambda_helpers.py
More file actions
561 lines (465 loc) Β· 17.2 KB
/
lambda_helpers.py
File metadata and controls
561 lines (465 loc) Β· 17.2 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""
Lambda Deployment Helper Functions
Utilities for deploying and managing AWS Lambda functions
"""
import boto3
import json
import time
import subprocess
import zipfile
import os
from pathlib import Path
from typing import Dict, List, Optional, Any
def create_or_update_lambda_role(
iam_client,
role_name: str,
description: str = "Lambda execution role"
) -> str:
"""
Create or reuse IAM role for Lambda execution
Returns:
role_arn: ARN of the created/existing role
"""
assume_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": "sts:AssumeRole"
}
]
}
try:
role = iam_client.create_role(
RoleName=role_name,
AssumeRolePolicyDocument=json.dumps(assume_policy),
Description=description
)
print(f"β
Created IAM role: {role_name}")
role_arn = role["Role"]["Arn"]
# Attach necessary policies
iam_client.attach_role_policy(
RoleName=role_name,
PolicyArn="arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
)
iam_client.attach_role_policy(
RoleName=role_name,
PolicyArn="arn:aws:iam::aws:policy/AmazonS3FullAccess"
)
# Wait for role to propagate
time.sleep(10)
except iam_client.exceptions.EntityAlreadyExistsException:
role = iam_client.get_role(RoleName=role_name)
role_arn = role["Role"]["Arn"]
print(f"βΉοΈ Using existing role: {role_name}")
return role_arn
def create_deployment_package(
source_files: List[str],
requirements: List[str],
output_zip: str,
package_dir: str = "lambda_package"
) -> str:
"""
Build Lambda deployment package with dependencies
Args:
source_files: List of Python files to include
requirements: List of pip packages to install
output_zip: Name of output zip file
package_dir: Temporary directory for building package
Returns:
Path to created zip file
"""
print(f"π¦ Creating deployment package: {output_zip}")
# Clean and create package directory
subprocess.run(f"rm -rf {package_dir}", shell=True)
subprocess.run(f"mkdir -p {package_dir}", shell=True)
# Install requirements
if requirements:
print(f" Installing dependencies: {', '.join(requirements)}")
req_string = " ".join(requirements)
result = subprocess.run(
f"pip install --quiet {req_string} -t {package_dir}",
shell=True,
capture_output=True,
text=True
)
if result.returncode != 0:
print(f"β οΈ Warning: Some dependencies may have failed to install")
print(result.stderr)
# Copy source files
for source_file in source_files:
print(f" Adding source: {source_file}")
subprocess.run(f"cp {source_file} {package_dir}/", shell=True)
# Create zip
print(f" Creating zip archive...")
subprocess.run(
f"cd {package_dir} && zip -r ../{output_zip} . > /dev/null 2>&1",
shell=True
)
# Cleanup
subprocess.run(f"rm -rf {package_dir}", shell=True)
# Get zip size
zip_size = os.path.getsize(output_zip) / (1024 * 1024)
print(f"β
Package created: {output_zip} ({zip_size:.1f} MB)")
return output_zip
def deploy_lambda_function(
lambda_client,
function_name: str,
zip_file: str,
role_arn: str,
handler: str,
env_vars: Dict[str, str],
runtime: str = "python3.10",
timeout: int = 900,
memory_size: int = 3008,
architectures: List[str] = ["x86_64"]
) -> Dict:
"""
Deploy or update Lambda function
Returns:
Function configuration dict
"""
print(f"π Deploying Lambda function: {function_name}")
# Read zip file
with open(zip_file, "rb") as f:
zipped_code = f.read()
try:
# Try to create new function
response = lambda_client.create_function(
FunctionName=function_name,
Runtime=runtime,
Role=role_arn,
Handler=handler,
Code={"ZipFile": zipped_code},
Timeout=timeout,
MemorySize=memory_size,
Architectures=architectures,
Environment={"Variables": env_vars},
Publish=True
)
print(f"β
Lambda function created: {function_name}")
except lambda_client.exceptions.ResourceConflictException:
# Function exists, update it
print(f"βΉοΈ Function exists, updating...")
# Update code
lambda_client.update_function_code(
FunctionName=function_name,
ZipFile=zipped_code,
Publish=True
)
print(" Code updated, waiting for deployment...")
time.sleep(10)
# Update configuration
response = lambda_client.update_function_configuration(
FunctionName=function_name,
Environment={"Variables": env_vars},
Timeout=timeout,
MemorySize=memory_size,
Handler=handler,
Runtime=runtime
)
print(f"β
Lambda function updated: {function_name}")
return response
def setup_s3_trigger(
s3_client,
lambda_client,
bucket: str,
prefix: str,
function_name: str,
suffix: Optional[str] = None
) -> None:
"""
Configure S3 event trigger for Lambda
Args:
bucket: S3 bucket name
prefix: Folder prefix to trigger on
function_name: Lambda function to trigger
suffix: Optional file suffix filter (e.g., '.pdf')
"""
print(f"βοΈ Setting up S3 trigger: s3://{bucket}/{prefix} β {function_name}")
# Get Lambda function ARN
function_config = lambda_client.get_function(FunctionName=function_name)
function_arn = function_config["Configuration"]["FunctionArn"]
# Give S3 permission to invoke the Lambda
try:
lambda_client.add_permission(
FunctionName=function_name,
StatementId="s3invokepermission",
Action="lambda:InvokeFunction",
Principal="s3.amazonaws.com",
SourceArn=f"arn:aws:s3:::{bucket}"
)
print(f" β
Added invoke permission for S3")
except Exception as e:
print(f" βΉοΈ Permission may already exist: {e}")
# Configure filter rules
filter_rules = [{"Name": "prefix", "Value": prefix}]
if suffix:
filter_rules.append({"Name": "suffix", "Value": suffix})
# Attach the notification configuration to S3
# Note: This replaces ALL existing Lambda notifications - use carefully
s3_client.put_bucket_notification_configuration(
Bucket=bucket,
NotificationConfiguration={
"LambdaFunctionConfigurations": [
{
"LambdaFunctionArn": function_arn,
"Events": ["s3:ObjectCreated:*"],
"Filter": {"Key": {"FilterRules": filter_rules}}
}
]
}
)
print(f"β
S3 trigger set for s3://{bucket}/{prefix} β {function_name}")
def invoke_lambda_sync(
lambda_client,
function_name: str,
payload: Optional[Dict] = None,
show_logs: bool = True
) -> Dict:
"""
Invoke Lambda synchronously and wait for response
Args:
function_name: Name of Lambda function
payload: Optional JSON payload
show_logs: Whether to print logs
Returns:
Response from Lambda function
"""
print(f"β‘ Invoking Lambda: {function_name}")
start_time = time.time()
invoke_params = {
"FunctionName": function_name,
"InvocationType": "RequestResponse",
"LogType": "Tail" if show_logs else "None"
}
if payload:
invoke_params["Payload"] = json.dumps(payload)
response = lambda_client.invoke(**invoke_params)
# Parse response
status_code = response["StatusCode"]
if "Payload" in response:
result = json.loads(response["Payload"].read())
else:
result = {}
elapsed = time.time() - start_time
if status_code == 200:
print(f"β
Lambda completed successfully in {elapsed:.1f} seconds")
else:
print(f"β οΈ Lambda returned status code: {status_code}")
# Show logs if requested
if show_logs and "LogResult" in response:
import base64
log_data = base64.b64decode(response["LogResult"]).decode("utf-8")
print("\nπ Lambda Logs:")
print("-" * 60)
for line in log_data.split("\n")[-20:]: # Last 20 lines
if line.strip():
print(line)
print("-" * 60)
return result
def monitor_s3_folder(
s3_client,
bucket: str,
prefix: str,
expected_count: Optional[int] = None
) -> List[str]:
"""
Monitor S3 folder for files
Args:
bucket: S3 bucket name
prefix: Folder prefix to monitor
expected_count: Optional expected number of files
Returns:
List of file keys found
"""
print(f"π Monitoring s3://{bucket}/{prefix}")
response = s3_client.list_objects_v2(Bucket=bucket, Prefix=prefix)
files = []
if "Contents" in response:
for obj in response["Contents"]:
if not obj["Key"].endswith("/"):
files.append(obj["Key"])
print(f" Found {len(files)} files")
if expected_count and len(files) < expected_count:
print(f" β³ Waiting for {expected_count - len(files)} more files...")
return files
def upload_folder_to_s3(
s3_client,
local_folder: str,
s3_prefix: str,
bucket: str,
file_extensions: Optional[List[str]] = None,
skip_existing: bool = True
) -> int:
"""
Upload entire folder to S3
Args:
local_folder: Local folder path
s3_prefix: S3 prefix for uploads
bucket: S3 bucket name
file_extensions: Optional list of extensions to filter
skip_existing: Skip files that already exist in S3 (default True)
Returns:
Number of files uploaded
"""
print(f"π€ Uploading {local_folder} β s3://{bucket}/{s3_prefix}")
if skip_existing:
print(" (Skipping files that already exist in S3)")
uploaded = 0
skipped = 0
local_path = Path(local_folder)
if not local_path.exists():
print(f"β Folder not found: {local_folder}")
return 0
files = list(local_path.glob("**/*"))
for file_path in files:
if file_path.is_file():
# Check extension filter
if file_extensions and file_path.suffix.lower() not in file_extensions:
continue
# Calculate S3 key
relative_path = file_path.relative_to(local_path)
s3_key = f"{s3_prefix}{relative_path}"
# Check if file already exists in S3
if skip_existing:
try:
s3_client.head_object(Bucket=bucket, Key=s3_key)
print(f" βοΈ Skipping (already exists): {relative_path}")
skipped += 1
continue
except s3_client.exceptions.ClientError:
# File doesn't exist, proceed with upload
pass
# Upload file
print(f" β¬οΈ Uploading: {relative_path}")
s3_client.upload_file(str(file_path), bucket, s3_key)
uploaded += 1
# Summary
if skipped > 0:
print(f"β
Uploaded {uploaded} files, skipped {skipped} existing files")
else:
print(f"β
Uploaded {uploaded} files")
return uploaded
def monitor_lambda_processing(
logs_client,
s3_client,
bucket_name: str,
function_name: str = "ade-s3-handler",
lookback_minutes: int = 10,
output_prefix: str = "output/"
) -> Dict:
"""
Monitor Lambda processing and display results.
Args:
logs_client: Boto3 CloudWatch Logs client
s3_client: Boto3 S3 client
bucket_name: S3 bucket name
function_name: Lambda function name to monitor
lookback_minutes: How many minutes back to look in logs
output_prefix: S3 prefix for output files
Returns:
Dict with processing statistics
"""
import time
log_group = f"/aws/lambda/{function_name}"
print(f"β³ Monitoring Lambda processing...")
print(" To stop monitoring, press esc followed by double clicking i\n")
# Track processed files
processed_files = set()
processing_files = set()
skipped_files = set()
error_files = set()
start_time = int((time.time() - (lookback_minutes * 60)) * 1000)
try:
while True:
resp = logs_client.filter_log_events(logGroupName=log_group, startTime=start_time)
events = resp.get("events", [])
for event in events:
message = event["message"].strip()
# Track successful completions
if "π Completed pipeline for" in message:
file_name = message.split("Completed pipeline for ")[1].split(" β")[0]
if file_name not in processed_files:
processed_files.add(file_name)
print(f"β
Processed: {file_name}")
# Track files being processed (but don't print)
elif "π€ Starting ADE parsing for" in message:
file_name = message.split("parsing for ")[1].split(" (")[0]
processing_files.add(file_name)
# Track skipped files
elif "βοΈ Skipping" in message and "already processed" in message:
file_name = message.split("Skipping ")[1].split(" -")[0]
if file_name not in skipped_files:
skipped_files.add(file_name)
print(f"βοΈ Skipped (already exists): {file_name}")
# Show errors
elif "β Error processing" in message:
print(f" {message}")
try:
file_name = message.split("Error processing ")[1].split(":")[0]
error_files.add(file_name)
except:
pass
start_time = max(start_time, event["timestamp"] + 1)
time.sleep(5)
except KeyboardInterrupt:
print(f"\nβ Monitoring stopped by user")
# Show summary from logs
print(f"\nπ Lambda Processing Summary:")
print(f" Processed: {len(processed_files)} files")
print(f" Skipped: {len(skipped_files)} files")
print(f" Errors: {len(error_files)} files")
if processed_files:
print("\n Files processed in this session:")
for f in sorted(processed_files):
print(f" - {f}")
# Check what's actually in the output folder
print(f"\nπ Checking S3 {output_prefix} folder...")
response = s3_client.list_objects_v2(
Bucket=bucket_name,
Prefix=output_prefix,
MaxKeys=1000 # Get all files
)
output_files = []
if "Contents" in response:
output_files = [obj["Key"] for obj in response["Contents"] if not obj["Key"].endswith("/")]
print(f" Total files in {output_prefix}: {len(output_files)}")
# Organize by folder
folders = {}
for key in output_files:
parts = key.split("/")
if len(parts) > 2: # Has subfolder
folder = parts[1]
if folder not in folders:
folders[folder] = []
folders[folder].append(key)
elif len(parts) == 2: # Direct in output/
if "root" not in folders:
folders["root"] = []
folders["root"].append(key)
# Display organized summary
if folders:
print(f"\n Files by folder:")
for folder, files in sorted(folders.items()):
if folder == "root":
print(f" {output_prefix} (root): {len(files)} files")
else:
print(f" {output_prefix}{folder}/: {len(files)} files")
# Option to show all files
show_all = input("\n Show all output files? (y/n): ").lower() == 'y'
if show_all:
print("\n All output files:")
for key in sorted(output_files):
print(f" - {key}")
else:
print(f" No files found in {output_prefix} yet")
return {
"processed": len(processed_files),
"skipped": len(skipped_files),
"errors": len(error_files),
"total_output_files": len(output_files),
"processed_files": list(processed_files),
"output_files": output_files
}