-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathlambda_function.py
More file actions
445 lines (370 loc) · 13.3 KB
/
lambda_function.py
File metadata and controls
445 lines (370 loc) · 13.3 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
#!/usr/bin/env python3
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
"""
IDP SDK Example: Lambda Function
This example demonstrates how to use the IDP SDK in an AWS Lambda function
for document processing automation.
Deployment Instructions:
------------------------
1. Create a Lambda layer with the IDP SDK:
cd lib/idp_sdk
pip install . -t python/
zip -r idp-sdk-layer.zip python/
aws lambda publish-layer-version \
--layer-name idp-sdk \
--zip-file fileb://idp-sdk-layer.zip \
--compatible-runtimes python3.11 python3.12
2. Create the Lambda function with this code and attach the layer.
3. Configure environment variables:
- IDP_STACK_NAME: Your IDP stack name (e.g., "IDP-Nova-1")
- OUTPUT_BUCKET: Bucket to write results (optional)
4. Required IAM permissions:
- cloudformation:DescribeStacks
- cloudformation:ListStackResources
- s3:GetObject, s3:PutObject, s3:ListBucket (on IDP buckets)
- sqs:SendMessage (on DocumentQueue)
- dynamodb:GetItem, dynamodb:Query, dynamodb:Scan (on TrackingTable)
- lambda:InvokeFunction (on LookupFunction)
Example Event:
--------------
{
"action": "process",
"source": "s3://my-bucket/documents/",
"batch_prefix": "lambda-batch"
}
Or for status check:
{
"action": "status",
"batch_id": "lambda-batch-20260123-123456"
}
"""
import json
import logging
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def handler(event, context):
"""
Lambda handler for IDP SDK operations.
Supports actions:
- process: Submit documents for processing
- status: Check batch status
- download: Download results (to S3)
- manifest: Generate manifest
- config: Configuration operations
"""
# Get stack name from environment or event
stack_name = event.get("stack_name") or os.environ.get("IDP_STACK_NAME")
region = event.get("region") or os.environ.get("AWS_REGION")
action = event.get("action", "process")
logger.info(f"Action: {action}, Stack: {stack_name}")
try:
if action == "process":
return process_documents(event, stack_name, region)
elif action == "status":
return get_status(event, stack_name, region)
elif action == "download":
return download_results(event, stack_name, region)
elif action == "manifest":
return generate_manifest(event)
elif action == "config":
return config_operation(event, stack_name, region)
else:
return {
"statusCode": 400,
"body": json.dumps({"error": f"Unknown action: {action}"}),
}
except Exception as e:
logger.exception(f"Error in {action}")
return {"statusCode": 500, "body": json.dumps({"error": str(e)})}
def process_documents(event, stack_name, region):
"""Submit documents for processing."""
from idp_sdk import IDPClient
if not stack_name:
return {"statusCode": 400, "body": json.dumps({"error": "stack_name required"})}
client = IDPClient(stack_name=stack_name, region=region)
# Get source from event
source = event.get("source") # Can be S3 URI, manifest path, or directory
s3_uri = event.get("s3_uri")
manifest = event.get("manifest")
test_set = event.get("test_set")
if not any([source, s3_uri, manifest, test_set]):
return {
"statusCode": 400,
"body": json.dumps(
{"error": "One of source, s3_uri, manifest, or test_set required"}
),
}
# Process
result = client.batch.process(
source=source,
s3_uri=s3_uri,
manifest=manifest,
test_set=test_set,
batch_prefix=event.get("batch_prefix", "lambda-batch"),
file_pattern=event.get("file_pattern", "*.pdf"),
number_of_files=event.get("number_of_files"),
config_path=event.get("config_path"),
context=event.get("context"),
)
logger.info(f"Batch submitted: {result.batch_id}")
return {
"statusCode": 200,
"body": json.dumps(
{
"batch_id": result.batch_id,
"document_count": len(result.document_ids),
"documents_queued": result.queued,
"documents_failed": result.failed,
}
),
}
def get_status(event, stack_name, region):
"""Get status of a batch or document."""
from idp_sdk import IDPClient
if not stack_name:
return {"statusCode": 400, "body": json.dumps({"error": "stack_name required"})}
batch_id = event.get("batch_id")
document_id = event.get("document_id")
if not batch_id and not document_id:
return {
"statusCode": 400,
"body": json.dumps({"error": "batch_id or document_id required"}),
}
client = IDPClient(stack_name=stack_name, region=region)
if batch_id:
status = client.batch.get_status(batch_id=batch_id)
else:
doc_status = client.document.get_status(document_id=document_id)
from idp_sdk import BatchStatus
status = BatchStatus(
batch_id="single-doc",
documents=[doc_status],
total=1,
completed=1 if doc_status.status.value == "COMPLETED" else 0,
failed=1 if doc_status.status.value == "FAILED" else 0,
in_progress=1
if doc_status.status.value in ["PROCESSING", "RUNNING"]
else 0,
queued=1 if doc_status.status.value == "QUEUED" else 0,
success_rate=1.0 if doc_status.status.value == "COMPLETED" else 0.0,
all_complete=doc_status.status.value in ["COMPLETED", "FAILED"],
)
# Convert documents to serializable format
documents = []
for doc in status.documents[:20]: # Limit to avoid large responses
documents.append(
{
"document_id": doc.document_id,
"status": doc.status.value,
"duration_seconds": doc.duration_seconds,
"error": doc.error,
}
)
return {
"statusCode": 200,
"body": json.dumps(
{
"batch_id": status.batch_id,
"total": status.total,
"completed": status.completed,
"failed": status.failed,
"in_progress": status.in_progress,
"queued": status.queued,
"success_rate": status.success_rate,
"all_complete": status.all_complete,
"documents": documents,
}
),
}
def download_results(event, stack_name, region):
"""
Download results to S3.
Note: Lambda has limited /tmp storage (512MB default, up to 10GB).
This function downloads to /tmp and optionally copies to another S3 bucket.
"""
import os as os_module
import tempfile
import boto3
from idp_sdk import IDPClient
if not stack_name:
return {"statusCode": 400, "body": json.dumps({"error": "stack_name required"})}
batch_id = event.get("batch_id")
if not batch_id:
return {"statusCode": 400, "body": json.dumps({"error": "batch_id required"})}
output_bucket = event.get("output_bucket") or os_module.environ.get("OUTPUT_BUCKET")
output_prefix = event.get("output_prefix", f"idp-results/{batch_id}")
file_types = event.get("file_types", ["sections", "summary"])
client = IDPClient(stack_name=stack_name, region=region)
# Download to temp directory
with tempfile.TemporaryDirectory() as tmpdir:
result = client.batch.download_results(
batch_id=batch_id,
output_dir=tmpdir,
file_types=file_types,
)
logger.info(f"Downloaded {result.files_downloaded} files to {tmpdir}")
# If output bucket specified, copy files there
if output_bucket:
s3 = boto3.client("s3")
files_copied = 0
for root, dirs, files in os_module.walk(tmpdir):
for filename in files:
local_path = os_module.path.join(root, filename)
rel_path = os_module.path.relpath(local_path, tmpdir)
s3_key = f"{output_prefix}/{rel_path}"
s3.upload_file(local_path, output_bucket, s3_key)
files_copied += 1
return {
"statusCode": 200,
"body": json.dumps(
{
"files_downloaded": result.files_downloaded,
"files_copied": files_copied,
"output_location": f"s3://{output_bucket}/{output_prefix}/",
}
),
}
return {
"statusCode": 200,
"body": json.dumps(
{
"files_downloaded": result.files_downloaded,
"documents_downloaded": result.documents_downloaded,
"note": "Files downloaded to Lambda /tmp. Specify output_bucket to persist.",
}
),
}
def generate_manifest(event):
"""Generate a manifest from S3 URI."""
from idp_sdk import IDPClient
s3_uri = event.get("s3_uri")
if not s3_uri:
return {"statusCode": 400, "body": json.dumps({"error": "s3_uri required"})}
client = IDPClient()
result = client.manifest.generate(
s3_uri=s3_uri,
file_pattern=event.get("file_pattern", "*.pdf"),
recursive=event.get("recursive", True),
)
return {
"statusCode": 200,
"body": json.dumps(
{
"document_count": result.document_count,
"baselines_matched": result.baselines_matched,
}
),
}
def config_operation(event, stack_name, region):
"""Configuration operations."""
from idp_sdk import IDPClient
operation = event.get("operation", "create")
if operation == "create":
# No stack required
client = IDPClient()
result = client.config.create(
features=event.get("features", "min"),
pattern=event.get("pattern", "pattern-2"),
)
return {
"statusCode": 200,
"body": json.dumps(
{
"yaml_content": result.yaml_content[
:5000
], # Truncate for response size
}
),
}
elif operation == "validate":
# Requires config content in event
config_content = event.get("config_content")
if not config_content:
return {
"statusCode": 400,
"body": json.dumps({"error": "config_content required for validate"}),
}
import tempfile
import yaml
client = IDPClient()
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
if isinstance(config_content, dict):
yaml.dump(config_content, f)
else:
f.write(config_content)
temp_path = f.name
try:
result = client.config.validate(
config_file=temp_path,
pattern=event.get("pattern", "pattern-2"),
)
return {
"statusCode": 200,
"body": json.dumps(
{
"valid": result.valid,
"errors": result.errors,
"warnings": result.warnings,
}
),
}
finally:
import os
os.unlink(temp_path)
elif operation == "download":
if not stack_name:
return {
"statusCode": 400,
"body": json.dumps({"error": "stack_name required for download"}),
}
client = IDPClient(stack_name=stack_name, region=region)
result = client.config.download(format=event.get("format", "full"))
return {
"statusCode": 200,
"body": json.dumps(
{
"config_keys": list(result.config.keys()),
"yaml_content": result.yaml_content[:5000], # Truncate
}
),
}
else:
return {
"statusCode": 400,
"body": json.dumps({"error": f"Unknown config operation: {operation}"}),
}
# For local testing
if __name__ == "__main__":
import sys
# Test events
test_events = {
"process": {
"action": "process",
"stack_name": "IDP-Nova-1",
"s3_uri": "s3://test-bucket/documents/",
},
"status": {
"action": "status",
"stack_name": "IDP-Nova-1",
"batch_id": "test-batch-123",
},
"config": {
"action": "config",
"operation": "create",
"features": "min",
},
}
if len(sys.argv) > 1:
event_name = sys.argv[1]
event = test_events.get(event_name)
if event:
result = handler(event, None)
print(json.dumps(result, indent=2))
else:
print(f"Unknown test event: {event_name}")
print(f"Available: {list(test_events.keys())}")
else:
print("Usage: python lambda_function.py <event_name>")
print(f"Available events: {list(test_events.keys())}")