Skip to content

awslabs/swift-aws-lambda-runtime

Swift AWS Lambda Runtime

Develop and deploy AWS Lambda functions written in Swift.

The Swift AWS Lambda Runtime is an implementation of the AWS Lambda Runtime API. It uses an embedded asynchronous HTTP client based on SwiftNIO and provides a layered API for building a range of Lambda functions, from simple closures to complex, performance-sensitive event handlers.

Documentation

The full documentation lives on the Swift Package Index.

New to Swift on Lambda? Start with the step-by-step tutorial. It walks you through writing, building, testing, and deploying your first function.

The documentation also covers:

TL;DR getting started

You need the Swift 6.x toolchain, Docker (or Apple container) to build for Amazon Linux, and the AWS CLI configured with aws configure. On macOS, use macOS 15 (Sequoia) or later.

  1. Create an executable project and add the runtime as a dependency.
mkdir MyLambda && cd MyLambda
swift package init --type executable --name MyLambda
swift package add-dependency https://github.com/awslabs/swift-aws-lambda-runtime.git --from 2.0.0
swift package add-target-dependency AWSLambdaRuntime MyLambda --package swift-aws-lambda-runtime
  1. Scaffold a minimal function. The runtime ships a plugin that generates a starting point in Sources/MyLambda/MyLambda.swift.
swift package lambda-init --allow-writing-to-package-directory

The generated function receives a JSON event and returns a JSON response.

import AWSLambdaRuntime

struct HelloRequest: Decodable {
    let name: String
    let age: Int
}

struct HelloResponse: Encodable {
    let greetings: String
}

let runtime = LambdaRuntime {
    (event: HelloRequest, context: LambdaContext) in
    HelloResponse(
        greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age."
    )
}

try await runtime.run()
  1. Test it locally. swift run starts a local server on port 7000.
swift run &
curl --header "Content-Type: application/json" \
     --data '{"name":"World","age":30}' \
     http://127.0.0.1:7000/invoke
  1. Build and package for Amazon Linux, then deploy.
swift package --allow-network-connections docker lambda-build
swift package --allow-network-connections all:443 lambda-deploy
  1. Invoke your deployed function.
aws lambda invoke \
  --function-name MyLambda \
  --payload $(echo '{"name":"World","age":30}' | base64) \
  /dev/stdout

For the full walkthrough, see the getting started guide and the tutorial.

Examples

The Examples directory contains complete, runnable functions covering JSON, API Gateway, streaming, background tasks, Service Lifecycle with PostgreSQL, and more.

Status

The Swift runtime client is incubating as part of the Swift Server Workgroup incubation process. It is an experimental package, subject to change, and intended only for evaluation purposes.

Open issues on GitHub for support requests.