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
| Parameter | Type | Required | Description |
|---|---|---|---|
configure | Action<LambdaRunOptions> | Yes | Callback to set the Lambda function name and client options |
Returns
SchedulerConfigurationBuilder, for continued fluent chaining.
LambdaRunOptions
| Property | Type | Default | Description |
|---|---|---|---|
FunctionName | string | (required) | The Lambda function name, ARN, or partial ARN to invoke |
ConfigureLambdaClient | Action<AmazonLambdaConfig>? | null | Optional callback to configure the AmazonLambdaConfig (set region, endpoint override for LocalStack, etc.) |
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:
| Service | Lifetime | Description |
|---|---|---|
LambdaRunOptions | Singleton | Configuration options |
IAmazonLambda | Singleton | AWS Lambda client |
IRunExecutor -> LambdaRunExecutor | Scoped | Dispatches run requests via Lambda SDK, blocks until response |
Note: Without
UseLambdaRun(), the defaultLocalRunExecutorexecutes trains in-process viaITrainBus.RunAsync().UseLambdaRun()overrides this.
How It Works
When a GraphQL run* mutation is called, the LambdaRunExecutor:
- Serializes a
RemoteRunRequestcontaining the train name, input JSON, and input type - Wraps it in a
LambdaEnvelopewithType = Run - Calls
IAmazonLambda.InvokeAsync()withInvocationType.RequestResponse - Blocks until the Lambda completes
- Reads
response.FunctionErrorand throwsTrainExceptionif the Lambda failed at the infrastructure level - Deserializes the response payload as
RemoteRunResponse - On success: returns the train output to GraphQL
- On error (
RemoteRunResponse.IsError): throwsTrainExceptionwith the remote error details
Differences from UseRemoteRun
| UseLambdaRun | UseRemoteRun | |
|---|---|---|
| Transport | AWS SDK direct invocation | HTTP POST |
| Public endpoint | None (IAM-governed) | Required (Function URL, API Gateway, or similar) |
| Package | Trax.Scheduler.Lambda | Trax.Scheduler (built-in) |
| Retry | Configurable LambdaRetryOptions | Configurable HttpRetryOptions |
| Timeout | Lambda 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 is process-local: Same limitation as other remote execution models.
See Also
- Remote Execution: architecture overview and deployment models
- UseLambdaWorkers: dispatch queued trains to Lambda
- TraxLambdaFunction: the Lambda receiver base class
- UseRemoteRun: HTTP-based remote run execution (alternative transport)