aws/lambda/event
Typed envelopes for the events Lambda delivers, with decoders for the common trigger sources. Lambda hands every trigger to the handler as a JSON document; these turn that document into a Gleam record.
Pair them with aws/lambda.start_json:
import aws/lambda
import aws/lambda/event
pub fn main() {
lambda.start_json(
event.sqs_decoder(),
fn(sqs, _ctx) {
list.each(sqs.records, fn(msg) { process(msg.body) })
Ok(Nil)
},
fn(_) { json.null() },
)
}
Each decoder models the fields handlers actually reach for and tolerates
missing/null members so a slightly different payload shape never crashes
decoding. Open-ended members are left for the caller to decode: an SQS
body and an SNS message are raw Strings (often JSON you parse with
your own decoder), and an EventBridge detail is decoded by a decoder
you supply. Field shapes follow the AWS sample events documented under
“Using AWS Lambda with
Types
An API Gateway REST API proxy-integration request (payload format 1.0).
request_context is omitted; the fields here cover routing, headers,
query, and body.
pub type ApiGatewayProxyRequest {
ApiGatewayProxyRequest(
resource: String,
path: String,
http_method: String,
headers: dict.Dict(String, String),
query_string_parameters: dict.Dict(String, String),
path_parameters: dict.Dict(String, String),
stage_variables: dict.Dict(String, String),
body: option.Option(String),
is_base64_encoded: Bool,
)
}
Constructors
-
ApiGatewayProxyRequest( resource: String, path: String, http_method: String, headers: dict.Dict(String, String), query_string_parameters: dict.Dict(String, String), path_parameters: dict.Dict(String, String), stage_variables: dict.Dict(String, String), body: option.Option(String), is_base64_encoded: Bool, )
An API Gateway HTTP API request (payload format 2.0). method, path,
and source_ip are flattened out of requestContext.http for
convenience.
pub type ApiGatewayV2Request {
ApiGatewayV2Request(
version: String,
route_key: String,
raw_path: String,
raw_query_string: String,
cookies: List(String),
headers: dict.Dict(String, String),
query_string_parameters: dict.Dict(String, String),
path_parameters: dict.Dict(String, String),
stage_variables: dict.Dict(String, String),
method: String,
path: String,
source_ip: String,
body: option.Option(String),
is_base64_encoded: Bool,
)
}
Constructors
-
ApiGatewayV2Request( version: String, route_key: String, raw_path: String, raw_query_string: String, cookies: List(String), headers: dict.Dict(String, String), query_string_parameters: dict.Dict(String, String), path_parameters: dict.Dict(String, String), stage_variables: dict.Dict(String, String), method: String, path: String, source_ip: String, body: option.Option(String), is_base64_encoded: Bool, )
An EventBridge (CloudWatch Events) event. The detail payload is
service-specific, so this type is generic over it: supply a decoder to
eventbridge_decoder. Use decode.dynamic (or
gleam/json’s document handling) if you want to keep it untyped.
pub type EventBridgeEvent(detail) {
EventBridgeEvent(
id: String,
version: String,
detail_type: String,
source: String,
account: String,
time: String,
region: String,
resources: List(String),
detail: detail,
)
}
Constructors
-
EventBridgeEvent( id: String, version: String, detail_type: String, source: String, account: String, time: String, region: String, resources: List(String), detail: detail, )
A single S3 notification record. The bucket and object fields are
flattened out of the nested s3 member. object_key is URL-encoded
exactly as S3 delivers it (spaces become +).
pub type S3Record {
S3Record(
aws_region: String,
event_name: String,
event_time: String,
event_source: String,
bucket_name: String,
bucket_arn: String,
object_key: String,
object_size: option.Option(Int),
object_etag: option.Option(String),
object_sequencer: option.Option(String),
)
}
Constructors
-
S3Record( aws_region: String, event_name: String, event_time: String, event_source: String, bucket_name: String, bucket_arn: String, object_key: String, object_size: option.Option(Int), object_etag: option.Option(String), object_sequencer: option.Option(String), )
A published SNS message. message is the raw payload — parse it with your
own decoder if it carries JSON.
pub type SnsMessage {
SnsMessage(
message_id: String,
topic_arn: String,
subject: option.Option(String),
message: String,
timestamp: String,
message_type: String,
)
}
Constructors
-
SnsMessage( message_id: String, topic_arn: String, subject: option.Option(String), message: String, timestamp: String, message_type: String, )
A single SNS record wrapping the published message.
pub type SnsRecord {
SnsRecord(
event_source: String,
event_subscription_arn: String,
sns: SnsMessage,
)
}
Constructors
-
SnsRecord( event_source: String, event_subscription_arn: String, sns: SnsMessage, )
An aws:sqs event: a batch of one or more messages.
pub type SqsEvent {
SqsEvent(records: List(SqsMessage))
}
Constructors
-
SqsEvent(records: List(SqsMessage))
A single SQS message. body is the raw message body — parse it with your
own decoder if it carries JSON.
pub type SqsMessage {
SqsMessage(
message_id: String,
receipt_handle: String,
body: String,
attributes: dict.Dict(String, String),
message_attributes: dict.Dict(String, SqsMessageAttribute),
md5_of_body: String,
event_source: String,
event_source_arn: String,
aws_region: String,
)
}
Constructors
-
SqsMessage( message_id: String, receipt_handle: String, body: String, attributes: dict.Dict(String, String), message_attributes: dict.Dict(String, SqsMessageAttribute), md5_of_body: String, event_source: String, event_source_arn: String, aws_region: String, )
A user-supplied SQS message attribute. Binary attributes carry data_type
"Binary" and no string_value.
pub type SqsMessageAttribute {
SqsMessageAttribute(
data_type: String,
string_value: option.Option(String),
)
}
Constructors
-
SqsMessageAttribute( data_type: String, string_value: option.Option(String), )
Values
pub fn api_gateway_decoder() -> decode.Decoder(
ApiGatewayProxyRequest,
)
Decoder for the API Gateway REST proxy request (payload format 1.0).
pub fn api_gateway_v2_decoder() -> decode.Decoder(
ApiGatewayV2Request,
)
Decoder for the API Gateway HTTP API request (payload format 2.0).
pub fn eventbridge_decoder(
detail_decoder: decode.Decoder(detail),
) -> decode.Decoder(EventBridgeEvent(detail))
Decoder for an EventBridge event, decoding detail with detail_decoder.
pub fn s3_decoder() -> decode.Decoder(S3Event)
Decoder for the S3 event-notification envelope.
pub fn sns_decoder() -> decode.Decoder(SnsEvent)
Decoder for the SNS event envelope.
pub fn sqs_decoder() -> decode.Decoder(SqsEvent)
Decoder for the SQS event envelope.