https://github.com/adrianhdezm/serverless-rest-api
Just another Serverless RESTful API based on AWS Lambda
https://github.com/adrianhdezm/serverless-rest-api
aws aws-lambda cloudformation sam-cli sam-template serverless typescript
Last synced: 27 days ago
JSON representation
Just another Serverless RESTful API based on AWS Lambda
- Host: GitHub
- URL: https://github.com/adrianhdezm/serverless-rest-api
- Owner: adrianhdezm
- License: mit
- Archived: true
- Created: 2019-02-27T11:51:23.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T13:52:23.000Z (over 2 years ago)
- Last Synced: 2025-03-05T04:02:05.572Z (3 months ago)
- Topics: aws, aws-lambda, cloudformation, sam-cli, sam-template, serverless, typescript
- Language: TypeScript
- Homepage:
- Size: 416 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
- SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
- Node.js - [Install Node.js 12](https://nodejs.org/en/), including the NPM package management tool.
- Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)## Use the SAM CLI to build and test locally
Build your application with the `sam build` command.
```bash
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.
```bash
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.
```bash
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.
```yaml
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:
```bash
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](https://jestjs.io) and run unit tests.
```bash
npm run test
```## Cleanup
To delete the application stack, use the following AWS CLI command:
```bash
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](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types.