Serverless Checkout
Serverless Checkout is an example of a Serverless Microservice that accepts orders from a client and processes them asynchronously by separating the "checkout" and "process" functions using AWS SQS.
The key features of Serverless Checkout are:
- Automatic scalability
- High availability
- Pay-per-execution
- You can create any stage (dev, test, uat, prod...) you want with a single
sls deploy -s STAGE
command
- Infrastructure-as-code, you only have to manage the serverless.yml, nothing else!
Architecture
Service Architecture
The application uses the following AWS services:
- AWS API Gateway for the REST API
- AWS Lambda for the processing
- AWS SQS to separate the 'checkout' and 'process' functions
- AWS DynamoDB to persist the order
- AWS CloudWatch to save logs
The application is created with the Serverless Framework to make it easier to manage and deploy. The entire architecture is described in the serverless.yml file.
Architecture diagram
Code Architecture
The code follows the "Hexagonal Architecture" pattern, also know as "Ports and Adapters".
How to run it
Requirements
- An AWS account
- AWS CLI
- Go 1.12.x
- Node 10.15.x (required for the Serverless Framework, which is intalled via NPM)
- Serverless Framework >=1.28.0
1. Installing the requirements
2. Clone the repository
git clone https://github.com/lucasrosa/serverless-checkout
3. Enter into the repository folder
cd serverless-checkout
4. Deploying the service
make deploy
IMPORTANT: Once you deploy the service, the Lambda attached to the SQS Queue will keep making requests behind the scene and you will be charged by "EmptyReceives". It is covered by the free tier but once you add more services you have to watch it carefully. See this and this.
Using the service
The output will give you an URL to the REST API of your service. Something like this: https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/checkout.
Now you just have to do a POST to this endpoint with the following parameters as RAW application/json in the body of the request:
{
"id": "some-unique-id",
"email": "your@email.com",
"amount": 123.00,
"currency": "brl",
"productid": 4,
"description": "Some product description",
"paymenttoken": "some-fake-payment-token"
}
This POST should return a HTTP 201, indicating that the resource was created with success. After a couple seconds you will be able to see this information pop-up in a table called "orders-STAGE" on your DynamoDB.
Code
Business Logic
Entities
Public
Testing, building and deploying the AWS Serverless adapter
Running tests
make test
How to build
make build
How to deploy
make deploy
Deploying a single function
sls deploy -f checkout