# Stop Model

This Lambda function stops a running Rekognition Custom Labels model. It selects the currently running model from Supabase and initiates the model shutdown process in AWS Rekognition.

## Functionality

The function performs the following operations:

1. Retrieves the currently running model from Supabase (checks both `model_status` and `status` fields)
2. Uses the model version ARN directly from the database (assuming it's already correctly formatted)
3. Updates the model status to `STOPPING` in Supabase
4. Stops the model with AWS Rekognition using `stop_project_version` API
5. Updates model status to `STOPPED` in Supabase once complete

## 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, STOPPING, 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:
- `STOPPING`: Model is being stopped
- `STOPPED`: Model has been successfully stopped
- `ERROR`: Error occurred during the stopping process

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

## Database Integration

The function integrates with Supabase to:
- Query running models 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`

## Error Handling

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

## Usage

This function is typically triggered:
- Manually when inference is no longer needed (to avoid ongoing costs)
- Through a Step Functions workflow
- Via an API Gateway endpoint
- On a schedule to manage costs

## Troubleshooting

Common issues:
- No running model found: Verify a model exists and has been started
- 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
- AWS API errors: Check CloudWatch logs for specific error messages from Rekognition 