Skip to content

An example of how to build a simple API with API Gateway, Lambda and the serverless framework

License

Notifications You must be signed in to change notification settings

fourTheorem/api-gateway-sls-example

Repository files navigation

api-gateway-sls-example

Node.js CI

An example of how to build a simple API with:

Using Node.js

This API will fetch the current home page of the New York Times and scrape the latest articles which will be returned as a JSON array of titles and links.

Requirements

Requires Node.js 14 (or newer) and npm.

Dependencies

Install dependencies with:

npm install

Test locally

To execute the function locally, you can run:

npx serverless invoke local -f getNews

To run the unit tests, you can run:

npm test

Deploy

Run:

npx serverless deploy

The first deployment will take 2-3 minutes.

This should output the url of the deployed API (something like: https://xyz.execute-api.eu-west-1.amazonaws.com/dev/news) and the API key (something like abcdefghijklmnopqrstuvwxyz0123456789abcd).

You can then run:

curl -H 'X-Api-Key: abcdefghijklmnopqrstuvwxyz0123456789abcd' https://xyz.execute-api.eu-west-1.amazonaws.com/dev/news | jq .

To see the API in action.

🎉

Cleanup

To remove all the resources created by this project run:

npx serverless remove

Notes

Scaffolding a new project

A quick way to scaffold a new Node.js project with Serverless framework is by using the following command:

npx serverless create --template aws-nodejs --path nameOfYourService

But be aware that this might not be fully up to date and it might give you a Serverless v2 project (rather than v3).

About

An example of how to build a simple API with API Gateway, Lambda and the serverless framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published