Server: remote Lambda tasks

A remote task runs in another Lambda function that you already have (or want to deploy and version separately). The runner invokes it synchronously. Use it when the task needs different dependencies, memory, timeout or permissions than the runner, or when the code belongs to another team.

The contract of the remote function

  • The runner sends the event {"args": [...], "kwargs": {...}}.

  • If the function returns a JSON object with a body key, body is the result of the task as it is: it is not decoded, so return a number or an object, not a JSON string (json.dumps(53) would reach the client as the string "53"). Otherwise the whole returned value is the result.

  • If the function raises, Lambda reports a function error: the task is stored as FAILURE and it is not retried, because the function already ran.

handler.py of the remote function remote-sum
"""A remote task: an ordinary Lambda function the runner invokes.

The runner calls it synchronously with ``{"args": [...], "kwargs": {...}}``. If
the response is a JSON object with a ``body`` key, ``body`` becomes the result of
the task *as it is* (it is not decoded, so return a number or an object, not a JSON
string); otherwise the whole response is the result.
"""


def lambda_handler(event: dict, context: object) -> dict:
    args = event.get("args", [])
    return {"statusCode": 200, "body": sum(args)}

Deploy the remote function

Any Lambda packaging works (zip or image). With a zip:

zip function.zip handler.py
aws lambda create-function --function-name remote-sum --region eu-central-1 \
  --runtime python3.12 --handler handler.lambda_handler \
  --role arn:aws:iam::123456789012:role/remote-sum-role \
  --zip-file fileb://function.zip

The role of remote-sum only needs the basic execution permissions (AWSLambdaBasicExecutionRole).

Route and permissions

Point the task name to the function ARN in TASKS:

{"remote.sum": "lambda://arn:aws:lambda:eu-central-1:123456789012:function:remote-sum"}

and allow the runner role to invoke it (see IAM roles and permissions):

{
  "Effect": "Allow",
  "Action": "lambda:InvokeFunction",
  "Resource": "arn:aws:lambda:eu-central-1:123456789012:function:remote-sum"
}

Timeouts and retries

  • A remote function can run at most 15 minutes (the Lambda limit), and the runner waits for it, so the runner’s own timeout (also 900 s at most) must be larger than the remote function’s. A remote task therefore has less than 15 minutes in practice; see the execution time limit.

  • LAMBDA_INVOKE_TIMEOUT (default 840 s) is how long the runner waits for the answer. Keep it below the runner’s timeout so a slow remote function is recorded as FAILURE instead of the runner being killed: if you set the runner’s timeout below 840 s, lower this value too.

  • Invocation problems (throttling, network) are retried up to MAX_ATTEMPTS times.

  • After CB_FAIL_MAX consecutive invocation failures the circuit of that function opens for CB_RESET_TIMEOUT seconds and calls fail fast, so one broken function does not consume the whole runner.

The client side of this example is a normal task declaration (see Client: sending a task); nothing on the client shows that the task is remote:

remote.py
"""Call a task that runs on an existing Lambda function (lambda:// route)."""

from celery_app import remote_sum

result = remote_sum.apply_async(args=(22, 31))
print(result.get(timeout=60))  # 53