aws-serverless-swift-api-template
aws-serverless-swift-api-template copied to clipboard
A Serverless REST API template, implemented in Swift with swift-aws-lambda-runtime.
AWS Serverless Swift API Template
This package demonstrates how to write a Scalable REST API with the Serverless stack by using only Swift as a development language.
Product API based on Breeze
The example shows how to build and deploy a Rest API based on a Product swift struct using Breeze
The following code is all you need to implement the engine of a Serverless Rest API in Swift.
The Serverless Rest API implements a CRUD interface to store the Product in DynamoDB.
import Foundation
import BreezeLambdaAPI
import BreezeDynamoDBService
struct Product: Codable {
public var key: String
public let name: String
public let description: String
public var createdAt: String?
public var updatedAt: String?
enum CodingKeys: String, CodingKey {
case key = "sku"
case name
case description
case createdAt
case updatedAt
}
}
extension Product: BreezeCodable { }
BreezeLambdaAPI<Product>.main()

API Definition
The API implements the following schema:
- /Product
-> GET - List Products
-> POST - Create Products
-> PUT - Update Products
- /Product/{sku}
-> DELETE - Delete Product
-> GET - Get Product
More details of the API are described in swagger.json.
The file can be imported into popular tools such as PostMan.
Be sure to update the "host": "<BASE_URL>" with the URL provided during the deployment.
Serverless architecture
The architecture is based on the classical AWS Serverless stack: APIGateway, Lambda and DynamoDB.
APIGatewayacts as aproxyforLambdaand exposes it to the internet.Lambdais the computational layer.DynamoDBis the AWSNoSQLdatabase
Advantages:
- Pay per use
- No fixed costs
- Auto-Scaling
- DevOps
REST API Application
The application uses swift-aws-lambda-runtime as AWS Custom Lambda Runtime and acts as a presentation layer of the DynamoDB content providing a REST API.
The following frameworks are used:
- swift-aws-lambda-runtime: Implements the AWS Custom Runtime using Swift NIO.
- aws-sdk-swift: Interacts with DynamoDB
Requirements
- Install Docker
- Install Serverless Framework version 3
Framework Core: 3.25.0 (standalone)
Plugin: 6.2.2
SDK: 4.3.2
- Ensure your AWS Account has the right credentials to deploy a Serverless stack.
- Clone this repository. From the command line type:
git clone https://github.com/swift-serverless/aws-serverless-swift-api-template.git
cd aws-serverless-swift-api-template
- Ensure you can run
make:
make --version
the Makefile was developed with this version:
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i386-apple-darwin11.3.0
Build
Use the following command to build the code before using the serverless commands:
./build.sh

Deploy
Deploy the full solution to your AWS using Serverless:
./deploy.sh

After the deployment is completed, the URL of the website is provided by the Serverless framework.
Update
Rebuild the code and update the Lambda to your AWS using Serverless:
./update.sh

Remove
To remove the deployment:
./remove.sh

arm64 M1 Graviton Support
If the project is built with an M1 processor, then the AWS Lambda will be deployed on the arm64 architecture.
amd64/x86_64 Support
If the project is built with an Intel processor, then the AWS Lambda will be deployed on the x86_64 architecture.
Troubleshooting
-
The Serverless version (2.40.0) does not satisfy the "frameworkVersion" (3) in serverless.yml
If during the deployment, the console prints the following message:
Serverless Error ----------------------------------------
The Serverless version (2.40.0) does not satisfy the "frameworkVersion" (3) in serverless.yml
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment Information ---------------------------
Operating System: darwin
Node Version: 14.4.0
Framework Version: 2.40.0 (standalone)
Plugin Version: 4.5.3
SDK Version: 4.2.2
Components Version: 3.9.2
Check the version of Serverless Framework installed in your environment:
sls -v
Framework Core: 2.40.0 (standalone)
Plugin: 4.5.3
SDK: 4.2.2
Components: 3.9.2
It's recommended to upgrade to version 3 the Serverless Framework.
In case you want to use version 2 make sure to override the content of serverless.yml with the content of serverless-v2.yml.