# Check Manifest Freshness Function

## Purpose

This Lambda function verifies if the manifest file in the S3 bucket is fresh enough (updated within the specified timeframe) before proceeding with the Rekognition training workflow. This check prevents training jobs from running with outdated data.

## Function Details

The function performs the following steps:
1. Retrieves the S3 bucket name from the environment or SSM Parameter Store
2. Gets the manifest file's last modified date using S3 head_object
3. Calculates the age of the manifest file in hours
4. Determines if the file is fresh based on the configured maximum age
5. Returns a detailed response with freshness information

## Inputs

### Environment Variables
- `TRAINING_BUCKET`: The S3 bucket containing the manifest file
- `MANIFEST_KEY`: The key of the manifest file in the bucket (default: `manifest.jsonl`)
- `MAX_AGE_HOURS`: Maximum age of the manifest file in hours to be considered fresh (default: 168, which is 7 days)

### Lambda Event
The function does not require any specific input in the Lambda event.

## Output

The function returns a JSON object with the following structure:

```json
{
  "statusCode": 200,
  "manifest_fresh": true|false,
  "freshness_info": {
    "is_fresh": true|false,
    "last_modified": "ISO-formatted-timestamp",
    "age_hours": 123.45,
    "age_days": 5.14,
    "max_age_hours": 168,
    "max_age_days": 7.0
  }
}
```

In case of errors, it returns:

```json
{
  "statusCode": 500,
  "manifest_fresh": false,
  "error": "error message"
}
```

## Integration with State Machine

This function is the first step in the Rekognition training state machine. The state machine checks the `manifest_fresh` flag to determine whether to proceed with training:

- If `manifest_fresh` is `true`, the workflow continues to the next steps
- If `manifest_fresh` is `false`, the workflow terminates with a message indicating the manifest is too old

## Error Handling

The function handles the following error scenarios:
- S3 access errors
- Missing manifest file (treated as not fresh)
- Configuration errors
- Parameter retrieval errors

In all error cases, the function defaults to `manifest_fresh: false` to prevent training with potentially problematic data.

## Customization

The freshness threshold can be customized by modifying the `MAX_AGE_HOURS` environment variable. The default is 7 days (168 hours), which aligns with the weekly training schedule.

## Logging

The function logs detailed information about:
- The manifest file's last modified timestamp
- The age of the manifest file in hours and days
- The freshness determination
- Any errors encountered

Logs can be monitored in CloudWatch. 