# MySQL Configuration with AWS Integration

This module provides database configuration that supports both local development and AWS deployment scenarios.

## Features

- **AWS Integration**: Retrieves database credentials from AWS Parameter Store and Secrets Manager
- **Environment Support**: Supports multiple environments (dev, qa, production)
- **Fallback Support**: Falls back to local environment variables if AWS is not available
- **Secure**: No hardcoded credentials in the codebase

## AWS Setup

### 1. Environment Variables

Set the following environment variables on your EC2 instance:

```bash
# Required
ENV=dev  # or 'qa' for QA environment

# Optional (defaults provided)
AWS_REGION=ap-southeast-2
```

### 2. AWS Credentials

Ensure your EC2 instance has the appropriate IAM role with permissions to:

- **Secrets Manager**: `secretsmanager:GetSecretValue`
- **Parameter Store**: `ssm:GetParameter`

Example IAM policy:

```json
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetSecretValue"
            ],
            "Resource": [
                "arn:aws:secretsmanager:ap-southeast-2:*:secret:rds!cluster-91f2520d-663f-4a7b-9f72-cc0878bdf354*",
                "arn:aws:secretsmanager:ap-southeast-2:*:secret:rds!cluster-252d62f4-d31e-4fe7-a51e-7f96a6a82e3f*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "ssm:GetParameter"
            ],
            "Resource": [
                "arn:aws:ssm:ap-southeast-2:*:parameter/rdsDetails",
                "arn:aws:ssm:ap-southeast-2:*:parameter/rdsDetailsQA"
            ]
        }
    ]
}
```

### 3. AWS Resources

#### Parameter Store Format
Your SSM parameters should contain comma-separated key=value pairs:
```
host=your-rds-endpoint.amazonaws.com,port=3306,database=auto_clicker
```

#### Secrets Manager Format
Your secrets should contain JSON with username and password:
```json
{
    "username": "your_db_user",
    "password": "your_secure_password"
}
```

## Local Development

For local development, create a `.env` file in the project root:

```env
# Local development database configuration
DB_HOST=localhost
DB_NAME=auto_clicker
DB_USER=auto_clicker_user
DB_PASSWORD=your_local_password
DB_PORT=3306
```

## Environment Configuration

### Development Environment
```bash
export ENV=dev
```
Uses:
- Secret: `rds!cluster-91f2520d-663f-4a7b-9f72-cc0878bdf354`
- Parameter: `rdsDetails`

### QA Environment
```bash
export ENV=qa
```
Uses:
- Secret: `rds!cluster-252d62f4-d31e-4fe7-a51e-7f96a6a82e3f`
- Parameter: `rdsDetailsQA`

## Usage

The configuration is automatically loaded when importing the module:

```python
from mysql.config import MYSQL_CONFIG

# MYSQL_CONFIG will contain the appropriate credentials
# based on the environment and AWS availability
```

## Troubleshooting

### Common Issues

1. **AWS Credentials Not Found**
   - Ensure your EC2 instance has the correct IAM role
   - Check that the role has the required permissions
   - Verify the AWS region is correct

2. **Parameter/Secret Not Found**
   - Verify the parameter/secret names match your AWS resources
   - Check the environment variable `ENV` is set correctly

3. **Permission Denied**
   - Review the IAM policy attached to your EC2 instance role
   - Ensure the resource ARNs match your actual AWS resources

### Debug Mode

To enable debug output, check the console when the module loads:
- ✅ indicates successful AWS credential retrieval
- ⚠️ indicates fallback to local environment variables

## Dependencies

- `boto3`: AWS SDK for Python
- `python-dotenv`: Environment variable management

Install with:
```bash
pip install boto3 python-dotenv
```