This package demonstrates how to write a Scalable REST API with the Serverless stack by using only Swift as a development language.
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()
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.
The architecture is based on the classical AWS Serverless stack: APIGateway, Lambda and DynamoDB.
APIGateway
acts as aproxy
forLambda
and exposes it to the internet.Lambda
is the computational layer.DynamoDB
is the AWSNoSQL
database
Advantages:
- Pay per use
- No fixed costs
- Auto-Scaling
- DevOps
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
- 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
Use the following command to build the code before using the serverless commands:
./build.sh
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.
Rebuild the code and update the Lambda to your AWS using Serverless:
./update.sh
To remove the deployment:
./remove.sh
If the project is built with an M1 processor, then the AWS Lambda will be deployed on the arm64
architecture.
If the project is built with an Intel processor, then the AWS Lambda will be deployed on the x86_64
architecture.
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
.