This project contains source code and supporting files for a serverless redirect application that you can deploy with the SAM CLI. It includes the following files and folders.
- redirect - Code for the application's Lambda function written in TypeScript.
- redirect/tests/unit - Unit tests for the application code.
- redirect/tests/integration - Integration tests for the application code.
- template.yaml - A template that defines the application's AWS resources.
The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the template.yaml
file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
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 18, including the NPM package management tool.
- Docker - Install Docker community edition
To build and deploy your application for the first time, run the following in your shell:
sam build
sam deploy --guided
Build your application with the sam build
command.
sample-redirect$ sam build
The SAM CLI installs dependencies defined in redirect/package.json
, compiles TypeScript with esbuild, 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.
sample-redirect$ sam local invoke RedirectFunction --event events/event.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.
To simplify troubleshooting, SAM CLI has a command called sam logs
. sam logs
lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug.
NOTE
: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
sample-redirect$ sam logs -n RedirectFunction --stack-name sample-redirect --tail
You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.
Tests are defined in the redirect/tests
folder in this project. Use NPM to install the Jest test framework and run unit tests.
sample-redirect$ cd redirect
redirect$ npm install
redirect$ npm run test
To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
sam delete --stack-name sample-redirect