Just another Serverless RESTful API based on AWS Lambda
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
To use the SAM CLI, you need the following tools.
- SAM CLI - Install the SAM CLI
- Node.js - Install Node.js 12, including the NPM package management tool.
- Docker - Install Docker community edition
Build your application with the sam build
command.
npm run build
sam build -m ./package.json -t ./templates/service.yaml
The SAM CLI installs dependencies defined in package.json
, creates a deployment package, and saves it in the .aws-sam/build
folder.
Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the events
folder in this project.
Run functions locally and invoke them with the sam local invoke
command.
sam local invoke HelloWorldFunction -t ./templates/service.yaml -e events/api-gateway.json
The SAM CLI can also emulate your application's API. Use the sam local start-api
to run the API locally on port 3000.
sam local start-api -t ./templates/service.yaml
curl http://localhost:3000/hello
The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The Events
property on each function's definition includes the route and method for each path.
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
To build and deploy your application, run the following in your shell:
npm run build
sam sam build -m ./package.json -t ./templates/service.yaml
sam deploy --guided
Tests are defined in the tests
folder in this project. Use NPM to install the Jest testing framework and run unit tests.
npm run test
To delete the application stack, use the following AWS CLI command:
aws cloudformation delete-stack --stack-name hello-world
The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in the SAM specification, you can use standard AWS CloudFormation resource types.