Why Lambda Calling Lambda Breaks Your Serverless App
Writing
SYSTEM DESIGN
Published November 20, 202611 min read

Why Lambda Calling Lambda Breaks Your Serverless App

Lambda calling Lambda is an anti-pattern that double-bills you, stacks timeouts, and hides failures. Here is the real cost, plus a decision table for the fix.

Rabinarayan Patra - Software Development Engineer

By Rabinarayan Patra

SDE II at Amazon

lambda-anti-patternaws-lambdaserverlessstep-functionsevent-driven-architecturesystem-design

The first serverless system I helped untangle had eleven Lambda functions and a bill nobody could explain. Every function looked cheap. The invoice did not.

The cause was Lambda calling Lambda, repeated across the codebase: a function finishing its own work, then calling lambda.invoke() on the next function and waiting for the answer. It reads like a method call. It bills like three servers.

Lambda calling Lambda is the most common serverless anti-pattern I run into, and the reason it survives code review is that it looks like ordinary composition. Here is what it actually does to cost, error handling, and scaling, and how to decide what to replace it with.

What does Lambda calling Lambda actually cost you?

It costs you the full duration of every function in the chain, running at the same time, for the length of the slowest one. AWS bills Lambda by execution duration, and a function blocked on a synchronous child invoke is still executing. It is just executing nothing.

The AWS Compute Blog is blunt about it in its anti-patterns write-up: "With Lambda, you pay for the duration of an invocation. In this example, while the Create invoice function runs, two other functions are also running in a wait state."

Say your chain looks like this. createOrder does 60 ms of its own work, then calls createInvoice. createInvoice does 60 ms of work, then calls takePayment. takePayment does the real job in 640 ms.

You did 640 ms of useful work. You were billed for 2,240 ms. Around 71% of the spend bought you nothing except two functions holding a socket open.

Now scale that. Because the parents cannot finish until the child does, the concurrency of all three functions has to match. AWS makes the same point: "The concurrency of all three functions must be equal. In a busy system, this uses more concurrency than would otherwise be needed." Your default account quota is 1,000 concurrent executions. A three-deep chain burns that budget three times as fast as the work requires, so you hit throttling at a third of the traffic you thought you could serve.

The insidious part is that no single dashboard shows this. Each function's duration looks reasonable in isolation. You have to sum the chain to see it, and nothing in the console sums the chain for you.

Why does nested invocation make error handling so much harder?

Because an error at the bottom of the chain has to be translated at every level on the way back up, and each level has to decide whether it is a retryable failure, a permanent one, or something to swallow.

AWS puts it this way: "In nested invocations, error handling can become more complex. Either errors are thrown to parent functions to handle at the top-level function, or functions require custom handling."

Both options are bad in different ways. Rethrowing means your top-level function needs to understand failure modes of services it never talks to directly. Handling locally means every function grows its own retry and compensation logic, and that logic drifts between functions until nobody knows which layer owns a rollback.

Then there is the part that surprises people:

# Lambda does NOT retry this. At all.
response = lambda_client.invoke(
    FunctionName="takePayment",
    InvocationType="RequestResponse",
    Payload=json.dumps(event),
)

For a direct synchronous invoke, Lambda does not automatically retry function errors. The docs are explicit: "When you invoke a function directly, you determine the strategy for handling errors related to your function code. Lambda does not automatically retry these types of errors on your behalf."

So where do retries come from? Your SDK. The AWS SDKs ship with standard retry mode, which makes up to three attempts. That is fine for one hop. Stack it three deep and a single failure at the bottom can be attempted up to 27 times, because each layer retries its child independently and has no idea the layers above are doing the same thing. Your payment provider sees a burst. Your logs show one failed request.

If you take one thing from this section: a nested chain needs idempotency keys far more urgently than a flat one does, because the retry multiplication is invisible until it charges someone twice.

What breaks when the chain gets slower than you expected?

Availability collapses to the slowest link, and the 15-minute wall arrives sooner than the arithmetic suggests.

Lambda's maximum function timeout is 900 seconds. That sounds generous until you remember the parent's clock is running the whole time the child works. A parent with a 30-second timeout calling a child that occasionally takes 35 seconds does not get a slow response. It gets a timeout, at the parent, while the child keeps running to completion and succeeds. The work happened. The caller thinks it failed. If the caller retries, the work happens twice.

That specific failure, a successful child under a timed-out parent, is the one I have seen cause real damage. It creates duplicate records that no error log accounts for.

AWS frames the coupling problem directly: "Processing a payment typically takes longer than creating an invoice. In this model, the availability of the entire workflow is limited by the slowest function." You have taken three independently scalable functions and welded them into one distributed monolith with worse debugging.

Payload limits bite too. A synchronous invoke allows 6 MB each for request and response. An asynchronous one allows 1 MB. Chains tend to accumulate context as they go, passing the original event plus each step's output, and the ceiling arrives without warning.

When is a direct Lambda invoke actually fine?

When you genuinely need the return value inline, the callee is fast and stable, and the call is one hop rather than a chain. That case exists and it is not worth being dogmatic about.

Concretely, direct synchronous invoke is defensible when:

  • You are calling a shared utility function that returns in tens of milliseconds, such as a token minter or a small enrichment lookup.
  • The alternative would be duplicating that logic into a Lambda layer and redeploying every consumer when it changes.
  • The chain is exactly one level deep and you have no plans to grow it.

It stops being defensible the moment it becomes a workflow. Two signals to watch for: a third function joins the chain, or someone adds a branch ("if the payment fails, call the refund function"). Both mean you are writing a state machine inside a function, which is a separate anti-pattern AWS calls Lambda as orchestrator, and the recommended fix is the same either way.

Which decoupling pattern should you pick?

It depends on whether the caller needs an answer, whether the consumer can keep up, and whether the steps have an order. Those three questions get you to the right service almost every time.

A few notes the table cannot hold:

Asynchronous direct invoke (InvocationType: "Event") is the cheapest change you can make, and it is underrated. The caller gets a 202 immediately and stops being billed. Lambda queues the event and retries function errors twice by default. The catch is that events can sit in that queue for hours under load, and if you do not attach a dead-letter queue, a failure past the retries vanishes silently. Use it for genuine fire-and-forget, not for work you need to account for.

SQS is the right answer when the shapes of producer and consumer differ. AWS recommends putting a queue between functions so it can "durably persist messages when downstream processing is slower." If your invoice service handles 500 requests per second and your payment provider handles 50, no amount of Lambda concurrency fixes that. A queue does.

SNS is for one event with several unrelated consumers. If you find yourself calling three functions in sequence purely because all three need to know something happened, they should be subscribers, not links in a chain.

Step Functions is what AWS points at for anything with real workflow shape: multiple failure types, retry logic, branching, compensation. AWS frames the split cleanly, and it is the whole value proposition: Step Functions owns the sequencing, the error handling, and the retries, while the Lambda functions hold only business logic. Retries and backoff become per-state configuration instead of code you maintain in five places.

How do you migrate a nested chain to Step Functions?

You invert the control flow. Each function stops knowing about the next one and just returns its result, and the state machine owns the sequence.

Start by deleting the invoke calls. In the nested version, createOrder looks like this:

def handler(event, context):
    order = save_order(event)
 
    # This is the line that costs you money.
    invoice = lambda_client.invoke(
        FunctionName="createInvoice",
        InvocationType="RequestResponse",
        Payload=json.dumps(order),
    )
    return json.loads(invoice["Payload"].read())

In the Step Functions version it looks like this:

def handler(event, context):
    return save_order(event)

That is the whole change to the function. The sequence moves into the state machine definition, where retries and error branches are declarative:

{
  "StartAt": "CreateOrder",
  "States": {
    "CreateOrder": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": { "FunctionName": "createOrder", "Payload.$": "$" },
      "ResultSelector": { "order.$": "$.Payload" },
      "Next": "CreateInvoice"
    },
    "CreateInvoice": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": { "FunctionName": "createInvoice", "Payload.$": "$.order" },
      "ResultSelector": { "invoice.$": "$.Payload" },
      "Next": "TakePayment"
    },
    "TakePayment": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": { "FunctionName": "takePayment", "Payload.$": "$.invoice" },
      "Retry": [
        {
          "ErrorEquals": ["PaymentProviderTimeout"],
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "BackoffRate": 2.0
        }
      ],
      "Catch": [
        { "ErrorEquals": ["States.ALL"], "Next": "RefundOrder" }
      ],
      "End": true
    }
  }
}

Three things got better and none of them required rewriting business logic. Each function is billed only while it runs. The retry policy on the payment step is visible in one place instead of implied by SDK defaults across three services. And the failure path is a state with a name, so when it fires you see it in the execution graph rather than reconstructing it from three sets of CloudWatch logs.

Migrate incrementally. Pick the chain with the widest gap between total billed duration and useful work, convert that one, and compare the bill before touching the rest.

Is the fix worth the added service?

For anything with more than one hop, yes, and the cost argument usually settles it before the architecture argument gets a hearing.

The objection I hear is that Step Functions adds a service, a deployment artifact, and state transition charges. All true. What that framing misses is that you were already paying for orchestration, just in the worst possible currency: idle Lambda duration, tripled concurrency, and retry logic scattered across functions that do not know about each other.

The deeper problem with nested invokes is not the money. It is that lambda.invoke() looks exactly like a function call while behaving nothing like one. A function call does not bill you for waiting, does not silently retry three times, and does not leave the callee running after the caller gives up. We reached for the familiar shape because serverless let us, and then spent years discovering the shape was lying to us.

Sum the billed duration across one of your chains this week. If the ratio of useful work to total spend looks anything like 640 over 2,240, you have found your next refactor.

For the source material, see the AWS Compute Blog on anti-patterns in event-driven architectures, the Lambda quotas reference, Lambda retry behavior, and invoking Lambda with Step Functions.

Keep Reading

Frequently Asked Questions

Is Lambda calling Lambda always an anti-pattern?

No, but it is wrong by default. A direct synchronous invoke is defensible when you genuinely need the return value inline and the callee is fast and stable. It becomes an anti-pattern when it is used as general-purpose glue between steps of a workflow, because the caller is billed for the entire time it sits blocked.

Why does a nested Lambda invoke cost more?

AWS bills Lambda by execution duration, and a function blocked on a synchronous child invoke is still running. In a three-function chain where only the innermost function does real work, you pay for all three concurrently. The two parent functions contribute nothing but still accrue billed milliseconds.

What should I use instead of Lambda calling Lambda?

Step Functions for ordered steps with branching and retries, SQS when the consumer is slower than the producer, SNS for fan-out to several consumers, and EventBridge for content-based routing across services. AWS recommends Step Functions specifically to avoid writing custom orchestration code inside a function.

Does Lambda retry a failed synchronous invoke?

No. Lambda does not automatically retry function errors on direct synchronous invocation. Any retry comes from your AWS SDK client configuration, which is why nested chains can multiply attempts. Asynchronous invocation is different: Lambda retries function errors twice by default.

Rabinarayan Patra - Software Development Engineer

Rabinarayan Patra

SDE II at Amazon. Previously at ThoughtClan Technologies building systems that processed 700M+ daily transactions. I write about Java, Spring Boot, microservices, and the things I figure out along the way. More about me →

X (Twitter)LinkedIn

Stay in the loop

Get the latest articles on system design, frontend and backend development, and emerging tech trends, straight to your inbox. No spam.