Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Latest commit

 

History

History
80 lines (53 loc) · 3.19 KB

README.md

File metadata and controls

80 lines (53 loc) · 3.19 KB

Serverless RESTful API example

Just another Serverless RESTful API based on AWS Lambda

Prerequisites

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.

Use the SAM CLI to build and test locally

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

Use the SAM CLI to deploy to AWS

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

Unit tests

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

Cleanup

To delete the application stack, use the following AWS CLI command:

aws cloudformation delete-stack --stack-name hello-world

Add a resource to the application

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.