This project creates four lambdas, one each to compute the mean, median, and mode of a series of numbers, and one lambda authorizer. Two of the lambdas are in Java (Mean and Median) and two are in TypeScript running on Node.js (Mode and Authorizer). As such, it has a number of installation requirements to work from end-to-end.
I went a bit outside my comfort zone; instead of using the TypeScript CDK, I decided to use the Java one. It was a bit of a (re)learning curve, but I got there after a little bit of effort.
Before you begin, make sure you have the following tools installed:
-
Node.js and npm via nvm:
- Install Node.js using nvm (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash nvm install --lts
- Alternatively, if on Windows for example, download and install the LTS version of Node.js from the official website. Verify your installation by running:
node -v npm -v
- Install Node.js using nvm (Node Version Manager):
-
AWS CLI: Follow the instructions in the AWS CLI Setup Guide to install the AWS CLI. Verify your installation by running:
aws --version
-
AWS CDK: Follow the instructions in the AWS CDK Getting Started guide to install the AWS CDK. Verify your installation by running:
cdk --version
-
AWS SAM CLI: Install the AWS SAM CLI using the instructions provided in the AWS SAM documentation.Verify your installation by running:
sam --version
-
Java Development Kit (JDK) 11: Download and install JDK 11. Verify your installation by running:
java -version
-
Maven: Download and install Maven from the official website. Verify your installation by running:
mvn -v
The project is primarily powered by NPM and series of commands registered in package.json
. Below is the suggested order, but feel free to play around with them yourself:
-
Install NPM dependecies:
npm install
-
Build the project, run tests, and synthesize the CDK into CloudFormation:
npm run build
This may take several minutes as maven installs the necessary java dependencies
-
Invoke the lambdas to test locally:
npm run invoke:mean npm run invoke:median npm run invoke:mode
-
You can also start an API for all three lambdas at localhost:3000
npm run start-api
-
Once the server is running you can POST to any of the endpoints (
/mean
,/median
,/mode
) with anAuthorization:c346c99b-4216-4ab8-bec6-8a04a5ff2ebe
header and a JSON body array of numbers and get the results:[1, 2, 5, 4, 5, 7 ]
-
You can run the unit tests individually or altogther:
npm run test #runs all the tests npm run test:mean npm run test:median npm run test:mode npm run test:infra npm run test:auth
-
If you want to deploy your lambdas to AWS, you can execute the following commands (assuming you have AWS credentials configured):
npm run bootstrap # deploys a CF template to AWS to bootstrap the CDK npm run deploy # deploys the lambdas and their api gateway integrations
Once everything is deployed you will see the URL of the API output in the console:
Outputs: StatisticsApiStack.HttpApi = https://abc123.execute-api.us-east-1.amazonaws.com
Make sure to copy down your specific URL for use in further manual testing.
-
You can now run the integration tests which will verify that the deployment succeeded:
npm run test:integration
-
You can manually test by POSTing to
/mean
,/median
,/mode
using the above API Gateway URL the same way you did in local testing. Make sure to include theAuthorization:c346c99b-4216-4ab8-bec6-8a04a5ff2ebe
header in your request.
Without spending too much time on this project, I feel like what I have done is a good representation of my skills, though with more time, there are a few things to do that would truly make this production ready:
- Quality gates for code coverage and linting
- Pre-push git hooks that enforce the quality gates
- Build templates for pipeline deployments (CircleCI, Jenkins, Travis CI, etc.)
- Better input validation and error messaging
- More, more, and more unit tests
- Could explore depdendency injection freamworks, but it's not always my go-to for functions as a service due to its (potential) impact on startup times
- Setup project debugging for various IDEs