Code Monkey home page Code Monkey logo

swift-lambda's Introduction

swift lambda

Build and deploy Swift Package Manager projects on AWS Lambda.

swift lambda builds Swift projects using the Swift AWS Lambda Runtime for deployment into AWS Lambda functions. It uses a Swift cross compilation toolchain for Amazon Linux to build the project right on macOS, no Docker required. Simply call swift lambda build.

A built Swift lambda can then be deployed using swift lambda deploy (using either aws lambda publish or sam deploy).

Blog article: Deploying Swift on AWS Lambda.

Note: Due to a bug in Xcode 11's Swift 5.2.4, a Swift 5.3 install (e.g. Xcode beta 12) is currently required.

Installation

First make sure swift --version shows a 5.3 release, it is currently required.

swift lambda is easiest to install using Homebrew, get it over here.

This single call installs swift lambda and all its dependencies:

$ brew install SPMDestinations/tap/swift-lambda

It's a pretty big download at over 1GB (binary host & target Swift toolchains from Swift.org and the AWS CLI).

Usage

A simple Swift Lambda using Macro.

Setup the Swift package:

mkdir HelloWorld && cd HelloWorld
swift package init --type executable
open Package.swift # opens Xcode

Configure the Package.swift to look like this:

// swift-tools-version:5.2
import PackageDescription

let package = Package(
  name         : "HelloWorld",
  platforms    : [ .macOS(.v10_13) ], // <== add this
  dependencies : [ // and add this dependency โ†“
    .package(url: "https://github.com/Macro-swift/MacroLambda.git",
             from: "0.1.3"),
    .package(url: "https://github.com/AlwaysRightInstitute/cows.git",
             from: "1.0.0") // optional but fun
  ],
  targets: [
    .target(name: "HelloWorld", 
            dependencies: [ "MacroLambda", "cows" ])
  ]
)

Fill in the Lambda's code in the main.swift:

import MacroLambda

let app = Express()
app.use(bodyParser.text())

app.post("/hello") { req, res, next in
  res.send("Client sent: \(req.body.text ?? "~nothing~")")
}

app.get { req, res, next in
  res.send("Welcome to Macro!\n")
}

if process.isRunningInLambda {
  Lambda.run(app)
}
else {
  app.listen(1337) {
    console.log("server running on http://localhost:1337/")
  }
}

Build the package using swift lambda build -d 5.2 and deploy it to AWS by calling swift lambda deploy -d 5.2. It expects a Lambda configuration called HelloWorld (select a different function using the -f argument).

The -d 5.2 is necessary until AmazonLinux 5.3 development toolchains are available.

A more complex example: express-simple-lambda, it looks like this:

Usage: swift lambda build

$ swift lambda build -h
Unknown argument: -h

usage: swift lambda build [mode] [options]

Modes:
  -c, --configuration debug|release [default: debug]
  --clean build|dist                [default: build]

Product:
  -p, --product <product>           [default: directory name]
  -d, --destination <dest>

Options:
  -v, --verbose
  -s, --silent
  --static
  --static-libs <libs>

Usage: swift lambda deploy

$ swift lambda deploy -h
usage: swift lambda deploy [options]

  -f, --function <name>
  -p, --product  <product>
  -t, --template <SAM template>     (optional)
  --stack-name   <SAM stackname>    (optional)

If no function/product name is provided, the current directory
will be used.

Build Options:
  --skip-build                      (do not invoke swift lambda build)
  -c, --configuration debug|release [default: debug]
  -d, --destination <dest>
  --static
  --static-libs <libs>

Options:
  -v, --verbose
  -s, --silent

Issues & Caveats

  • Swift 5.3 required on host (SR-13312)
  • There is no "direct" Xcode support yet, i.e. the package needs to be created using a Package.swift (vs create a new tool project in Xcode). We might address that and even provide a SwiftXcode style Lambda Xcode template. Direct deployment from within Xcode should be possible. (Issue 2)
  • C++ support seems b0rked right now. Regular C code compile just fine (e.g. SwiftNIO includes some). (SPMDestinations Issue #4)
  • swift lambda build has some support for static linking already builtin, but it doesn't fully work yet. To be fixed. (static linking reduces the size of the zip and further improves Lambda startup time). (Issue 3)
  • There is no Swift 5.3 toolchain for Amazon Linux yet (just 5.2). (SPMDestinations Issue #5)
  • The X toolchains only have the packages that seemed necessary. If someone needs additionals ones please file an issue (the toolchains can carry lots of dev packages, that's perfectly fine).

Links

Who

swift-lambda is brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.