# Fleet Module

This Terraform module provisions a complete fleet infrastructure including API servers, admin API, cloud API, cloud websocket servers, and web applications on AWS.

## Prerequisites

- Terraform >= 0.12
- AWS CLI configured with appropriate credentials
- AWS account with necessary permissions to create EC2 instances, load balancers, and related resources

## Module Structure

The fleet module consists of the following sub-modules:
- `api` - API server infrastructure
- `admin_api` - Admin API server infrastructure
- `cloud_api` - Cloud API server infrastructure
- `cloud_websocket` - Cloud websocket server infrastructure
- `webapps` - Web applications infrastructure

## Usage

### Basic Usage

Navigate to the fleet directory and run:

```bash
cd fleet
terraform init
terraform plan
terraform apply
```

### Custom Configuration

Create a `terraform.tfvars` file to override default values:

```hcl
network_name = "production"
fleet_name   = "prod"

# Instance types
api_instance_type              = "t3.medium"
admin_api_instance_type        = "t3.small"
cloud_api_instance_type        = "t3.medium"
cloud_websocket_instance_type  = "t3.medium"
webapps_instance_type          = "t3.large"

# AMIs (replace with your custom AMIs)
api_ami              = "ami-xxxxxxxxxxxxxxxxx"
admin_api_ami        = "ami-xxxxxxxxxxxxxxxxx"
cloud_api_ami        = "ami-xxxxxxxxxxxxxxxxx"
cloud_websocket_ami  = "ami-xxxxxxxxxxxxxxxxx"
webapps_ami          = "ami-xxxxxxxxxxxxxxxxx"

# SSH Key names
api_key_name              = "production_key"
admin_api_key_name        = "production_key"
cloud_api_key_name        = "production_key"
cloud_websocket_key_name  = "production_key"
webapps_key_name          = "production_key"

# Certificate ARNs
api_elb_certificate_arn              = "arn:aws:acm:us-west-2:xxxx:certificate/xxxx"
admin_api_elb_certificate_arn        = "arn:aws:acm:us-west-2:xxxx:certificate/xxxx"
cloud_api_elb_certificate_arn        = "arn:aws:acm:us-west-2:xxxx:certificate/xxxx"
cloud_websocket_elb_certificate_arn  = "arn:aws:acm:us-west-2:xxxx:certificate/xxxx"
webapps_elb_certificate_arn          = "arn:aws:acm:us-west-2:xxxx:certificate/xxxx"

# Placement group (set to "1" to enable for webapps)
use_placement_group_for_webapps = "0"
```

## Variables

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `network_name` | string | `"dev"` | Name of the network environment |
| `fleet_name` | string | `"dev"` | Name of the fleet |
| `name` | string | `"cloud"` | Base name for resources |
| `api_ami` | string | `"ami-03aa99ddf5498ceb9"` | AMI ID for API servers |
| `admin_api_ami` | string | `"ami-03aa99ddf5498ceb9"` | AMI ID for Admin API servers |
| `cloud_api_ami` | string | `"ami-03aa99ddf5498ceb9"` | AMI ID for Cloud API servers |
| `cloud_websocket_ami` | string | `"ami-03aa99ddf5498ceb9"` | AMI ID for Cloud Websocket servers |
| `webapps_ami` | string | `"ami-03aa99ddf5498ceb9"` | AMI ID for Webapps servers |
| `api_instance_type` | string | `"t2.micro"` | Instance type for API servers |
| `admin_api_instance_type` | string | `"t2.micro"` | Instance type for Admin API servers |
| `cloud_api_instance_type` | string | `"t2.micro"` | Instance type for Cloud API servers |
| `cloud_websocket_instance_type` | string | `"t2.micro"` | Instance type for Cloud Websocket servers |
| `webapps_instance_type` | string | `"t2.micro"` | Instance type for Webapps servers |
| `api_key_name` | string | `"dev_cloud"` | SSH key name for API servers |
| `admin_api_key_name` | string | `"dev_cloud"` | SSH key name for Admin API servers |
| `cloud_api_key_name` | string | `"dev_cloud"` | SSH key name for Cloud API servers |
| `cloud_websocket_key_name` | string | `"dev_cloud"` | SSH key name for Cloud Websocket servers |
| `webapps_key_name` | string | `"dev_cloud"` | SSH key name for Webapps servers |
| `api_elb_certificate_arn` | string | See main.tf | ACM certificate ARN for API ELB |
| `admin_api_elb_certificate_arn` | string | See main.tf | ACM certificate ARN for Admin API ELB |
| `cloud_api_elb_certificate_arn` | string | See main.tf | ACM certificate ARN for Cloud API ELB |
| `cloud_websocket_elb_certificate_arn` | string | See main.tf | ACM certificate ARN for Cloud Websocket ELB |
| `webapps_elb_certificate_arn` | string | See main.tf | ACM certificate ARN for Webapps ELB |
| `use_placement_group_for_webapps` | string | `"0"` | Enable placement group for webapps (set to "1" to enable) |

## Outputs

The module outputs the following values:

- `api` - API module outputs
- `admin_api` - Admin API module outputs
- `cloud_api` - Cloud API module outputs
- `cloud_websocket` - Cloud Websocket module outputs
- `webapps` - Webapps module outputs

View outputs after applying:

```bash
terraform output
```

## Terraform Commands

### Initialize

Initialize the Terraform working directory:

```bash
terraform init
```

### Plan

Preview changes before applying:

```bash
terraform plan
```

Or save the plan to a file:

```bash
terraform plan -out=tfplan
```

### Apply

Apply the Terraform configuration:

```bash
terraform apply
```

Or apply a saved plan:

```bash
terraform apply tfplan
```

### Destroy

Destroy all resources created by this module:

```bash
terraform destroy
```

### Validate

Validate the Terraform configuration:

```bash
terraform validate
```

### Format

Format Terraform files to canonical style:

```bash
terraform fmt
```

## Notes

- Default region is set to `us-west-2`
- Default AMI is Ubuntu 24 Base (`ami-03aa99ddf5498ceb9`)
- Ensure your AWS credentials have sufficient permissions before running
- Always run `terraform plan` before `terraform apply` to review changes
- Certificate ARNs must be valid ACM certificates in the same region
