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!
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.
The code follows the "Hexagonal Architecture" pattern, also know as "Ports and Adapters".
- 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
- AWS CLI
- Configuring your AWS account locally
- Go installation guide
- NPM/Node
- Serverless Framework installation
git clone https://github.com/lucasrosa/serverless-checkout
cd serverless-checkout
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.
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": "[email protected]",
"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.
- Order
- Checkout
- PlaceOrder
make test
make build
make deploy
sls deploy -f checkout