Preporato

AIP-C01 Study Guide

Foundation Model Integration, Data Management, and ComplianceGenAI Solution Design and ArchitectureIntegration Patterns for Enterprise Applications

Key Concepts

  • API-first integration approach

  • Event-driven GenAI architectures

  • Microservices with FM integration

  • Legacy system integration

  • Error handling and fallback strategies

Integration Patterns for Enterprise Applications

Overview

Integrating foundation models into enterprise applications requires careful architectural decisions around API design, event-driven patterns, microservices integration, and error handling. AWS provides multiple services that work together to build robust, scalable GenAI solutions.

This topic covers the key integration patterns for connecting Amazon Bedrock to enterprise systems, including API Gateway for access control, Step Functions for orchestration, EventBridge for event-driven workflows, and SQS for asynchronous processing.

Key Principle

Enterprise GenAI integration is about building governance around model access - authorization, rate limiting, cost control, observability, and graceful error handling are essential for production systems.

Exam Tip

Expect questions about when to use synchronous vs asynchronous patterns, how to handle errors and retries, and which AWS services to use for different integration scenarios. Know the Step Functions Bedrock integration actions.


Architecture Diagram

The following diagram illustrates common enterprise integration patterns with Amazon Bedrock:

Enterprise Integration Patterns
Figure 1: Key integration patterns showing API Gateway, Step Functions, EventBridge, and SQS with Bedrock

Key Concepts

API-First Integration Approach

AI Gateway Pattern

API Gateway as the Access Layer:

Building an AI gateway with Amazon API Gateway provides enterprise governance:

Key Capabilities:

  • Authorization - JWT validation, IAM, Cognito integration
  • Rate Limiting - Usage plans and API keys
  • Quota Management - Per-tenant limits
  • Request Throttling - Protect backend from overload
  • WAF Integration - Protection against attacks
  • Canary Releases - Gradual rollout of changes

Architecture:

Client → API Gateway → Lambda → Bedrock
              ↓
         WAF, Auth, Throttling

Benefits:

  • Centralized access control
  • Cost allocation by tenant/API key
  • Consistent logging and monitoring
  • Decouples clients from Bedrock directly

API Types

API Gateway Options for GenAI:

REST APIs:

  • Full feature set (throttling, caching, WAF)
  • Higher latency than HTTP APIs
  • Best for: Enterprise features, complex authorization

HTTP APIs:

  • Lower latency, lower cost
  • Limited features vs REST
  • Best for: Performance-critical applications

WebSocket APIs:

  • Bidirectional communication
  • Persistent connections
  • Best for: Streaming responses, chat applications

Private APIs:

  • VPC-only access
  • No internet exposure
  • Best for: Internal applications, compliance
PYAPI Gateway Lambda Integration
import boto3
import json

bedrock = boto3.client('bedrock-runtime')

def lambda_handler(event, context):
    # Extract user input from API Gateway event
    body = json.loads(event.get('body', '{}'))
    user_message = body.get('message', '')

    # Validate input
    if not user_message or len(user_message) > 10000:
        return {
            'statusCode': 400,
            'body': json.dumps({'error': 'Invalid input'})
        }

    try:
        # Invoke Bedrock with Converse API
        response = bedrock.converse(
            modelId='anthropic.claude-3-sonnet-20240229-v1:0',
            messages=[
                {'role': 'user', 'content': [{'text': user_message}]}
            ],
            inferenceConfig={'maxTokens': 1024}
        )

        output = response['output']['message']['content'][0]['text']

        return {
            'statusCode': 200,
            'body': json.dumps({'response': output})
        }

    except bedrock.exceptions.ThrottlingException:
        return {
            'statusCode': 429,
            'body': json.dumps({'error': 'Rate limit exceeded'})
        }
    except Exception as e:
        return {
            'statusCode': 500,
            'body': json.dumps({'error': 'Internal error'})
        }

Event-Driven GenAI Architectures

EventBridge Integration

Amazon EventBridge for GenAI Workflows:

Use Cases:

  • Trigger GenAI processing on file uploads
  • React to model inference completions
  • Cross-account event routing
  • Audit and compliance logging

Event Patterns:

{
  "source": ["aws.s3"],
  "detail-type": ["Object Created"],
  "detail": {
    "bucket": {"name": ["documents-bucket"]},
    "object": {"key": [{"prefix": "input/"}]}
  }
}

Architecture:

S3 Upload → EventBridge → Step Functions → Bedrock
                  ↓
              Archive (audit)

Benefits:

  • Loose coupling between components
  • Easy to add new consumers
  • Built-in archive and replay
  • Cross-account routing

Event-Driven Patterns

Common Event-Driven GenAI Patterns:

1. Document Processing Pipeline:

S3 → EventBridge → Step Functions
                        ↓
                   Textract → Bedrock → S3 Output

2. Real-Time Classification:

Kinesis → Lambda → Bedrock → DynamoDB
              ↓
         EventBridge (alerts)

3. Batch Inference Completion:

Bedrock Batch → EventBridge → Lambda → SNS
    (completed)              (notify)

4. Cross-Account AI Services:

Account A: Event → EventBridge (central bus)
                        ↓
Account B: → Step Functions → Bedrock

Microservices with FM Integration

Microservices Patterns

Integrating Bedrock into Microservices:

Service Mesh Pattern:

  • Each microservice calls Bedrock independently
  • Shared SDK configuration
  • Distributed tracing with X-Ray

API Gateway Pattern:

  • Central AI gateway service
  • Other services call gateway
  • Centralized governance

Sidecar Pattern:

  • AI proxy container alongside service
  • Handles model calls and caching
  • Consistent across services

Best Practices:

  • Isolate AI logic in dedicated service
  • Use async patterns where possible
  • Implement circuit breakers
  • Cache responses when appropriate

Microservices Integration Patterns

PatternProsConsBest For
Direct IntegrationSimple, low latencyNo centralized governanceSmall teams, single service
API GatewayCentralized control, governanceAdditional hop, complexityEnterprise, multi-team
Event-DrivenLoose coupling, scalableEventual consistencyAsync workloads, pipelines
SidecarConsistent, portableResource overheadKubernetes, service mesh

Legacy System Integration

Legacy Integration

Connecting GenAI to Legacy Systems:

Common Challenges:

  • SOAP/XML APIs vs REST/JSON
  • Synchronous blocking calls
  • Limited scalability
  • Security constraints

Integration Strategies:

1. API Facade:

Legacy → API Gateway → Lambda (transform) → Bedrock
  • Transform SOAP to REST
  • Handle authentication differences
  • Abstract legacy complexity

2. Event Bridge:

Legacy → SQS → Lambda → Bedrock → SQS → Legacy
  • Asynchronous decoupling
  • Legacy polls for results
  • No changes to legacy system

3. Database Integration:

Legacy → DB → Lambda (trigger) → Bedrock → DB
  • DynamoDB Streams or RDS proxy
  • Legacy reads results from DB
  • Minimal integration surface

Error Handling and Fallback Strategies

Error Handling

Robust Error Handling for GenAI:

Common Bedrock Errors: | Error | Cause | Handling | |-------|-------|----------| | ThrottlingException | Rate limit exceeded | Exponential backoff | | ValidationException | Invalid input/parameters | Validate before sending | | ModelTimeoutException | Model took too long | Retry or use smaller model | | AccessDeniedException | IAM permission issue | Fix IAM policy | | ServiceUnavailableException | Bedrock unavailable | Retry with backoff |

Retry Strategy:

import time
from botocore.config import Config

config = Config(
    retries={
        'max_attempts': 3,
        'mode': 'adaptive'  # Exponential backoff
    }
)
bedrock = boto3.client('bedrock-runtime', config=config)

Fallback Strategies

Implementing Fallbacks:

1. Model Fallback:

Claude 3.5 Sonnet (primary)
    ↓ (failure)
Claude 3 Haiku (fallback)
    ↓ (failure)
Cached response or error

2. Regional Fallback:

us-east-1 (primary)
    ↓ (unavailable)
us-west-2 (secondary)

3. Graceful Degradation:

Full GenAI response (normal)
    ↓ (overloaded)
Cached similar response
    ↓ (no cache)
Static fallback message

Implementation with Step Functions:

  • Use Catch blocks for error handling
  • Retry with exponential backoff
  • Choice state for fallback routing
  • Error notification via SNS
JSONStep Functions Error Handling
{
  "InvokeBedrock": {
    "Type": "Task",
    "Resource": "arn:aws:states:::bedrock:invokeModel",
    "Parameters": {
      "ModelId": "anthropic.claude-3-sonnet-20240229-v1:0",
      "Body": {
        "anthropic_version": "bedrock-2023-05-31",
        "messages": [{"role": "user", "content.$": "$.prompt"}],
        "max_tokens": 1024
      }
    },
    "Retry": [
      {
        "ErrorEquals": ["Bedrock.ThrottlingException"],
        "IntervalSeconds": 2,
        "MaxAttempts": 3,
        "BackoffRate": 2
      }
    ],
    "Catch": [
      {
        "ErrorEquals": ["States.ALL"],
        "Next": "FallbackModel"
      }
    ],
    "Next": "ProcessResponse"
  },
  "FallbackModel": {
    "Type": "Task",
    "Resource": "arn:aws:states:::bedrock:invokeModel",
    "Parameters": {
      "ModelId": "anthropic.claude-3-haiku-20240307-v1:0",
      "Body": {
        "anthropic_version": "bedrock-2023-05-31",
        "messages": [{"role": "user", "content.$": "$.prompt"}],
        "max_tokens": 512
      }
    },
    "Next": "ProcessResponse"
  }
}

How It Works

Step Functions Bedrock Integration

Step Functions Bedrock Integration
Figure 2: AWS Step Functions orchestrating Bedrock calls with error handling and parallel processing

Event-Driven Document Processing

Event-Driven Document Processing
Figure 3: Event-driven architecture for document processing with S3, EventBridge, and Bedrock

Use Cases

Use Case 1: Multi-Tenant AI Gateway

Scenario: SaaS platform providing AI capabilities to multiple customers with usage tracking.

Architecture:

Tenants → API Gateway → Lambda → Bedrock
              ↓
         Usage Plans (per tenant)
              ↓
         CloudWatch (cost allocation)

Implementation:

  • API keys per tenant
  • Usage plans with quotas
  • Custom domain with WAF
  • Per-tenant cost tagging

Benefits:

  • Isolated rate limits per customer
  • Cost allocation and billing
  • Centralized security controls
  • Easy to add new tenants

Use Case 2: Document Processing Pipeline

Scenario: Process uploaded documents with AI for classification and extraction.

Architecture:

S3 Upload → EventBridge → Step Functions
                              ↓
                         Textract (OCR)
                              ↓
                         Bedrock (classify)
                              ↓
                         Bedrock (extract)
                              ↓
                         DynamoDB (store)
                              ↓
                         SNS (notify)

Step Functions Benefits:

  • Parallel processing (Distributed Map)
  • Automatic retries on failure
  • Visual workflow monitoring
  • Native Bedrock integration

Use Case 3: Real-Time Chat with Fallbacks

Scenario: Customer chat application with high availability requirements.

Architecture:

WebSocket API → Lambda → Bedrock (us-east-1)
                    ↓ (failure)
                Bedrock (us-west-2)
                    ↓ (failure)
                Cached response

Implementation:

  • Primary/secondary region routing
  • Circuit breaker pattern
  • Response caching for common queries
  • Graceful degradation message

Best Practices

Enterprise Integration Best Practices
  1. Use API Gateway for governance - Centralize auth, rate limiting, and monitoring
  2. Implement retry with backoff - Handle ThrottlingException gracefully
  3. Design for failure - Include fallback models and graceful degradation
  4. Use async when possible - SQS/EventBridge for non-real-time workloads
  5. Leverage Step Functions - Native Bedrock integration with error handling
  6. Enable observability - X-Ray tracing, CloudWatch metrics, logging
  7. Isolate GenAI logic - Easier to update models and prompts

Common Exam Scenarios

Exam Scenarios and Solutions

ScenarioSolutionWhy
Need to process documents triggered by S3 uploadEventBridge + Step Functions + BedrockEvent-driven, orchestrated workflow
Multi-tenant SaaS with usage limitsAPI Gateway with usage plans and API keysBuilt-in rate limiting and quota management
High availability GenAI chatMulti-region with Route 53 failoverRegional redundancy for availability
Long-running document batch processingStep Functions Distributed Map + BedrockParallel processing with state management
Legacy SOAP system needs AI capabilitiesAPI Gateway + Lambda transformation layerTransform SOAP to REST, abstract complexity

Common Pitfalls

Pitfall 1: No Error Handling for Throttling

Mistake: Calling Bedrock without retry logic for rate limit errors.

Why it's wrong: ThrottlingException is common under load; failed requests hurt user experience.

Correct Approach:

  • Implement exponential backoff retries
  • Use adaptive retry mode in SDK
  • Consider SQS to smooth traffic spikes
  • Monitor throttling metrics
Pitfall 2: Synchronous Calls for Batch Processing

Mistake: Using synchronous API calls for processing thousands of documents.

Why it's wrong: Inefficient, expensive, and prone to timeouts.

Correct Approach:

  • Use Bedrock batch inference (50% savings)
  • Queue-based processing with SQS
  • Step Functions Distributed Map for orchestration
  • EventBridge for completion notifications
Pitfall 3: Direct Bedrock Access from Clients

Mistake: Exposing Bedrock credentials directly to client applications.

Why it's wrong: No governance, cost control, or security boundaries.

Correct Approach:

  • Always proxy through API Gateway
  • Use Cognito or IAM for authentication
  • Implement rate limiting and quotas
  • Log all requests for audit

Test Your Knowledge

Q

A company wants to trigger AI processing whenever a document is uploaded to S3. What is the RECOMMENDED architecture?

APoll S3 bucket every minute with Lambda
BUse EventBridge to trigger Step Functions workflow
CUse S3 bucket notifications directly to Bedrock
DCreate a scheduled batch job to process S3 files
Q

An application is receiving ThrottlingException errors from Bedrock during peak usage. What is the BEST approach?

AImmediately switch to a different model
BImplement exponential backoff retry with queuing
CIncrease Lambda memory to process faster
DDisable rate limiting in API Gateway
Q

Which AWS service provides native integration for orchestrating multiple Bedrock calls with error handling and retries?

AAmazon EventBridge
BAWS Step Functions
CAmazon SQS
DAWS Lambda


Quick Reference

Integration Pattern Selection Guide

TEXTWhen to Use Each Pattern
API Gateway + Lambda:
  - Real-time synchronous requests
  - Need auth, rate limiting, WAF
  - Multi-tenant with usage tracking

Step Functions + Bedrock:
  - Multi-step workflows
  - Need error handling and retries
  - Parallel processing (Distributed Map)
  - Long-running processes

EventBridge + Lambda:
  - Event-driven triggers (S3, etc.)
  - Cross-account routing
  - Audit and replay needs
  - Loose coupling required

SQS + Lambda:
  - Buffering against traffic spikes
  - Guaranteed message delivery
  - Fan-out to multiple processors
  - Async with retry/DLQ

Common Bedrock Error Codes

Bedrock Error Handling Reference

ErrorHTTP CodeCauseRecommended Action
ThrottlingException429Rate limit exceededExponential backoff retry
ValidationException400Invalid requestFix input parameters
AccessDeniedException403IAM permission issueUpdate IAM policy
ModelTimeoutException408Model too slowRetry or use smaller model
ServiceUnavailableException503Service issueRetry with backoff

Further Reading

Related AWS Services

API GatewayStep FunctionsEventBridgeSQS