Serverless Framework Node REST API on AWS
This template demonstrates how to make a simple REST 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 "aws-node-rest-api" to stage "dev" (us-east-1)
✔ Service deployed to stack aws-node-rest-api-dev (91s)
endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
functions:
hello: aws-node-rest-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 event docs.
Invocation
After successful deployment, you can call the created application via HTTP:
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
Which should result in response similar to the following (removed input content for brevity):
{
"message": "Go Serverless v4! Your function executed successfully!",
"input": {
...
}
}
Local development
You can invoke your function locally by using the following command:
serverless invoke local --function hello
Which should result in response similar to the following:
{
"statusCode": 200,
"body": "{\n \"message\": \"Go Serverless v4! Your function executed successfully!\",\n \"input\": \"\"\n}"
}
Alternatively, it is also possible to emulate API Gateway and Lambda locally by using 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.
Scaling
By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 1000. The default limit is a safety limit that protects you from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, follow the steps in To request a limit increase for concurrent executions.