# Oshun ECR Repositories Terraform Module

This Terraform module creates Amazon Elastic Container Registry (ECR)
repositories for all Oshun services across domains.

## Features

- **Domain-organized repositories**: Separate namespaces for each domain
- **Lifecycle policies**: Automatic cleanup of old images
- **Image scanning**: Vulnerability scanning on push
- **Cross-account access**: Share images across AWS accounts
- **Pull-through cache**: Cache images from Docker Hub and GHCR
- **Cross-region replication**: Replicate images to multiple regions

## Architecture

```
ECR Registry
├── oshun/lilith/
│   ├── bff
│   ├── svc-ai
│   ├── svc-auth
│   └── ... (78 services)
├── oshun/yemaya/
│   ├── api
│   ├── studio-web
│   └── workers
├── oshun/aja/
│   ├── svc-motion-ai
│   ├── svc-motion-pipeline
│   └── svc-reference-video
├── oshun/isis/
│   ├── generation-api
│   ├── gpu-worker
│   └── ...
├── oshun/sophia/
│   ├── search-api
│   └── ...
├── oshun/hathor/
│   └── ...
├── oshun/bellona/
│   └── ...
└── oshun/infra/
    └── gpu-dispatcher
```

## Usage

### Basic Usage

```hcl
module "ecr" {
  source = "./infra/terraform/ecr"

  region      = "us-east-1"
  name_prefix = "oshun"

  # Enable all domains by default
  enable_lilith  = true
  enable_yemaya  = true
  enable_aja     = true
  enable_isis    = true
  enable_sophia  = true
  enable_hathor  = true
  enable_bellona = true

  tags = {
    Project = "oshun"
  }
}
```

### Selective Domain Deployment

```hcl
module "ecr" {
  source = "./infra/terraform/ecr"

  region      = "us-east-1"
  name_prefix = "oshun"

  # Only deploy specific domains
  enable_lilith  = false
  enable_yemaya  = true
  enable_aja     = true
  enable_isis    = true
  enable_sophia  = false
  enable_hathor  = false
  enable_bellona = true

  tags = {
    Environment = "staging"
  }
}
```

### With Cross-Account Access

```hcl
module "ecr" {
  source = "./infra/terraform/ecr"

  region      = "us-east-1"
  name_prefix = "oshun"

  # Allow other accounts to pull
  allowed_account_ids = [
    "111111111111",  # Development account
    "222222222222",  # Staging account
  ]

  # Or allow entire organization
  allowed_organization_ids = ["o-xxxxxxxxxx"]
}
```

### With Pull-Through Cache

```hcl
module "ecr" {
  source = "./infra/terraform/ecr"

  region      = "us-east-1"
  name_prefix = "oshun"

  # Enable pull-through cache for base images
  enable_pull_through_cache        = true
  dockerhub_credentials_secret_arn = "arn:aws:secretsmanager:us-east-1:123456789:secret:dockerhub"
  ghcr_credentials_secret_arn      = "arn:aws:secretsmanager:us-east-1:123456789:secret:ghcr"
}
```

### With Cross-Region Replication

```hcl
module "ecr" {
  source = "./infra/terraform/ecr"

  region      = "us-east-1"
  name_prefix = "oshun"

  # Replicate to other regions for disaster recovery
  replication_regions = ["us-west-2", "eu-west-1"]
}
```

## Initialization

```bash
# Initialize with remote state backend
terraform init \
  -backend-config="bucket=oshun-terraform-state" \
  -backend-config="key=ecr/terraform.tfstate" \
  -backend-config="region=us-east-1" \
  -backend-config="dynamodb_table=oshun-terraform-locks" \
  -backend-config="encrypt=true"

# Plan
terraform plan

# Apply
terraform apply
```

## Inputs

| Name                 | Description                   | Type     | Default       |
| -------------------- | ----------------------------- | -------- | ------------- |
| region               | AWS region                    | `string` | `"us-east-1"` |
| name_prefix          | Repository name prefix        | `string` | `"oshun"`     |
| image_tag_mutability | Tag mutability                | `string` | `"MUTABLE"`   |
| scan_on_push         | Enable vulnerability scanning | `bool`   | `true`        |
| max_image_count      | Max images to retain          | `number` | `10`          |
| enable_lilith        | Create Lilith repos           | `bool`   | `true`        |
| enable_yemaya        | Create Yemaya repos           | `bool`   | `true`        |
| enable_aja           | Create Aja repos              | `bool`   | `true`        |
| enable_isis          | Create Isis repos             | `bool`   | `true`        |
| enable_sophia        | Create Sophia repos           | `bool`   | `true`        |
| enable_hathor        | Create Hathor repos           | `bool`   | `true`        |
| enable_bellona       | Create Bellona repos          | `bool`   | `true`        |

See `variables.tf` for all available inputs.

## Outputs

| Name                   | Description                      |
| ---------------------- | -------------------------------- |
| repository_urls        | Map of all repository URLs       |
| lilith_repository_urls | Lilith service repository URLs   |
| yemaya_repository_urls | Yemaya service repository URLs   |
| registry_url           | ECR registry URL                 |
| docker_login_command   | AWS CLI command for Docker login |
| summary                | Configuration summary            |

## Docker Usage

### Authenticate with ECR

```bash
# Using AWS CLI
aws ecr get-login-password --region us-east-1 | \
  docker login --username AWS --password-stdin \
  123456789.dkr.ecr.us-east-1.amazonaws.com
```

### Build and Push

```bash
# Build image
docker build -t oshun/lilith/svc-auth:latest .

# Tag for ECR
docker tag oshun/lilith/svc-auth:latest \
  123456789.dkr.ecr.us-east-1.amazonaws.com/oshun/lilith/svc-auth:latest

# Push
docker push 123456789.dkr.ecr.us-east-1.amazonaws.com/oshun/lilith/svc-auth:latest
```

### Pull Image

```bash
docker pull 123456789.dkr.ecr.us-east-1.amazonaws.com/oshun/lilith/svc-auth:latest
```

## Lifecycle Policy

The default lifecycle policy:

1. **Untagged images**: Deleted after 7 days
2. **Tagged images** (v\*, latest, main, develop, staging, production): Keep
   last 10
3. **All other images**: Keep last 20 (for PR/branch builds)

## Security

- **Image scanning**: All images scanned for vulnerabilities on push
- **Encryption**: AES256 by default, or KMS for additional control
- **Cross-account**: Explicit allow-list for external access
- **Tag immutability**: Optional for production environments

## Adding New Services

To add a new service:

1. Add service name to the appropriate domain list in `variables.tf`
2. Run `terraform plan` to verify changes
3. Run `terraform apply` to create the repository

Example for Lilith:

```hcl
variable "lilith_services" {
  default = [
    "bff",
    "svc-ai",
    # ... existing services ...
    "svc-new-service",  # Add your new service here
  ]
}
```

## Troubleshooting

### Push Fails with "no basic auth credentials"

Run the docker login command:

```bash
aws ecr get-login-password --region us-east-1 | \
  docker login --username AWS --password-stdin \
  123456789.dkr.ecr.us-east-1.amazonaws.com
```

### Image Not Found

Ensure you're using the full repository URL:

```bash
# Correct
123456789.dkr.ecr.us-east-1.amazonaws.com/oshun/lilith/svc-auth:latest

# Incorrect
oshun/lilith/svc-auth:latest
```

### Lifecycle Policy Not Working

Lifecycle policies run asynchronously. Check policy status:

```bash
aws ecr get-lifecycle-policy --repository-name oshun/lilith/svc-auth
```
