This project consists of a single API Lambda function deployed to AWS that can be accessed using the Function URL(FURL). The AWS CDK has been used to define the IaC and the Lambda is written in TS. It has two endpoints:
- Get all pets
- Get a pet by ID
A pre-build step uses the OpenAPI spec (src/openapi/openapi.yaml
) to generate TS types to be used at compile time.
It also uses the Open API spec to generate JS validation functions using the AJV standalone method that can be used at runtime.
This is how we validate both the input and output of our API and make it the source of truth for our API.
Deployment is done manually using the JS GULP script in this project. This project does it manually to keep it brief, but ideally you do
this in your pipeline. After the CDK is deployed (with the cdk_deploy
command), it publishes the API SDK as an NPM package to GitHub.
The published NPM package version is the same as the OpenAPI spec version and will only
be published if the version is more than the current version.
You can use the GULP plugin for your IDE to debug and run the
gulpfile.js
that is used as the build script. You can also run the scripts vianpm run <TASK>
for your convenience.
- Replace the values if you want to do deploy to AWS and publish the API as an SDK in the
gulpfile.js
on lines 45-52:
<YOUR AWS PROFILE NAME TO USE FOR DEPLOYMENTS>
<AWS REGION TO DEPLOY IN>
<AWS ACCOUNT ID TO DEPLOY IN>
<HTTPS URL OF THE EXISTING EMPTY GITHUB REPO TO STORE THE API SDK ENDING IN .git>
example https://github.com/rehanvdm/pets-api.git<SDK PACKAGE NAME AS IN THE package.json>
must start with @, example @rehanvdm/pets-api<SDK PACKAGE REPO AS IN THE package.json>
example git://github.com/rehanvdm/pets-api.git
Useful commands:
npm run build_types
- Takes the OpenAPI spec and creates TS types to be used at compile time as well as JS validation functions to be used at runtime.npm run watch_types
- Watches the OpenAPI spec atsrc/openapi/openapi.yaml
and if they change, it runs thebuild_types
command above.npm run build_lambdas
- Does the TS => JS transpolation and then copies thesrc
files to thedist
folder.npm run test
- Runs thetest/api.js
file that contains basic Mocha & Chai tests that first transpiles the TS to JS with the same ESBuild function that is used for deployments in the GULP file. Then it passes an event and context to the function to test it locally.
- Create a Personal Access Token(PAT) on GitHub
so that we can write the SDK API NPM package. You need to assign the flowing permissions:
write:packages
Upload packages to the GitHub Package Registry
- Make a copy of the
.npmrc.example
file and replace: -<YOUR PERSONAL ACCESS TOKEN HERE>
with your GitHub Personal Access Token (PAT) that you obtained above -<YOUR GITHUB PROFILE OR ORGANIZATION NAME>
with your GutHub username of where the package is installed
Deploys a Lambda + FURL on AWS.
npm run cdk_diff
- Runsbuild_types
andbuild_lambdas
, then thecdk diff
command.npm run cdk_deploy
- Runsbuild_types
andbuild_lambdas
, then thecdk deploy
command. After that it runs thegenerate_and_push_api_sdk
command to do the API SDK generation and package publishing logic.npm run cdk_hotswap
- Runsbuild_types
andbuild_lambdas
, then thecdk deploy --hotswap
command.
After you deployed, you will be given the Function URL. You will have to update the API URL in the frontend repository with this value.
See the frontend docs here: https://github.com/rehanvdm/pets-frontend