# SST Serverless Stack: Full-Stack Serverless Framework

# SST (Serverless Stack): Full-Stack Serverless Framework

## Overview
SST is a modern framework for building and deploying full-stack serverless applications. It combines infrastructure-as-code with a local development environment, enabling developers to build, test, and deploy serverless applications efficiently.

## Key Features

### 1. **Full-Stack Development**
- Frontend and backend in a single codebase
- Unified deployment pipeline
- Shared type definitions between client and server
- Built-in support for React, Vue, Svelte, and other frameworks

### 2. **Local Development**
- Live Lambda function reloading
- Local DynamoDB and other AWS service emulation
- Real-time debugging capabilities
- Hot module replacement for frontend

### 3. **Infrastructure as Code**
- TypeScript-based infrastructure definitions
- Type-safe resource configuration
- Automatic resource linking and permissions
- Environment-specific deployments

### 4. **Built-in Constructs**
- **Api**: REST and GraphQL APIs
- **Function**: Lambda functions with automatic permissions
- **Table**: DynamoDB tables with simplified configuration
- **Bucket**: S3 buckets with CORS and access control
- **Queue**: SQS queues for async processing
- **Topic**: SNS topics for pub/sub messaging
- **Auth**: Cognito authentication
- **RDS**: Relational database support

## Architecture

```
┌─────────────────────────────────────┐
│      Frontend (React/Vue/etc)       │
├─────────────────────────────────────┤
│    SST Client SDK (Type-safe)       │
├─────────────────────────────────────┤
│   API Gateway + Lambda Functions    │
├─────────────────────────────────────┤
│  DynamoDB | RDS | S3 | Other AWS    │
└─────────────────────────────────────┘
```

## Project Structure

```
my-sst-app/
├── sst.config.ts          # Stack configuration
├── stacks/
│   ├── Api.ts             # API definitions
│   ├── Database.ts        # Database setup
│   └── Storage.ts         # S3 buckets
├── packages/
│   ├── core/              # Shared code
│   ├── functions/         # Lambda handlers
│   └── web/               # Frontend app
└── package.json
```

## Core Concepts

### 1. **Stacks**
Logical groupings of AWS resources that can be deployed independently:
```typescript
export function API({ stack }: StackContext) {
  const api = new Api(stack, "api", {
    routes: {
      "GET /notes": "packages/functions/src/list.main",
      "POST /notes": "packages/functions/src/create.main",
    },
  });
  
  stack.addOutputs({
    ApiEndpoint: api.url,
  });
}
```

### 2. **Resource Linking**
Automatic permission management and environment variable injection:
```typescript
const table = new Table(stack, "notes", {
  fields: { id: "string" },
  primaryIndex: { partitionKey: "id" },
});

const api = new Api(stack, "api", {
  routes: {
    "GET /notes": "list.main",
  },
});

api.attachPermissions([table]);
```

### 3. **Type Safety**
Automatic type generation for resources:
```typescript
import { Resource } from "sst";

export const handler = async (event) => {
  const table = Resource.NotesTable;
  // Full TypeScript support for table operations
};
```

## Development Workflow

### Local Development
```bash
# Start local development environment
npm run dev

# Runs:
# - Frontend dev server with hot reload
# - Lambda functions with live reload
# - Local AWS service emulation
```

### Deployment
```bash
# Deploy to AWS
npm run deploy

# Deploy specific stage
npm run deploy -- --stage prod

# Remove stack
npm run remove
```

## Common Use Cases

### 1. **REST API with Database**
- Lambda functions handling HTTP requests
- DynamoDB for data persistence
- Automatic CORS configuration

### 2. **Real-time Applications**
- WebSocket APIs via API Gateway
- DynamoDB Streams for real-time updates
- SNS/SQS for async processing

### 3. **File Processing**
- S3 event triggers
- Lambda for processing
- Results stored in database

### 4. **Authentication & Authorization**
- Cognito user pools
- JWT token validation
- Role-based access control

## Advantages

✅ **Unified Development**: Single codebase for frontend and backend  
✅ **Type Safety**: End-to-end TypeScript support  
✅ **Fast Iteration**: Local development with live reload  
✅ **Cost Effective**: Pay-per-use serverless pricing  
✅ **Scalability**: Auto-scaling built-in  
✅ **Developer Experience**: Minimal boilerplate, sensible defaults  
✅ **AWS Native**: Direct access to AWS services  

## Limitations

❌ Cold starts (mitigated with provisioned concurrency)  
❌ Vendor lock-in to AWS  
❌ Debugging complexity in production  
❌ Limited execution time (15 minutes for Lambda)  
❌ Stateless by design  

## Comparison with Alternatives

| Feature | SST | Serverless Framework | AWS SAM | Terraform |
|---------|-----|---------------------|---------|-----------|
| Full-Stack | ✅ | ❌ | ❌ | ✅ |
| Local Dev | ✅ | ⚠️ | ⚠️ | ❌ |
| Type Safety | ✅ | ❌ | ❌ | ⚠️ |
| Learning Curve | Low | Medium | Medium | High |
| AWS Focus | ✅ | ✅ | ✅ | ❌ |

## Getting Started

```bash
# Create new SST project
npm create sst@latest my-app

# Navigate and start development
cd my-app
npm run dev

# Deploy
npm run deploy
```

## Conclusion

SST is ideal for teams building modern, full-stack serverless applications on AWS. It significantly reduces development friction through excellent local development experience, type safety, and sensible defaults, making it one of the best choices for serverless development in 2024.
