AWS Lambda Runtime API

I just wanted to give you folks a small update on swift-lambda-runtime: With the latest release I added event types for six different source events:

  • Application Load Balancer handler
  • APIGateway handler
  • Cloudwatch Scheduled Events
  • DynamoDB Streams
  • SNS Messages
  • SQS Messages

So If you want to consume a SNS message in your lambda your code would look like this:

struct SNSBody: Codable {
  let name: String
  let whatevar: String
}

func handleSNS(event: SNS.Event, ctx: Context) -> EventLoopFuture<Void> {
  do {
    let message = event.records.first!.sns
    let body: SNSBody = try message.payload()
    
    // handle your message
    
    return ctx.eventLoop.makeSucceededFuture(Void())
  }
  catch {
    return ctx.eventLoop.makeFailedFuture(error)
  }
}

// some more setup

let runtime = try LambdaRuntime.createRuntime(eventLoopGroup: group, handler: handleSNS)

You can get an idea of how to use the events by looking at my example: EventSources. In this package you can also find a template.yaml that can give you an idea on how to setup your surroundings with aws-sam.

If you have any questions please reach out: I'm @fabianfett on twitter or just open a GitHub issue.

10 Likes