Building a Practical Serverless API with SAM: Leveraging Nested Stacks for Organization

This example demonstrates how to build and deploy a simple serverless API using AWS Lambda, API Gateway, and AWS SAM (Serverless Application Model). The core focus here is on leveraging modularity and nested stacks to create a well-organized and scalable project structure. Project Structure Here’s a quick overview of the project’s key components: api.yaml: This SAM file defines the API Gateway. It’s responsible for linking HTTP paths (like /orders and /logs) to the appropriate Lambda Functions. Crucially, it imports the API definition from openapi.yaml. log.yaml: This SAM file contains the definition for the Lambda Function responsible for fetching logs (GetLogs). order.yaml: This SAM file contains the definition for the Lambda Function responsible for fetching orders (GetOrders). openapi.yaml: This is the OpenAPI (Swagger) file that specifies the API paths (e.g., /orders, /logs) and how they integrate with the Lambda Functions. Here, we define the x-amazon-apigateway-integration that links each path to the ARN of the relevant Lambda Function. template.yaml: This is the main SAM template for your project. It acts as an orchestrator for all the sub-stacks (OrderStack, LogStack, ApiStack). Each sub-stack points to its respective YAML file using the Location property of AWS::Serverless::Application. Why Modularity and Nested Stacks? Modularity & Organization: When you split your project into separate files like order.yaml, log.yaml, and api.yaml, your project becomes easier to organize and understand. Each file focuses on one part of the app, so it’s easier to work on and update, especially when the project gets bigger. ...

Build a Simple API on AWS with Lambda, API Gateway, and SAM

This example shows how to build and deploy a simple serverless API using AWS Lambda, API Gateway, and AWS SAM (Serverless Application Model). The API fetches a list of todos from a public endpoint and returns them via an HTTP GET request. Project Structure Here’s a quick overview of the project’s key components: functions/: Contains the Lambda function ListTodos.mjs, which fetches todo items from a public API using Axios. tests/: Contains a Jest test to ensure the function returns items with a 200 status code. layers/: Optional shared layer for common dependencies (e.g., axios), defined under nodejs/package.json. template.yaml: AWS SAM template that defines the infrastructure: Lambda function (ListTodosFunction) API Gateway (/posts) Shared Layer How It Works When a client makes a GET request to /todos, the Lambda function is triggered, fetches data from https://jsonplaceholder.typicode.com/todos?_limit=3, and returns it as JSON. ...

Serverless Image Processing with AWS SAM: Uploads, Thumbnails & S3 Events

This example demonstrates a robust serverless architecture on AWS for handling file uploads and automated thumbnail generation. It leverages AWS SAM (Serverless Application Model), Lambda functions, API Gateway, and S3 event notifications to create a streamlined and scalable image processing pipeline. Project Structure Here’s a quick overview of the project’s key components: functions/: Contains the core Lambda function code: event.mjs: Handles S3 event notifications, triggering the thumbnail generation process. upload.mjs: Manages the initial file upload through API Gateway. layers/nodejs/: A Lambda layer containing shared code and dependencies for the functions: lib/utils.mjs: Common utility functions. package.json & package-lock.json: Node.js dependencies for the layer. template.yaml: The AWS SAM template defining the serverless resources: Lambda functions, API Gateway endpoints, S3 buckets, and their respective permissions and event triggers. How It Works The workflow is straightforward and efficient: ...

Serverless API Monitoring with AWS SAM: Lambda, API Gateway & CloudWatch Alarms

This example demonstrates how to build a lightweight, serverless API using AWS SAM (Serverless Application Model) while integrating real-time error monitoring and email alerts using CloudWatch Alarms and SNS. This pattern is ideal for production-ready serverless applications that need observability without heavy tooling. Project Structure The project includes the following core components: functions/: Contains the Lambda function that handles HTTP requests: app.mjs: Returns a simple response (e.g., “Hello World”). template.yaml: The SAM template defining resources including: Lambda Function (hello handler) API Gateway (to expose the function via HTTP) CloudWatch Alarm (monitors API 5XX errors) SNS Topic & Subscription (sends email alerts) How It Works This architecture sets up an API and monitors its availability in real time: ...