AWS Serverless Swift API Template
This package demostrates how to write a Scalable REST API with the Serverless stack by using only Swift as development language.
Product API Example
The example shows how to build a Rest API based on a Product
swift class.
public struct Product: Codable {
public let sku: String
public let name: String
public let description: String
public var createdAt: String?
public var updatedAt: String?
}
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 in popular tool such as PostMan.
Be sure to update the "host": "<BASE_URL>"
with the url provided during the deployment.
The full swagger-doc.html
has been generated using pretty-swag
Serverless architecture
The architecture is based on the classical AWS Serverless stack: APIGateway, Lambda and DynamoDB.
APIGateway
: acts as aproxy
for theLambda
and exposing it to the internet.Lambda
: is the computational layer.DynamoDB
: is the AWSNoSQL
database
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 usin Swift NIO.
- aws-sdk-swift: Interacts with DynamoDB
Requirements
- Install Docker
- Install Serverless Framework
- 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-sprinter/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 use:
serverless remove