Skip to content

ryanmiville/glambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

glambda

Write AWS Lambda functions in Gleam!

Package Version Hex Docs

Write your Lambda function as an http handler, or accept direct events as normal Gleam types.

Glambda only supports the JavaScript target. Your compiled code uses the AWS Lambda Node.js runtime.

gleam add glambda

HTTP Handler

import glambda.{type Context}
import gleam/http/request.{type Request}
import gleam/http/response.{type Response, Response}
import gleam/javascript/promise.{type Promise}
import gleam/option.{type Option, Some}

fn handle_request(
  _req: Request(Option(String)),
  ctx: Context,
) -> Promise(Response(Option(String))) {
  let json = "{\"functionName\": \"" <> ctx.function_name <> "\"}"
  Response(
    200,
    [#("content-type", "application/json; charset=utf-8")],
    Some(json),
  )
  |> promise.resolve
}

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

Event Handler

import glambda.{
  type ApiGatewayProxyEventV2, type ApiGatewayProxyResultV2, type Context,
  ApiGatewayProxyResultV2,
}
import gleam/dict
import gleam/javascript/promise.{type Promise}
import gleam/option.{Some}

fn event_handler(
  _event: ApiGatewayProxyEventV2,
  ctx: Context,
) -> Promise(ApiGatewayProxyResultV2) {
  ApiGatewayProxyResultV2(
    status_code: 200,
    headers: dict.from_list([#("content-type", "application/json")]),
    body: Some("{\"functionName\": \"" <> ctx.function_name <> "\"}"),
    is_base64_encoded: False,
    cookies: [],
  )
  |> promise.resolve
}

// This is the actual function lambda invokes
pub fn handler(event, ctx) {
  glambda.api_gateway_proxy_v2_handler(event_handler)(event, ctx)
}

Check out the examples directory for examples of each supported event type, deployable with SST.

Supported Events

  • ApiGatewayProxyEventV2 (can be handled directly or as a Request(Option(String)))
  • EventBridgeEvent
  • SqsEvent

Further documentation can be found at https://hexdocs.pm/glambda.

Development

gleam run   # Run the project
gleam test  # Run the tests

About

Write AWS Lambda functions in Gleam!

Resources

Stars

Watchers

Forks

Packages

No packages published