Skip to content

Agenda Fetcher Lambda Implementation #74

Description

@ramtoearth

Agenda Fetcher Lambda Implementation

Objective: Create a serverless function that detects changes in Sessionize API and triggers downstream actions only when changes occur.

Functional Requirements

  1. Input Handling:

    • Accept POST requests with JSON body containing api_url parameter
    • Validate presence of api_url
    • Return 400 error if missing
  2. Data Fetching:

    • Fetch JSON data from provided api_url
    • Handle HTTP errors (non-2xx responses)
  3. Change Detection:

    • Compute MD5 hash of fetched JSON payload
    • Store/retrieve last known hash in DynamoDB
    • Compare current hash with stored hash
    • Track three states:
      • First run (no existing hash)
      • Changed data
      • Unchanged data
  4. State Management:

    • Store initial hash in DynamoDB on first run
    • Update hash when changes detected
    • Maintain updatedAt timestamp
  5. Action Trigger:

    • Set mutation flag when:
      • First run (no existing hash)
      • Hash mismatch (changed data)
    • Skip mutation when hash unchanged
  6. Response:

    • Return 200 with { changed: boolean }
    • Include metrics for each state
  7. Error Handling:

    • Catch all processing errors
    • Return 500 for unexpected errors
    • Log detailed errors with context

Non-Functional Requirements

  1. Observability:

    • Integrate AWS Powertools:
      • Logger for structured logging
      • Tracer for AWS call instrumentation
      • Metrics for state tracking
    • Add middleware for automatic context injection
  2. Infrastructure:

    • Use DynamoDB table with single-item design
    • Configure environment variables:
      • DYNAMODB_TABLE_NAME
      • DYNAMODB_HASH_KEY
  3. Efficiency:

    • Only trigger downstream actions when data changes
    • Avoid unnecessary network calls
    • Optimize DynamoDB operations

Flowchart Diagram

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#1e1e2e'}}}%%
flowchart TD
    A([Start]) --> B[Parse Event Body]
    B --> C{api_url present?}
    C -->|No| D[Return 400 Error]
    C -->|Yes| E[Fetch External API]
    E --> F{API Response OK?}
    F -->|No| G[Throw Error]
    F -->|Yes| H[Compute JSON Hash]
    H --> I[Get Last Hash from DynamoDB]
    I --> J{Item exists?}
    J -->|No| K[Store New Hash\nSet mutation flag=true]
    J -->|Yes| L{Hash Changed?}
    L -->|No| M[Set flag=false\nMetric: NoChange]
    L -->|Yes| N[Update Hash\nSet flag=true\nMetric: HashChanged]
    K --> O{Mutation needed?}
    N --> O
    M --> O
    O -->|flag=true| P[Execute Mutation\nMetric: MutationInvoked]
    O -->|flag=false| Q[Skip Mutation]
    P --> R[Return 200\nchanged: true]
    Q --> S[Return 200\nchanged: false]
    G --> T[Log Error]
    T --> U[Return 500 Error]
    D --> U
    
    style A fill:#2ecc71,stroke:#27ae60
    style U fill:#e74c3c,stroke:#c0392b
    style R fill:#3498db,stroke:#2980b9
    style S fill:#3498db,stroke:#2980b9
Loading

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions