# Start Model

This Lambda function starts a trained Rekognition Custom Labels model for inference. It selects the most recently trained model from Supabase and initiates the AWS Rekognition model deployment.

## Functionality

The function performs the following operations:

1. Retrieves the latest successfully trained model from Supabase (`TRAINING_COMPLETED` status)
2. Uses the model version ARN directly from the database (assuming it's already correctly formatted)
3. Starts the model with AWS Rekognition using `start_project_version` API
4. Waits for the model to reach the `RUNNING` state
5. Updates model status in Supabase database using the `model_status` field

## Environment Variables

This function does not use direct environment variables but instead retrieves configuration from SSM Parameter Store:

- `/supabase/url`: URL for the Supabase instance
- `/supabase/anon`: Anonymous API key for Supabase
- `/datafy-rekognition-stack/project-arn`: Project ARN for Rekognition

## Required Project ID

All Rekognition operations require the correct project ID: `1725357683732`. The function includes logic to fix ARNs with incorrect project IDs for both project ARNs and model version ARNs.

## Database Schema

### Supabase Table Fields

The function interacts with the `model_versions` table in Supabase which should have:

- `version_name`: Name of the model version (used as a key for updates)
- `project_version_arn`: ARN of the model in AWS Rekognition
- `status`: Original training status field (maintained for compatibility)
- `model_status`: Field for tracking deployment status (STARTING, RUNNING, etc.)
- `status_message`: Field for storing detailed status messages from AWS
- `training_timestamp`: Timestamp of when training was completed (used for ordering)

### Model Status Values

The function uses these `model_status` values:
- `STARTING`: Model is being started for inference
- `RUNNING`: Model is running and available for inference
- `ERROR`: Error occurred during model startup

This is separate from the `status` field which tracks training status.

## Database Integration

The function integrates with Supabase to:
- Query the latest trained model from `model_versions` table (checks both `status` and `model_status` fields)
- Update model deployment state in the `model_status` field (preserving the original `status` value)
- Store status messages from AWS Rekognition

## Schema Update

Before using this function, the Supabase database schema must be updated to include the `model_status` field. An SQL script is provided in:
`lambda/Rekognition/supabase_schema_update.sql`

Run this script to:
1. Add the `model_status` column
2. Initialize existing records
3. Create an index for performance

## Error Handling

The function includes error handling for:
- Missing or invalid project ARN
- Missing or invalid model version ARN
- Failed model startup
- Supabase connection failures
- Missing database schema fields

## Usage

This function is typically triggered:
- Manually when an inference endpoint is needed
- Through a Step Functions workflow
- Via an API Gateway endpoint

## Troubleshooting

Common issues:
- Model fails to start: Check if the model version exists in AWS and has a TRAINING_COMPLETED status
- ARN validation errors: Ensure the project ID (1725357683732) is correctly appended to ARNs
- Database errors: Ensure the `model_status` column has been added to the table
- Timeout errors: Starting a model may take longer than the Lambda function timeout, but will continue in AWS 