DEV Community

Matheus Cardoso
Matheus Cardoso

Posted on β€’ Originally published at getstream.io

15 13

Deploy HTTP Services Written in Swift to AWS in Seconds

Recently, Apple announced the Swift AWS Lambda Runtime. It's now possible to write self-contained functions that run on AWS using the same Swift you use for iOS development. This lets you not only reuse the knowledge you already have of Swift, but also share code between the server and client. However, everything is still a little bit complicated to set up. To help you get started faster, we've built a starting point for writing an HTTP service in Swift, which does everything you need by running a single script.

Image shows a terminal window with the deployment process of an AWS Lambda written in Swift finished

Swift Lambda contains configuration files and scripts to fully automate deploying to AWS in a matter of seconds using the Serverless Framework. It is based on samples and documentation from swift-server/swift-aws-lambda-runtime.

Getting started

  1. Clone the repository: https://github.com/GetStream/swift-lambda

  2. Install Docker

  3. Install the Serverless Framework

  4. Configure the AWS Credentials

  5. Write your code in Sources/Lambda/main.swift

  6. Deploy by running ./Scripts/deploy.sh

  7. Open the output URL in your browser.

Image shows a browser on an AWS Lambda URL showing the words

Writing code

There is some code already present in the Sources/Lambda/main.swift file. It simply outputs "Hello, world!" in plain text.

import AWSLambdaEvents
import AWSLambdaRuntime
import NIO
// MARK: - Run Lambda
Lambda.run(APIGatewayProxyLambda())
// MARK: - Handler, Request and Response
// FIXME: Use proper Event abstractions once added to AWSLambdaRuntime
struct APIGatewayProxyLambda: EventLoopLambdaHandler {
public typealias In = APIGateway.Request
public typealias Out = APIGateway.Response
public func handle(context: Lambda.Context, event: APIGateway.Request) -> EventLoopFuture<APIGateway.Response> {
context.logger.debug("hello, api gateway!")
return context.eventLoop.makeSucceededFuture(APIGateway.Response(statusCode: .ok, body: "Hello, world!"))
}
}
view raw main.swift hosted with ❀ by GitHub

If you want to output some HTML, just set the "Content-Type" header to "text/html; charset=UTF-8"

...
return context.eventLoop.makeSucceededFuture(APIGateway.Response(
statusCode: .ok,
headers: ["Content-Type": "text/html; charset=UTF-8"],
body: """
<p align="center">
<h2 align="center"> Hello, world! From Swift 5.2 πŸ’˜ </h2>
</p>
<hl/>
<p align="center">\(event.requestContext.identity.userAgent ?? "")</p>
""")
)
...
view raw main.swift hosted with ❀ by GitHub

Image shows a browser on an AWS Lambda URL showing the words

For more information on the available settings and methods, refer to the Swift AWS Lambda Runtime README

Configuring the endpoint

To change some characteristics of your HTTP endpoint, such as the method expected, you should modify the serverless.yml file. For more information on the available parameters, refer to the Serverless.yml Reference

Contributing

If you have a suggestion or bug report, please file an issue in the Swift Lambda repository. If you want to take a stab at contributing code, don't hesitate in submitting a PR. Don't forget to leave a star if you liked it :)

Top comments (0)

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay