Skip to main content

make_lambda_runtime

Macro make_lambda_runtime 

Source
macro_rules! make_lambda_runtime {
    (
        internal
        $handler:path,
        telemetry_init = $telemetry_init:path,
        trigger = $trigger:expr
    ) => { ... };
    (
        internal
        $handler:path,
        telemetry_init = $telemetry_init:path,
        trigger = $trigger:expr
        $(, $name:ident() -> $client:ty)+
    ) => { ... };
    (
        internal
        $handler:path,
        telemetry_init = $telemetry_init:path,
        trigger = $trigger:expr ;
        $(with_code $($code:tt)+)?
    ) => { ... };
    (
        $handler:path,
        trigger = $trigger:expr,
        telemetry_init = $telemetry_init:path
        $(, $name:ident() -> $client:ty)*
    ) => { ... };
    (
        $handler:path,
        telemetry_init = $telemetry_init:path,
        trigger = $trigger:expr
        $(, $name:ident() -> $client:ty)*
    ) => { ... };
    (
        $handler:path,
        trigger = $trigger:expr
        $(, $name:ident() -> $client:ty)*
    ) => { ... };
    (
        $handler:path,
        telemetry_init = $telemetry_init:path
        $(, $name:ident() -> $client:ty)*
    ) => { ... };
    ($handler:path $(, $name:ident() -> $client:ty)*) => { ... };
}
Expand description

Generates a complete #[tokio::main] async fn main() for a Lambda function.

This macro wires together telemetry initialisation, optional AWS SDK client singletons, and the Lambda runtime with the DefaultTracingLayer applied. It is the recommended entry point for Lambda functions using this crate.

§Syntax

make_lambda_runtime!(
    handler_fn
    [, trigger = OTelFaasTrigger::Variant]
    [, telemetry_init = my_telemetry_init_fn]
    [, client_fn() -> SdkClientType]*
);

All parameters after handler_fn are optional and can appear in any order:

  • handler_fn (required) — path to the async handler function.
  • trigger — the OTelFaasTrigger variant for the faas.trigger attribute. Defaults to OTelFaasTrigger::Http.
  • telemetry_init — a custom telemetry init function with signature fn() -> SdkTracerProvider. Defaults to default_telemetry_init.
  • client_fn() -> SdkClientType — zero or more SDK client declarations. Each generates a OnceLock-backed accessor with DefaultInterceptor pre-attached.

§Prerequisites

tokio must be a direct dependency of your crate. Lambda functions in Rust need tokio anyway.

§Examples

Minimal usage — just the handler:

use awssdk_instrumentation::lambda::{LambdaError, LambdaEvent};
use serde_json::Value;

async fn handler(event: LambdaEvent<Value>) -> Result<Value, LambdaError> {
    Ok(event.payload)
}

awssdk_instrumentation::make_lambda_runtime!(handler);

With a DynamoDB client and a datasource trigger:

use awssdk_instrumentation::lambda::{LambdaError, LambdaEvent, OTelFaasTrigger};
use serde_json::Value;

async fn handler(event: LambdaEvent<Value>) -> Result<Value, LambdaError> {
    let _client = dynamodb_client();
    Ok(event.payload)
}

awssdk_instrumentation::make_lambda_runtime!(
    handler,
    trigger = OTelFaasTrigger::Datasource,
    dynamodb_client() -> aws_sdk_dynamodb::Client
);