Skip to content

A template for deploying Lambda functions with Swift AWS Lambda Runtime

License

Notifications You must be signed in to change notification settings

pokryfka/swift-aws-lambda-template

Repository files navigation

Swift AWS Lambda Template

Build Package

An opinionated template for deploying serverless functions to AWS Lambda using Swift AWS Lambda Runtime.

Goals:

  • streamline deployment
  • promote best practices
  • improve performance

Features

Requirements

Overview

The template contains code with two AWS Lambda functions:

as well as a AWS SAM template used to deploy them using AWS SAM CLI.

Since SAM CLI does not support Swift, a Docker image is created and used to cross compile and package Swift code to run in AWS Lambda environment.

Configuration

Deploying to AWS Lambda

Build and deploy:

$ make deploy

This will:

  • build Docker image used to cross compile the code (if it does not exist)
  • cross compile and package Lambda functions and Swift Linux Runtime Lambda Layer
  • prompt to configure AWS SAM project (if samconfig.toml does not exist)
  • deploy (create or update) AWS resources defined in template.yaml including Lambda functions and layers

The HelloWorldAPI endpoint is printed after successful deployment, example: https://xxx.execute-api.us-east-1.amazonaws.com/Prod/hello/

Troubleshooting

When prompted, confirm that it is okay to deploy an endpoint without authorization, see:

HelloWorldAPIFunction may not have authorization defined, Is this okay? [y/N]: y

Testing

Unit testing

Build and run tests:

$ swift test

Invoking events (locally)

Set the LOCAL_LAMBDA_SERVER_ENABLED environment variable to true .

HelloWorldAPI

Build and run Lambda:

$ swift run HelloWorldAPI

Invoke the Lambda with curl:

$ curl --header "Content-Type: application/json" \
  --request POST --data @events/api2.json \
  http://localhost:7000/invoke

or with HTTPie:

$ http POST http://localhost:7000/invoke @events/api2.json

Developing

Code Formatting

Format code using swiftformat:

swiftformat .

Consider creating Git pre-commit hook

echo 'swiftformat --lint .' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

GitHub Actions

Note github-wiki-publish-action used by Documentation workflow requires that your repository has the following:

  • A wiki with at least one page in it
  • A secret named GH_PERSONAL_ACCESS_TOKEN with a Github personal access token with "repo" authorization