Skip to content

Run your Vapor api server on AWS Lambda using the official Swift Server runtime.

License

Notifications You must be signed in to change notification settings

vapor-community/vapor-aws-lambda-runtime

Repository files navigation

vapor-aws-lambda-runtime

Swift 5.2 Vapor 4 github-actions codecov

Run your Vapor app on AWS Lambda. This package bridges the communication between swift-aws-lambda-runtime and the Vapor framework. APIGateway requests are transformed into Vapor.Requests and Vapor.Responses are written back to the APIGateway. It intents to bring the funcionality of aws-lambda-go-api-proxy to Vapor.

Status

Note: Currently this is nothing more than a proof of concept. Use at your own risk. I would like to hear feedback, if you played with this. Please open a GitHub issues for all open ends, you experience.

What I have tested:

  • Routing
  • JSON Coding
  • Cors Middleware
  • Fluent

There are probably tons of other things that we should test. I haven't done much with Vapor so far. Therefore you will need to help me list the things to test.

Examples:

If you test anything, please open a PR so that we can document the state of affairs better. A super small example would be even better. I plan to create some integration tests with the examples.

Usage

Add vapor-aws-lambda-runtime and vapor as dependencies to your project. For this open your Package.swift:

  dependencies: [
    .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "4.0.0")),
    .package(url: "https://github.com/fabianfett/vapor-aws-lambda-runtime", .upToNextMajor(from: "0.4.0")),
  ]

Add VaporLambdaRuntime as depency to your target:

  targets: [
    .target(name: "Hello", dependencies: [
      .product(name: "Vapor", package: "vapor"),
      .product(name: "VaporAWSLambdaRuntime", package: "vapor-aws-lambda-runtime")
    ]),
  ]

Create a simple Vapor app.

import Vapor
import VaporAWSLambdaRuntime

let app = Application()

struct Name: Codable {
  let name: String
}

struct Hello: Content {
  let hello: String
}

app.get("hello") { (_) -> Hello in
  Hello(hello: "world")
}

app.post("hello") { req -> Hello in
  let name = try req.content.decode(Name.self)
  return Hello(hello: name.name)
}

Next we just need to run the vapor app. To enable running in Lambda, we need to change the "serve" command. Then we can start the app by calling app.run()

app.servers.use(.lambda)

try app.run()

Contributing

Please feel welcome and encouraged to contribute to vapor-aws-lambda-runtime. The current version has a long way to go before being ready for production use and help is always welcome.

If you've found a bug, have a suggestion or need help getting started, please open an Issue or a PR. If you use this package, I'd be grateful for sharing your experience.

If you like this project, I'm excited about GitHub stars. 🤓