This page is intended for developers who want to contribute to the project. This is the backend engine of Launch Pad. We use AWS CDK to deploy the backend infrastructure.
Check Resources for a list of links, guides, etc contributors have found helpful for this project.
-
Clone the repo
git clone https://github.com/ubclaunchpad/StarPort.git
-
Check you have node and npm installed. you can run the following to see if they are installed
node -v npm -v
if not installed refer to their docs for installation steps: node, npm
-
install Typescript (optionally) globally by running
npm install -g typescript
-
install AWS CDK
npm install -g aws-cdk
. verify by runningcdk --version
-
install AWS SAM
npm install -g aws-sam-local
. verify by runningsam --version
if you intend to run the project locally -
install dependencies:
cd starport npm install
To start developing after you run the setup steps you should do the following.
- since our api is connected to a database to run it fully we recommend setting up a MySQL server on your machine or if familiar with Docker, on there.
We are using PlanetScale as our database provider. You can create a free account and create a database. You can then create a user and get the credentials.
After setting up your database, store your db credentials in a env.local
file at the source directory (during deployment we will use .env
that has the cloud database credentials)
An example of how the file will look
DATABASE_HOST=<your-db-host>
DATABASE_USERNAME=<your-db-username>
DATABASE_PASSWORD=<your-db-password>
DATABASE_URL=<your-db-url>
ENVIRONMENT=dev
you will also need to seyt your CDK environment variables. You can do this by creating a cdk.context.json
file at the root of the project. An example of how the file will look
add the following to the .env
file
CDK_TEST_ACCOUNT=<test-account-id>
CDK_TEST_REGION=<test-region>
CDK_DEFAULT_ACCOUNT=<your-account-id>
CDK_DEFAULT_REGION=<your-region>
Tip: lots of good resources to set up on your laptop quickly, and to verify it's connected first try connecting to it either via your terminal or with a client like
Workbench
Following steps varies depending on how you want to run the project
you can easily write tests for each of the lambda groups in their test folder. you can run the tests by running
npm run test:src
if you're writing tests for the deployment stacks too you can just do npm run test
Cdk is a toolkit for AWS cloudformation. after all our code we want to tell AWS what is what and how things are related. Assuming you have looked up basic AWS CDK resources you can run the follwing to build your assets
npm run build
cdk synth
first transpiles our .ts and the next, synthesizes our stacks and constructs (to make it ready for deployment)
To test your lambda functions or API integrations locally, AWS SAM helps here. Sam emulates a porduction environment that AWS would have used to run our project. SAM is a framework for building serverless applications on AWS. It provides a Lambda-like execution environment that lets you locally build, test, and debug applications defined by SAM templates.
To do this, you need to first install SAM and Docker.
check you have them installed:
docker version
sam --version
Tip: check
package.json
for some frequently used scripts related to this
You can test your functions in two ways:
-
Invoke your lambda function based on a specific predefined event (e.g. an
event.json
file)- This will run your lambda function locally and pass in the event as the input and return the output of the function. This is useful for testing your lambdas with testing frameworks like Mocha
- To run your lambda function locally, run
sam local invoke <function-name> -e <event.json>
- To run your lambda function locally with a debugger, run
sam local invoke <function-name> -e <event.json> -d 5858
-
Run your lambda function locally as an API
We recommend a test-driven approach to developing your functions. So use this method sparingly as it can take up your battery if you are running it for a long time. It's a good apporach to do your holistic testing before deploying your functions to AWS.
- To run your lambda function locally as an API, run
sam local start-api
- To run your lambda function locally as an API with a debugger, run
sam local start-api -d 5858
- To run your lambda function locally as an API, run
Note: invoking just runs your function and exits. great for integration/e2e testing. Second one keep the function running
Similar to locally testing your lambdas, you can also run and test your APIs locally. This is helpful when you have a few lambdas or stepfunctions, etc integrated to your API and you want to test the whole flow.
- To run your API locally, run
sam local start-api
- To run your API locally with a debugger, run
sam local start-api -d 5858
Note: For all the sam scripts to run the correct stack, you need to provide the template cdk generates
so before emulation you should run
npm run build
cdk synth
and then with all your stacks you should provide the relative path to the stack's template - check package.json for examples
We have integrated build-start commands into one bash script.
- To run your API locally, run
npm run test:api [stack] [port] [docker-option]
. [stack]
field will be the full stack name under folder[port]
field will be the port the service will listen on[docker-option]
field will be eitherEAGER
orLAZY
Note:
[]
fields are optional, and if is null the script will just use default setting (stack=wiki-template.stack; port=8000; docker-option=LAZY)
Note: when trying to end the process using ctrl+c, if it stuck just kill the terminal.
Refer to the AWS deployment guide for more information. However, we only allow cloud deployment via the CI/CD pipeline. So you should not be deploying to AWS manually; in fact you should not have the AWS credentials to do so or we have set up something wrong :).
The repository offers/implementes a lot of different things. Here is a briefly overview:
- AWS CDK is used for cloud deployment. Everything in the project gets bundled together with CDK. So CDK is responsible for creating all our resources. APIs, Database, running our scripts and so on.
- we test our deployment process with Jest (A testing framework)
- The APIs are based on a serverless model. everything in the src folder serves as the source code. With CDK and the AWS Lambda we create our APIs (if familiar with Express, it's same end-result but different implementation)
- we test our API source code locally by using Jest
- Additonally we use AWS SAM to live-test our api (lambdas + cdk integration)
- The database is a MySQL flavour of SQL. We use Aurora Serverless to adapt to flexible traffic.
- We keep our database identicial throughout development by running migration scripts