Serverless Framework Node HTTP API on AWS
This template demonstrates how to make a simple HTTP API with Node.js 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.
Usage
Deployment
In order to deploy the example, you need to run the following command:
serverless deploy
After running deploy, you should see output similar to:
Deploying "serverless-http-api" to stage "dev" (us-east-1)✔ Service deployed to stack serverless-http-api-dev (91s)endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/functions: hello: serverless-http-api-dev-hello (1.6 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 API (API Gateway V2) event docs.
Invocation
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:
{ "message": "Go Serverless v4! Your function executed successfully!" }
Local development
The easiest way to develop and test your function is to use the dev
command:
serverless dev
This will start a local emulator of AWS Lambda and tunnel your requests to and from AWS Lambda, allowing you to interact with your function as if it were running in the cloud.
Now you can invoke the function as before, but this time the function will be executed locally. Now you can develop your function locally, invoke it, and see the results immediately without having to re-deploy.
When you are done developing, don't forget to run serverless deploy
to deploy the function to the cloud.