DEV Community

Cover image for Day 9: When APIs Fail. How to implement a "Mock Mode" in AWS Lambda.
Eric Rodríguez
Eric Rodríguez

Posted on

Day 9: When APIs Fail. How to implement a "Mock Mode" in AWS Lambda.

Welcome to Day 9 of my Cloud Journey. I am building an AI Financial Agent. Today's goal was to authenticate with the GoCardless API to fetch bank transactions.

But reality hit hard: The API signup page is down. Instead of halting the project, I implemented a Mock Strategy.

Why Mock?
In a real-world Distributed System, dependencies fail all the time. If your Third-Party API goes down, your development shouldn't stop. By mocking the data, I can verify that my infrastructure works, even if the provider doesn't.

The Code Implementation
I modified my Lambda to include a toggle switch.

  1. The Configuration Flag

Python

Simple toggle at the top of my Lambda

MOCK_MODE = True

  1. The Mock Generator

Instead of hardcoding a string, I created a function that returns a Python dictionary matching the exact schema of the real API.

Python
def generate_mock_data():
"""Generates synthetic data matching GoCardless Schema"""
return {
"access_token": "mock_token_live_simulation",
"access_expiry": 86400,
"status": "active"
}

  1. The Logic Switch

In my lambda_handler, I added a bypass before the network request:

Python
if MOCK_MODE:
print("⚠️ NOTICE: Simulation Mode Active.")
# Simulate network latency for realism
time.sleep(1)
return {
'statusCode': 200,
'body': json.dumps(generate_mock_data())
}

The Outcome
My AWS CloudWatch logs show a successful "connection". Now I have a valid JSON object flowing through my system. Tomorrow, I can feed this data into DynamoDB as if it were real money.

Lesson: Never let an external blocker stop your internal progress.

See you on Day 10! ☁️

Top comments (0)