# Microservices/serverless, AWS Lambda example
AWS Lambda is a core component for building serverless microservices, allowing you to run code without managing servers. The most common example of this architecture involves combining **Amazon API Gateway** with **AWS Lambda** and **Amazon DynamoDB** to create a fully serverless, event-driven API.
Serverless Microservice Architecture Example (API-driven)
This pattern is ideal for creating discrete, independent services that handle specific tasks over HTTP requests, such as processing payments or managing user data.
Key Components
- **Amazon API Gateway**: Acts as the "front door" for the microservice, handling API calls (e.g., GET, POST) and routing them to the appropriate backend service. - **AWS Lambda**: The compute service that runs your business logic in response to events (in this case, an API request). You only pay for the exact compute time consumed. - **Amazon DynamoDB**: A serverless NoSQL database used to store and retrieve data for the microservice.
Example Scenario: "Order Processing" Microservice
Imagine a large e-commerce application that needs a dedicated service for handling new orders.
**1. The Request:** A user clicks "Place Order" on the website. A front-end application makes a `POST` request to the `/orders` endpoint exposed by the Amazon API Gateway.
**2. Event Trigger & Compute:**
- API Gateway receives the request. - It is configured to trigger a specific AWS Lambda function (e.g., `processOrderFunction`). - AWS Lambda automatically runs the associated code without you needing to provision or manage any server infrastructure.
**3. Business Logic & Data Storage:**
- The `processOrderFunction` executes the order processing logic (e.g., validating items, calculating the total). - The function then writes the new order details into a dedicated `OrdersTable` in Amazon DynamoDB.
**4. The Response:**
- DynamoDB confirms the write operation to the Lambda function. - The Lambda function returns a success response to API Gateway. - API Gateway sends an `HTTP 200 OK` status back to the user's front-end application.
Other Serverless Patterns
Beyond API-driven microservices, AWS Lambda is central to other event-driven patterns:
- **Data Processing Pipeline:** An object uploaded to an Amazon S3 bucket can trigger a Lambda function to resize an image, convert a file, or analyze metadata. - **Stream Processing:** Data flowing through Amazon Kinesis or Amazon SQS (Simple Queue Service) can trigger Lambda functions for real-time analytics or decoupled message processing.