# CDK Infrastructure

All Suno infrastructure is defined using AWS CDK (Cloud Development Kit) in TypeScript.

## What is CDK?

AWS Cloud Development Kit - define infrastructure using TypeScript (or Python) instead of YAML/JSON.

**Location**: `/suno-cdk`

## Key Stacks

**File**: `suno-cdk/bin/suno-cdk.ts`

### 1. BackendInfraStack

Django API server infrastructure:

```typescript
new BackendInfraStack(app, 'BackendInfra', {
  vpc,
  cluster,
  database,
  // ECS services, load balancers, auto-scaling
});
```

**Contains**:
- ECS Fargate services for Django
- Application Load Balancer
- Target groups and health checks
- Auto-scaling policies (CPU-based)
- Security groups and IAM roles

### 2. BackgroundJobStack

Celery worker infrastructure:

```typescript
new BackgroundJobStack(app, 'BackgroundJobs', {
  cluster,
  redisCluster,
  // Celery beat scheduler, worker pools
});
```

**Contains**:
- Celery Beat scheduler (periodic tasks)
- Celery worker pools with auto-scaling
- Redis connection for task broker
- Dead letter queues for failed tasks

### 3. RedisCacheStack

Redis/Valkey cluster setup:

```typescript
new RedisCacheStack(app, 'RedisCache', {
  vpc,
  // Multiple cache clusters for different purposes
});
```

**Contains**:
- Multiple ElastiCache clusters
- Multi-AZ deployment
- Automatic failover configuration
- Parameter groups for tuning

### 4. EventStreamStack

Analytics pipeline:

```typescript
new EventStreamStack(app, 'EventStream', {
  // Kinesis streams, Firehose delivery
});
```

**Contains**:
- Kinesis Data Streams
- Firehose delivery to S3
- Lambda processors for transformations
- Integration with analytics platforms

### 5. CloudFrontStack

CDN distribution:

```typescript
new CloudFrontStack(app, 'CDN', {
  s3Bucket,
  // CloudFront distribution, SSL certs
});
```

**Contains**:
- CloudFront distribution
- Origin configurations (S3, ALB)
- SSL/TLS certificates
- Cache behaviors and policies

## CDK Deployment

import { Callout } from 'nextra/components'

<Callout type="info">
  **CDK auto-deploys via CodePipeline!**

  The CDK is automatically deployed to prod after merging to main via [suno-cdk-pipeline](https://us-east-2.console.aws.amazon.com/codesuite/codepipeline/pipelines/suno-cdk-pipeline/view?region=us-east-2) in the suno-dev AWS account.

  Manual deployment is rarely needed.
</Callout>

### Automated Deployment Process

1. **Make changes** to CDK code in `/suno-cdk`
2. **Open a PR** with your infrastructure changes
3. **Get approval** from team
4. **Merge to main** - Pipeline automatically deploys to dev → staging → prod

### Manual Deployment (Rare)

Only needed for emergency fixes or testing:

```bash
cd suno-cdk

# View changes
cdk diff --profile staging BackendInfraStack

# Deploy specific stack
cdk deploy --profile staging BackendInfraStack
```

<Callout type="warning">
  **Always run `cdk diff` before manual deployment!**

  Review changes carefully, especially:
  - Resource deletions (will lose data)
  - Security group changes (can break connectivity)
  - Database modifications (can cause downtime)
</Callout>
