# Start Training Function

## Overview
This function starts the training process for a Rekognition Custom Labels model. It validates the manifest file, initiates training, monitors the process, and optionally stores metadata about the training job.

## Important: Manual Invocation Only
**This function is designed to be triggered manually, not by S3 events.**

Unlike the `create_manifest` function which is triggered automatically when new XML files are uploaded to S3, this function should be invoked manually when you want to start training a new model version.

## Prerequisites
Before invoking this function, ensure:
1. The manifest file (`manifest.jsonl`) has been created and validated
2. The manifest is located in the correct S3 bucket (`TRAINING_BUCKET`)
3. The Rekognition project has been created

## Configuration
The function reads configuration from:

### Environment Variables (Optional)
- `TRAINING_BUCKET`: S3 bucket containing the training data/manifest
- `PROJECT_ARN`: ARN of the Rekognition Custom Labels project

### SSM Parameters
If the environment variables are not set, the function will attempt to read:
- `/datafy-rekognition-stack/training-bucket`: S3 bucket for training data
- `/datafy-rekognition-stack/project-name`: Rekognition project name (used to construct ARN)

For Supabase integration, these parameters are always read from SSM:
- `/datafy-rekognition-stack/supabase-url`: URL for Supabase integration
- `/datafy-rekognition-stack/supabase-key`: API key for Supabase integration

### Deployment
When deploying with SAM, you can pass the Supabase parameters which will be stored in SSM:

```bash
sam deploy \
  --stack-name datafy-rekognition-stack \
  --parameter-overrides \
    StackName=rekognition \
    Environment=dev \
    SUPABASE_URL=your-supabase-url \
    SUPABASE_KEY=your-supabase-key
```

Alternatively, you can manually create or update these parameters in AWS Systems Manager Parameter Store:

```bash
aws ssm put-parameter \
  --name /datafy-rekognition-stack/supabase-url \
  --value "your-supabase-url" \
  --type String \
  --overwrite

aws ssm put-parameter \
  --name /datafy-rekognition-stack/supabase-key \
  --value "your-supabase-key" \
  --type SecureString \
  --overwrite
```

## How to Invoke
You can invoke this function manually through:

### AWS Console
1. Navigate to the Lambda console
2. Select the `Rekognition-StartTrainingFunction`
3. Click "Test" and create a new test event (an empty JSON object `{}` is sufficient)
4. Click "Test" to run the function

### AWS CLI
```bash
aws lambda invoke \
  --function-name Rekognition-StartTrainingFunction \
  --payload '{}' \
  response.json
```

### AWS SDK
```python
import boto3

lambda_client = boto3.client('lambda')
response = lambda_client.invoke(
    FunctionName='Rekognition-StartTrainingFunction',
    Payload='{}'
)
```

## Process Flow
1. Validates the manifest file in the training bucket
2. Generates a version name based on current timestamp
3. Starts the Rekognition training job
4. Stores metadata in Supabase (if configured)
5. Polls for training completion
6. Evaluates the model when training completes
7. Updates metadata with final status and evaluation results

## Limitations
- The Lambda function has a maximum execution time of 15 minutes (900 seconds)
- Training jobs typically take longer than 15 minutes to complete
- If the function times out, training will continue in AWS Rekognition
- For longer training monitoring, consider implementing a Step Function

## Troubleshooting
If the function fails to start training:
1. Check that the manifest file exists and is valid
2. Verify the correct S3 bucket is specified in environment variables or SSM
3. Ensure the Rekognition project exists and is accessible
4. Check the CloudWatch logs for detailed error messages
5. Verify that the Supabase parameters exist in SSM if using metadata storage 