AWS Lambda function template with TypeScript for creating a API proxy with multiple endpoints that support both Get and Post methods. It creates an API proxy for the endpoints from JSONPlaceholder.
This API creates 6 different endpoints
Endpoint | Method | Output |
---|---|---|
/get-users | GET | Get all users |
/get-user/{id} | GET | Get a single user by id |
/add-user | POST | Add a user |
/get-posts | GET | Get all posts |
/get-post/{id} | GET | Get a single post by id |
/add-post | POST | Add a post |
Data Examples
- For add-user
User data
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
response comes back with mock id which is always the same
{
"id": 11
}
- for add-post
Post data example
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore"
}
response comes back with mock id which is always the same
{
"id": 101
}
- Serverless
- Webpack
- Typescript
- Jenkins
- Mocha
- Lambda
- API Gateway
(1) Install all modules
yarn install
It uses mocha for unit & integration tests. Istanbul for coverage.
# unit test
yarn test
# integration test
yarn run integration
Use sls
commands. Make sure to install serverless globally (yarn global add serverless
)
# nonprod
sls deploy --stage nonprod
# prod
sls deploy --stage prod
To remove, run the command below:
sls remove --stage nonprod
curl -H "x-api-key: <api key" -X GET https://url
We recommend to use AWS Lambda Power Tuning to optimise the memory size allocation (see details here).