glambda

This module provides types and adapters to write AWS Lambda functions that target the Node runtime.

The core type of glambda is the Handler(event, result), which is simply a function, fn(event, Context) -> Promise(result). Then you use one of the handler adapters to wrap your handler to make it compatible with AWS Lambda.

The adapter creates a new function, fn(JsEvent, JsContext) -> Promise(JsResult). You shouldn’t need to use these types in your code. In fact, you typically do not need to reference them at all due to type inference.

Examples

You can handle API Gateway proxy v2 events using Gleam HTTP types

fn handle_request(
  request: Request(Option(String)),
  ctx: Context,
) -> Promise(Response(Option(String))) {
  Response(200, [], None)
  |> promise.resolve
}

// this is the actual function lambda will invoke
pub fn handler(event, ctx) {
  glambda.http_handler(handle_request)(event, ctx)
}

You can also handle the API Gateway event directly, or several other supported event types, such as EventBridge.

fn handle_request(event: EventBridgeEvent, ctx: Context) -> Promise(Nil) {
  io.debug(event)
  promise.resolve(Nil)
}

// this is the actual function lambda will invoke
pub fn handler(event, ctx) {
  glambda.eventbridge_event_handler(handle_request)(event, ctx)
}

Supported Events

Reference the *_handler functions for the correct signatures.

Types

pub type ApiGatewayEventClientCertificate {
  ApiGatewayEventClientCertificate(
    client_cert_pem: String,
    issuer_dn: String,
    serial_number: String,
    subject_dn: String,
    validity: ApiGatewayEventValidity,
  )
}

Constructors

  • ApiGatewayEventClientCertificate(
      client_cert_pem: String,
      issuer_dn: String,
      serial_number: String,
      subject_dn: String,
      validity: ApiGatewayEventValidity,
    )
pub type ApiGatewayEventRequestContextAuthentication {
  ApiGatewayEventRequestContextAuthentication(
    client_cert: ApiGatewayEventClientCertificate,
  )
}

Constructors

  • ApiGatewayEventRequestContextAuthentication(
      client_cert: ApiGatewayEventClientCertificate,
    )
pub type ApiGatewayEventRequestContextHttp {
  ApiGatewayEventRequestContextHttp(
    method: String,
    path: String,
    protocol: String,
    source_ip: String,
    user_agent: String,
  )
}

Constructors

  • ApiGatewayEventRequestContextHttp(
      method: String,
      path: String,
      protocol: String,
      source_ip: String,
      user_agent: String,
    )
pub type ApiGatewayEventRequestContextIamAuthorizer {
  ApiGatewayEventRequestContextIamAuthorizer(
    access_key: String,
    account_id: String,
    caller_id: String,
    principal_org_id: String,
    user_arn: String,
    user_id: String,
  )
}

Constructors

  • ApiGatewayEventRequestContextIamAuthorizer(
      access_key: String,
      account_id: String,
      caller_id: String,
      principal_org_id: String,
      user_arn: String,
      user_id: String,
    )
pub type ApiGatewayEventRequestContextJwtAuthorizer {
  ApiGatewayEventRequestContextJwtAuthorizer(
    claims: Dict(String, String),
    scopes: Option(List(String)),
  )
}

Constructors

  • ApiGatewayEventRequestContextJwtAuthorizer(
      claims: Dict(String, String),
      scopes: Option(List(String)),
    )
pub type ApiGatewayEventValidity {
  ApiGatewayEventValidity(not_after: String, not_before: String)
}

Constructors

  • ApiGatewayEventValidity(not_after: String, not_before: String)
pub type ApiGatewayProxyEventV2 {
  ApiGatewayProxyEventV2(
    version: String,
    route_key: String,
    raw_path: String,
    raw_query_string: String,
    cookies: Option(List(String)),
    headers: Dict(String, String),
    query_string_parameters: Option(Dict(String, String)),
    path_parameters: Option(Dict(String, String)),
    stage_variables: Option(Dict(String, String)),
    request_context: ApiGatewayRequestContextV2,
    body: Option(String),
    is_base64_encoded: Bool,
  )
}

Constructors

  • ApiGatewayProxyEventV2(
      version: String,
      route_key: String,
      raw_path: String,
      raw_query_string: String,
      cookies: Option(List(String)),
      headers: Dict(String, String),
      query_string_parameters: Option(Dict(String, String)),
      path_parameters: Option(Dict(String, String)),
      stage_variables: Option(Dict(String, String)),
      request_context: ApiGatewayRequestContextV2,
      body: Option(String),
      is_base64_encoded: Bool,
    )
pub type ApiGatewayProxyResultV2 {
  ApiGatewayProxyResultV2(
    status_code: Int,
    headers: Dict(String, String),
    body: Option(String),
    is_base64_encoded: Bool,
    cookies: List(String),
  )
}

Constructors

  • ApiGatewayProxyResultV2(
      status_code: Int,
      headers: Dict(String, String),
      body: Option(String),
      is_base64_encoded: Bool,
      cookies: List(String),
    )
pub type ApiGatewayRequestContextAuthorizer {
  Iam(iam: ApiGatewayEventRequestContextIamAuthorizer)
  Jwt(
    principal_id: String,
    integration_latency: Int,
    jwt: ApiGatewayEventRequestContextJwtAuthorizer,
  )
  Lambda(lambda: Dynamic)
}

Constructors

  • Iam(iam: ApiGatewayEventRequestContextIamAuthorizer)
  • Jwt(
      principal_id: String,
      integration_latency: Int,
      jwt: ApiGatewayEventRequestContextJwtAuthorizer,
    )
  • Lambda(lambda: Dynamic)
pub type ApiGatewayRequestContextV2 {
  ApiGatewayRequestContextV2(
    route_key: String,
    account_id: String,
    stage: String,
    request_id: String,
    authorizer: Option(ApiGatewayRequestContextAuthorizer),
    api_id: String,
    domain_name: String,
    domain_prefix: String,
    time: String,
    time_epoch: Int,
    http: ApiGatewayEventRequestContextHttp,
    authentication: Option(
      ApiGatewayEventRequestContextAuthentication,
    ),
  )
}

Constructors

  • ApiGatewayRequestContextV2(
      route_key: String,
      account_id: String,
      stage: String,
      request_id: String,
      authorizer: Option(ApiGatewayRequestContextAuthorizer),
      api_id: String,
      domain_name: String,
      domain_prefix: String,
      time: String,
      time_epoch: Int,
      http: ApiGatewayEventRequestContextHttp,
      authentication: Option(
        ApiGatewayEventRequestContextAuthentication,
      ),
    )
pub type ClientContext {
  ClientContext(
    client: ClientContextClient,
    custom: Option(Dynamic),
    env: ClientContextEnv,
  )
}

Constructors

  • ClientContext(
      client: ClientContextClient,
      custom: Option(Dynamic),
      env: ClientContextEnv,
    )
pub type ClientContextClient {
  ClientContextClient(
    installation_id: String,
    app_title: String,
    app_version_name: String,
    app_package_name: String,
  )
}

Constructors

  • ClientContextClient(
      installation_id: String,
      app_title: String,
      app_version_name: String,
      app_package_name: String,
    )
pub type ClientContextEnv {
  ClientContextEnv(
    platform_version: String,
    platform: String,
    make: String,
    model: String,
    locale: String,
  )
}

Constructors

  • ClientContextEnv(
      platform_version: String,
      platform: String,
      make: String,
      model: String,
      locale: String,
    )
pub type CognitoIdentity {
  CognitoIdentity(
    cognito_identity_id: String,
    cognito_identity_pool_id: String,
  )
}

Constructors

  • CognitoIdentity(
      cognito_identity_id: String,
      cognito_identity_pool_id: String,
    )
pub type Context {
  Context(
    callback_waits_for_empty_event_loop: Bool,
    function_name: String,
    function_version: String,
    invoked_function_arn: String,
    memory_limit_in_mb: String,
    aws_request_id: String,
    log_group_name: String,
    log_stream_name: String,
    identity: Option(CognitoIdentity),
    client_context: Option(ClientContext),
  )
}

Constructors

  • Context(
      callback_waits_for_empty_event_loop: Bool,
      function_name: String,
      function_version: String,
      invoked_function_arn: String,
      memory_limit_in_mb: String,
      aws_request_id: String,
      log_group_name: String,
      log_stream_name: String,
      identity: Option(CognitoIdentity),
      client_context: Option(ClientContext),
    )
pub type EventBridgeEvent {
  EventBridgeEvent(
    id: String,
    version: String,
    account: String,
    time: String,
    region: String,
    resources: List(String),
    source: String,
    detail: Dynamic,
  )
}

Constructors

  • EventBridgeEvent(
      id: String,
      version: String,
      account: String,
      time: String,
      region: String,
      resources: List(String),
      source: String,
      detail: Dynamic,
    )

Alias for a strongly typed lambda function

pub type Handler(event, result) =
  fn(event, Context) -> Promise(result)

Represents the raw JavaScript context for the lambda.

pub type JsContext

Represents the raw JavaScript event that invoked the lambda.

pub type JsEvent

Represents the raw Javascript handler for the lambda.

pub type JsHandler =
  fn(JsEvent, JsContext) -> Promise(JsResult)

Represents the raw JavaScript response for the lambda.

pub type JsResult {
  JsResult
  Void
}

Constructors

  • JsResult
  • Void
pub type SqsBatchItemFailure {
  SqsBatchItemFailure(item_identifier: String)
}

Constructors

  • SqsBatchItemFailure(item_identifier: String)
pub type SqsBatchResponse {
  SqsBatchResponse(
    batch_item_failures: List(SqsBatchItemFailure),
  )
}

Constructors

  • SqsBatchResponse(batch_item_failures: List(SqsBatchItemFailure))
pub type SqsEvent {
  SqsEvent(records: List(SqsRecord))
}

Constructors

  • SqsEvent(records: List(SqsRecord))
pub type SqsMessageAttribute {
  SqsMessageAttribute(
    string_value: Option(String),
    binary_value: Option(String),
    string_list_values: Option(List(String)),
    binary_list_values: Option(List(String)),
    data_type: String,
  )
}

Constructors

  • SqsMessageAttribute(
      string_value: Option(String),
      binary_value: Option(String),
      string_list_values: Option(List(String)),
      binary_list_values: Option(List(String)),
      data_type: String,
    )
pub type SqsRecord {
  SqsRecord(
    message_id: String,
    receipt_handle: String,
    body: String,
    attributes: SqsRecordAttributes,
    message_attributes: Dict(String, SqsMessageAttribute),
    md5_of_body: String,
    event_source: String,
    event_source_arn: String,
    aws_region: String,
  )
}

Constructors

  • SqsRecord(
      message_id: String,
      receipt_handle: String,
      body: String,
      attributes: SqsRecordAttributes,
      message_attributes: Dict(String, SqsMessageAttribute),
      md5_of_body: String,
      event_source: String,
      event_source_arn: String,
      aws_region: String,
    )
pub type SqsRecordAttributes {
  SqsRecordAttributes(
    aws_trace_header: Option(String),
    approximate_receive_count: String,
    sent_timestamp: String,
    sender_id: String,
    approximate_first_receive_timestamp: String,
    sequence_number: Option(String),
    message_group_id: Option(String),
    message_deduplication_id: Option(String),
    dead_letter_queue_source_arn: Option(String),
  )
}

Constructors

  • SqsRecordAttributes(
      aws_trace_header: Option(String),
      approximate_receive_count: String,
      sent_timestamp: String,
      sender_id: String,
      approximate_first_receive_timestamp: String,
      sequence_number: Option(String),
      message_group_id: Option(String),
      message_deduplication_id: Option(String),
      dead_letter_queue_source_arn: Option(String),
    )

Functions

pub fn api_gateway_proxy_v2_handler(
  handler: fn(ApiGatewayProxyEventV2, Context) ->
    Promise(ApiGatewayProxyResultV2),
) -> fn(JsEvent, JsContext) -> Promise(JsResult)

Wraps a handler to create a lambda function to handle API Gateway proxy events

pub fn eventbridge_handler(
  handler: fn(EventBridgeEvent, Context) -> Promise(Nil),
) -> fn(JsEvent, JsContext) -> Promise(JsResult)

Wraps a handler to create a lambda function to handle EventBridge events

pub fn http_handler(
  handler: fn(Request(Option(String)), Context) ->
    Promise(Response(Option(String))),
) -> fn(JsEvent, JsContext) -> Promise(JsResult)

Wraps an HTTP handler to create a lambda function to handle API Gateway proxy events

pub fn sqs_handler(
  handler: fn(SqsEvent, Context) ->
    Promise(Option(SqsBatchResponse)),
) -> fn(JsEvent, JsContext) -> Promise(JsResult)

Wraps a handler to create a lambda function to handle SQS events

Search Document