UseLambdaRun

Offloads synchronous run execution to an AWS Lambda function via direct SDK invocation instead of executing in-process. The call blocks until the Lambda completes and returns the train output. No public endpoint is created; access is governed by IAM policies.

Package

dotnet add package Trax.Scheduler.Lambda

Signature

public static SchedulerConfigurationBuilder UseLambdaRun(
    this SchedulerConfigurationBuilder builder,
    Action<LambdaRunOptions> configure
)

Defined in Trax.Scheduler.Lambda.Extensions.LambdaSchedulerExtensions.

Parameters

ParameterTypeRequiredDescription
configureAction<LambdaRunOptions>YesCallback to set the Lambda function name and client options

Returns

SchedulerConfigurationBuilder, for continued fluent chaining.

LambdaRunOptions

PropertyTypeDefaultDescription
FunctionNamestring(required)The Lambda function name, ARN, or partial ARN to invoke
ConfigureLambdaClientAction<AmazonLambdaConfig>?nullOptional callback to configure the AmazonLambdaConfig (set region, endpoint override for LocalStack, etc.)
SigningKeybyte[]?nullThe key shared with the function's ConfigureRunner, at least 32 bytes. When set, each envelope carries a Signature over its PayloadJson. See Authorization Posture

Examples

Basic Usage

using Trax.Scheduler.Lambda.Extensions;
 
services.AddTrax(trax => trax
    .AddEffects(effects => effects
        .UsePostgres(connectionString)
    )
    .AddMediator(assemblies)
    .AddScheduler(scheduler => scheduler
        .UseLambdaWorkers(
            lambda => lambda.FunctionName = "content-shield-runner",
            routing => routing.ForTrain<IMyTrain>()
        )
        .UseLambdaRun(lambda =>
            lambda.FunctionName = "content-shield-runner"
        )
    )
);

With Custom Region

.UseLambdaRun(lambda =>
{
    lambda.FunctionName = "content-shield-runner";
    lambda.ConfigureLambdaClient = config =>
        config.RegionEndpoint = Amazon.RegionEndpoint.EUWest1;
})

With LocalStack (Development)

.UseLambdaRun(lambda =>
{
    lambda.FunctionName = "content-shield-runner";
    lambda.ConfigureLambdaClient = config =>
        config.ServiceURL = "http://localhost:4566";
})

Registered Services

UseLambdaRun() registers:

ServiceLifetimeDescription
LambdaRunOptionsSingletonConfiguration options
IAmazonLambdaSingletonAWS Lambda client
IRunExecutorScopedReplaced with an internal implementation that dispatches run requests via the Lambda SDK and blocks until the response

Note: Without UseLambdaRun(), the default LocalRunExecutor executes trains in-process via ITrainBus.RunAsync(). UseLambdaRun() overrides this.

How It Works

When a GraphQL run* mutation is called, the IRunExecutor registered by UseLambdaRun():

  1. Serializes a RemoteRunRequest containing the train name, input JSON, and input type
  2. Wraps it in a LambdaEnvelope with Type = Run
  3. Calls IAmazonLambda.InvokeAsync() with InvocationType.RequestResponse
  4. Blocks until the Lambda completes
  5. Reads response.FunctionError and throws RemoteRunException (a TrainException) with no public message if the Lambda failed at the infrastructure level
  6. Deserializes the response payload as RemoteRunResponse
  7. On success: returns the train output to GraphQL
  8. On error (RemoteRunResponse.IsError): throws RemoteRunException with the remote error details and the runner's PublicMessage

Differences from UseRemoteRun

UseLambdaRunUseRemoteRun
TransportAWS SDK direct invocationHTTP POST
Public endpointNone (IAM-governed)Required (Function URL, API Gateway, or similar)
PackageTrax.Scheduler.LambdaTrax.Scheduler (built-in)
RetryConfigurable LambdaRetryOptionsConfigurable HttpRetryOptions
TimeoutLambda execution timeout (AWS config)RemoteRunOptions.Timeout (default 5 min)

IAM Permissions

The scheduler process needs:

{
  "Effect": "Allow",
  "Action": "lambda:InvokeFunction",
  "Resource": "arn:aws:lambda:us-east-1:123456789012:function:content-shield-runner"
}

Limitations

  • Payload size limit: Lambda response payloads are limited to 6 MB (synchronous). Train outputs exceeding this will fail.
  • Execution timeout: Lambda functions have a maximum execution time of 15 minutes. Long-running trains may time out.
  • Cancellation reaches the function through the database: a cancel sets the run's cancel flag, which the train sees at its next junction boundary when the function registers CancellationCheckProvider (via AddJunctionProgress()). A junction already running is not interrupted. See Remote Execution.

See Also