How to maintain previous response when AWS Lambda function condition fails in API Gateway?

I’m working with an AWS Lambda function that gets called through API Gateway. Here’s my current implementation:

import json

def handler_function(event_data, context_data):
    sample_text = "bar"
    if "b" in sample_text:
        return {
            "statusCode": 200,
            "body": json.dumps(sample_text)
        }

When I test this with HTTP API and REST API, both work fine since the condition is true and return “bar”. However, I’m facing an issue when the condition becomes false. Let’s say sample_text changes to “car” and the condition fails. Instead of getting the previous successful response, HTTP API returns null and REST API shows {"message": "Internal server error"}.

I want the API to keep returning the last successful result instead of failing. Here’s what happens when the condition fails:

import json

def handler_function(event_data, context_data):
    sample_text = "car"
    if "b" in sample_text:
        return {
            "statusCode": 200,
            "body": json.dumps(sample_text)
        }
    else:
        # Need help here: how to return previous successful response?

I’m new to building APIs. What’s the best approach to handle this scenario?

yeah, totally get that! use s3 for storing the last successful response, it’s cheap n’ easy, or go for ElastiCache if u need speed. write the good responses when condition is met and just pull em back when it fails.

I encountered a similar challenge while developing my first Lambda API. It’s important to understand that Lambda functions are stateless, meaning they do not retain any data once execution completes. To maintain the last successful response, you will need to use external storage. I opted for DynamoDB, which integrates seamlessly with Lambda, allowing you to store the response when the condition is met and retrieve it if the condition fails. For simpler data, consider AWS Systems Manager Parameter Store as a budget-friendly alternative. In practice: if the condition evaluates to true, save and return the response; if false, fetch the last saved response. Be sure to account for scenarios when no previous response is available, and implement error handling for your storage interactions, as they can also encounter issues.

Your problem comes from Lambda being stateless - each run starts fresh with zero memory. I usually fix this with AWS Systems Manager Parameter Store since it’s simple and cheap. When your condition works, save the response there. When it fails, grab the cached value. But here’s the thing - returning old data when logic breaks might hide real problems. Ask yourself: do you actually need caching, or should you fix why the condition fails in the first place? If you really need caching, wrap your storage calls in try-catch blocks because external services fail too. Don’t forget to handle cold starts where there’s no cached response yet.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.