4.2.1. Event-Driven and Microservice Integration
💡 First Principle: Loose coupling between GenAI components and enterprise systems via events and APIs means a model update, a RAG pipeline change, or a provider switch has zero impact on consuming applications — they never know what's behind the integration surface.
Event-driven GenAI integration with EventBridge:
Webhook integration for real-time FM triggers:
# API Gateway → Lambda webhook: triggered by external system events
def lambda_handler(event, context):
body = json.loads(event['body'])
# Validate webhook signature (security)
if not validate_hmac(event['headers'], body):
return {'statusCode': 401, 'body': 'Unauthorized'}
# Async processing: send to SQS to decouple processing from response
sqs.send_message(
QueueUrl=PROCESSING_QUEUE_URL,
MessageBody=json.dumps({
'type': body['event_type'],
'payload': body['data'],
'received_at': datetime.utcnow().isoformat()
})
)
# Return immediately — don't make caller wait for FM processing
return {'statusCode': 202, 'body': json.dumps({'status': 'accepted'})}
Pattern decision: sync vs. async FM integration:
| Pattern | When to Use | AWS Services |
|---|---|---|
| Synchronous | User waiting for response; SLA < 30s | API Gateway → Lambda → Bedrock |
| Asynchronous | Background processing; no waiting; SLA in minutes | SQS → Lambda → Bedrock → SNS notify |
| Streaming | Progressive display; long-form generation | API Gateway WebSocket → Lambda → Bedrock streaming |
| Batch | Nightly processing; cost optimization | S3 event → Lambda batch → Bedrock → S3 output |
⚠️ Exam Trap: API Gateway has a maximum integration timeout of 29 seconds. Long-context Bedrock calls that take 30+ seconds will cause API Gateway to return a 504 timeout to the client even if the Lambda and Bedrock call eventually succeed. For long-running FM calls, use an async pattern (SQS + polling or WebSocket) rather than a synchronous API Gateway integration.
Reflection Question: Your enterprise system needs to generate AI summaries for every new Salesforce opportunity created. The sales team expects summaries to appear within 5 minutes of opportunity creation. What event-driven architecture would you implement, and why would you choose async over sync?