This template demonstrates how to make a simple HTTP API with Firetail running on AWS Lambda and API Gateway using the Serverless Framework.
This template does not include any kind of persistence (database). For more advanced examples, check out the serverless/examples repository which includes Typescript, Mongo, DynamoDB and other examples.
For a complete kitchen sink style example. See: Petstore Express.js Example
Place your API YAML inside the root path of your application. Then run:
// ========== Lets import what we are going to need
const firetailSetup = require("@public.firetail.io/firetail-api");
// ========== firetail options
const firetailOpts = { addApi: "./swagger.yaml" }
// IF you run locally via 'serverless offline'
// firetailOpts.lamdba = true // Add the lamdba flag
// ========== install the firetail middleware
const firetailWrapper = firetailSetup(firetailOpts)
// ========== Add the end-point you want
//...
//To add Firetail logging into your lambda function,
// just pass you function into the `firetailWrapper` before exporting it
module.exports.app = firetailWrapper((event,context) => {
return {
statusCode:200,
body: "FireTail sample"
};
})
//... They should match whats in your YAML
Open the quickstart
cd javascript-quickstarts/lamdba
Now we can install the samples dependencies
npm install
Now you can start the server
serverless offline
Now that the server is started. Open a browser and go to http://localhost:3000
lamdba/
├─ animals.json ← Sample data to be use
├─ petstore.yaml ← The open API specification that this service will a here too
├─ serverless.yml ← Entry point and wiring for the service
└─ handler.js ← Function and wiring for the service
- it's recommended when testing an API to use a tool such as postman
Use Lamdba's native routing mechanism | GET: /pets
This endpoint list all pets. You can find the implementation at handler.js ~ module.exports.pets. It is load on serverless.yml.js ~ functions:pets:handler
The important thing to note is that the service's handler is passed to firetailWrapper
before being exported. This is what allow Firetail to intercept the request and and apply the API protection.
Passing query arguments | GET: /pets?limit=2
The same as above while utilising queryStringParameters
fetch('http://localhost:3000/dev/pets', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
},
body: JSON.stringify({ "name": "Mr Beast", "tag": "dog" })
})
.then((response) => response.text())
.then((data) => console.log('Success:', data))
.catch((error) => console.error('Error:', error));
$ serverless deploy
After deploying, you should see output similar to:
Deploying aws-node-http-api-project to stage dev (us-east-1)
✔ Service deployed to stack aws-node-http-api-project-dev (152s)
endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
hello: aws-node-http-api-project-dev-hello (1.9 kB)
Note: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to http event docs.
After successful deployment, you can call the created application via HTTP:
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/
Which should result in response similar to the following (removed input
content for brevity):
{
"message": "Go Serverless v2.0! Your function executed successfully!",
"input": {
...
}
}