A simple example (inspired on the most basic calculator API) using:
- TypeScript + Node for lambda implementation
- TypeScript + Jest for unit testing
- API Gateway + OpenAPI/Swagger integration for API validation
- AWS SAM for infrastructure templating
- Node + NPM
- aws-cli (installed and configured)
- aws-sam-cli
There's a sh file called deploy.sh
meant to be used for infrastructure creating/updating:
sh deploy.sh <STACK_NAME> <S3_BUCKET>
Where:
STACK_NAME
is the name of the stackS3_BUCKET
refers to an existing S3 bucket where the OpenAPI definition is going to be upload
An API whose Url can be obtained on the CloudFormation stack outputs (ApiUrl
) with 4 endpoints:
/addition/{number1}/{number2}
: for addition ofnumber1
andnumber2
./subtraction/{number1}/{number2}
: for subtraction ofnumber1
andnumber2
./multiplication/{number1}/{number2}
: for multiplication ofnumber1
andnumber2
./division/{number1}/{number2}
: for division ofnumber1
andnumber2
. It handles the error generated whennumber2
is zero.
The execution of the math operation takes more than 1 second for pure only academic purposes, by using a ´setTimeout´ call:
function apply(operation: BinaryOperation, number1: number, number2: number): Promise<number> {
return new Promise<number>(
resolve => setTimeout(() => resolve(operation(number1, number2)), 1000)
);
}