choefele / swift-lambda-app

Amazon Lambda app with Swift and Docker
MIT License
161 stars 20 forks source link
aws-lambda swift swift-server

Swift + Amazon Lambda

Swift Lambda App

Template to build an Amazon Lambda app in Swift

Build Status

Overview

This repo contains code and scripts to quickly get you started with writing Swift apps for AWS Lambda, Amazon's serverless computing platform. It contains:

swift-lambda-app has been inspired by SwiftOnLambda, which provided the initial working code to execute Swift programs on Lambda.

Also see the article Serverless Swift for a detailed introduction to running Swift on Lambda.

Using this Repo as a Template

Tools: Serverless (optional)

Simply fork this repo to start developing your own Lambda function in Swift. Alternatively, you can use Serverless to make a copy of this repo for you by using serverless install --url https://github.com/choefele/swift-lambda-app

Development

Tools: Xcode, ngrok (optional)

The sample app in this repo uses a standard Swift Package Manager directory layout and package file thus swift build, swift test and swift package generate-xcodeproj work as expected. Check out the SPM documentation for more info.

There are three targets:

For development, I recommend a TDD approach against the library target because this results in the quickest turnaround for code changes. Uploading to Lambda to quickly verify changes isn't really an option because of slow updating times. Exposing your functionality via HTTPS as described below, however, enables you to test and debug your functionality in a slightly different way.

To run a local HTTPS server:

Now you can test the skill in the Alexa Console using the utterance "test swift". This will call your local HTTP server allowing you to modify and debug your code with the Alexa service.

Integration

Tools: Docker, Travis

Before uploading to Lambda, it's worthwhile to run the unit tests in a Linux environment and run integration tests that simulate the execution environment. This repo provides run-unit-tests.sh to do the former and run-integration-tests.sh to do the latter.

run-unit-tests.sh builds and tests the Lambda target inside a Swift Docker container based on Ubuntu because there's currently no Swift compiler for Amazon Linux (based on RHEL). Executables built on different Linux distributions are compatible with each other if you provide all dependencies necessary to run the program. For this reason, the script captures all shared libraries required to run the executable using ldd.

To prove that the resulting package works, run-integration-tests.sh runs a release build of the Swift code inside a Docker container that comes close to Lambda’s execution environment (unfortunately, Amazon only provides a few Docker images that don't necessarily match what Lambda is using).

The integration with Lambda is done via a small Node.js script that uses the child_process module to run the Swift executable. The script follows Amazon's recommendations to run arbitrary executables in AWS Lambda.

After configuring Travis, you can run the same integration script also for every commit.

Deploying the Lambda Function

Deployment Using the Serverless Framework

Tools: Serverless

This project contains a serverless.yml configuration file for the Serverless Framework, which automates uploading and configuring the Lambda function:

To verify that the Lambda function works, run sls invoke -f alexaSkill -p session_start.json

Manual Deployment

To deploy your code to Lambda manually:

To verify that the Lambda function works, condigure a test event with the contents of session_start.json in the AWS console.

Creating an Alexa Skill

After creating the Lambda function, you can now create an Alexa skill:

Now you can test the skill in the Alexa Console using the utterance "test swift". More details on configuring Alexa skills can be found on Amazon's developer portal.