This template is only a study case using serverless framework for a User api.
First start local stack with the necessary services already configured:
docker compose up
By that, the service will be available on http://localhost:3000
If you want to simulate how this deploy will run to aws, perform the following deploy command to LocalStack:
serverless deploy --stage local
$ 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 user-serverless-api (152s)
endpoint: GET - https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/
functions:
hello: user-serverless-api-dev-hello (1.9 kB)
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": {
...
}
}
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 v3.0! Your function executed successfully!\",\n \"input\": \"\"\n}"
}
Alternatively, it is also possible to emulate API Gateway and Lambda locally by using serverless-offline
plugin. In order to do that, execute the following command:
serverless plugin install -n serverless-offline
It will add the serverless-offline
plugin to devDependencies
in package.json
file as well as will add it to plugins
in serverless.yml
.
After installation, you can start local emulation with:
serverless offline
To learn more about the capabilities of serverless-offline
, please refer to its GitHub repository.