/fastify-lambda-url

Primary LanguageTypeScriptMIT LicenseMIT

project-logo

AWS Lambda Function URL Example

Supporting the blog article "AWS Lambda Function URLs with SAM Local and Postman"

license last-commit repo-top-language repo-language-count


Table of Contents

Overview

This repo is supporting code for the Medium article "AWS Lambda Function URLs with SAM Local and Postman". In the article, we look specifically at invoking a local Lambda Function via URL using Postman. To simplify the approach we utilize Fastify to quickly set up an API in our Lambda, and include a SAM template to facilitate the local deployment and execution of the function.

This repo contains all the code needed to run the lambda locally and should be sufficient for deploying to AWS.


Features

Feature Description
โš™๏ธ Architecture The project leverages AWS Lambda and Fastify to create a serverless web application.
๐Ÿงช Testing Jest and ts-jest are used for testing, ensuring code reliability and robustness. Test coverage is minimal for example only.
๐Ÿ“ฆ Dependencies Key dependencies include Fastify, AWS Lambda, Jest, SAM CLI and other related libraries.

Repository Structure

โ””โ”€โ”€ fastify-lambda-url/
    โ”œโ”€โ”€ LICENSE
    โ”œโ”€โ”€ README.md
    โ”œโ”€โ”€ event.json
    โ”œโ”€โ”€ jest.config.js
    โ”œโ”€โ”€ package-lock.json
    โ”œโ”€โ”€ package.json
    โ”œโ”€โ”€ src
    โ”‚   โ”œโ”€โ”€ app.ts
    โ”‚   โ”œโ”€โ”€ index.ts
    โ”‚   โ””โ”€โ”€ tests
    โ”œโ”€โ”€ template.yaml
    โ”œโ”€โ”€ tsconfig.json
    โ””โ”€โ”€ webpack.config.js

Modules

root
File Summary
jest.config.js Configures Jest for TypeScript tests in a Node environment by specifying the TypeScript preset and test file matching patterns.
event.json Describes an AWS Lambda event payload for a route related to headers, query parameters, request context, and body data.
webpack.config.js The webpack configuration for building the project.
package.json Manages dependencies, testing, starting the server, and bundling for production. Dependencies include Fastify, AWS Lambda, and Jest for testing.
tsconfig.json Enables strict TypeScript compilation targeting ES2020 and CommonJS modules while excluding test files. Maintains consistent file casing and interop with ES modules, with output to the dist directory.
template.yaml Defines SAM template for deployment of the lambda using sam or cfn and creating the Lambda function URL.
src
File Summary
index.ts Enables AWS Lambda integration for Fastify app, routing APIGatewayProxyEvent to app logic via awsLambdaFastify. Centralizes Lambda proxy setup within handler method.
app.ts Implements Fastify routes for hello endpoint using GET and POST methods. Registers routes with Fastify instance for a server.

Getting Started

System Requirements:

  • TypeScript
  • NPM
  • SAM CLI
  • Postman or ability to issue curl commands

Installation

From source

  1. Clone the fastify-lambda-url repository:
git clone https://github.com/sawyerit/fastify-lambda-url
  1. Change to the project directory:
cd fastify-lambda-url
  1. Install the dependencies:
npm install

Usage

From source

Run fastify-lambda-url using the command below:

npm run build

and then

npm run start

Tests

Run the test suite using the command below:

npm run test

Building your own from scratch

See CREATING.


License

This project is protected under the MIT License. For more details, refer to the LICENSE file.