Version 1.0.0 - ami-0166bfe4e72e26222
# Database Stack AMI **MongoDB + InfluxDB + MySQL** - A plug-and-play multi-database stack for AWS EC2 with persistent EBS storage. ## Overview This AMI provides a production-ready database stack with three popular databases: | Database | Version | Port | Use Case | |----------|---------|------|----------| | **MongoDB** | 7.x | 27017 | Document/NoSQL database | | **InfluxDB** | 2.x | 8086 | Time-series database | | **MySQL** | 8.x | 3306 | Relational database | All databases share a single EBS volume for persistent storage, making it easy to backup, snapshot, and manage your data. ## Requirements ### Instance Sizing | Workload | Instance Type | vCPU | RAM | Notes | |----------|--------------|------|-----|-------| | Development | t3.small | 2 | 2 GB | Light testing | | Small Production | t3.medium | 2 | 4 GB | Recommended minimum | | Medium Production | t3.large | 2 | 8 GB | Better performance | | Large Production | t3.xlarge+ | 4+ | 16+ GB | Heavy workloads | ### Storage - **Minimum EBS**: 20 GB (gp3) - **Recommended**: 50-100 GB+ depending on data volume - **Type**: gp3 for best price/performance ### Network (Security Group) | Port | Protocol | Source | Description | |------|----------|--------|-------------| | 22 | TCP | Your IP | SSH access | | 27017 | TCP | Your App/VPC | MongoDB | | 8086 | TCP | Your App/VPC | InfluxDB | | 3306 | TCP | Your App/VPC | MySQL | ⚠️ **Security Note**: Never expose database ports (27017, 8086, 3306) to 0.0.0.0/0 in production! ## Building the AMI ### 1. Launch Base Instance ```bash # Launch Ubuntu 24.04 instance (t3.medium recommended) # Attach a secondary EBS volume for testing ``` ### 2. Upload Scripts ```bash # From your local machine scp -i your-key.pem setup-dbstack.sh configure-dbstack.sh cleanup-for-ami.sh \ first-boot.sh dbstack-firstboot.service dbstack-cli \ ubuntu@:/tmp/ ``` ### 3. Run Setup ```bash # On the instance sudo bash /tmp/setup-dbstack.sh ``` ### 4. Test Configuration (Optional) ```bash # Attach an EBS volume first, then: sudo /opt/dbstack/configure-dbstack.sh # Verify everything works dbstack-cli status # Test connections docker exec dbstack-mongodb mongosh --eval "db.runCommand({ ping: 1 })" docker exec dbstack-mysql mysql -u root -p -e "SELECT VERSION();" ``` ### 5. Clean Up for AMI ```bash sudo /opt/dbstack/cleanup-for-ami.sh ``` ### 6. Create AMI 1. Stop the instance (do not terminate) 2. AWS Console → EC2 → Instances → Select instance 3. Actions → Image and templates → Create image 4. Name: `dbstack-mongodb-influxdb-mysql-v1.0` 5. Wait for AMI to be available ## Customer Usage ### Option 1: Interactive Setup 1. **Launch Instance** from the AMI 2. **Attach EBS Volume** (20 GB+ recommended) 3. **SSH and Configure**: ```bash sudo /opt/dbstack/configure-dbstack.sh ``` 4. Follow the prompts to set passwords and configure databases ### Option 2: Automated Setup (User-Data) Launch with this JSON user-data for fully automated configuration: ```json { "mongodb": { "root_user": "admin", "root_password": "YourSecurePassword123!", "database": "myapp", "app_user": "appuser", "app_password": "AppUserPassword456!" }, "influxdb": { "org": "mycompany", "bucket": "metrics", "user": "admin", "password": "InfluxPassword789!" }, "mysql": { "root_password": "MySQLRootPass321!", "database": "myapp", "user": "appuser", "password": "MySQLUserPass654!" }, "host": "your-domain.com" } ``` **Note**: All password fields are required. The `host` field is optional (defaults to public IP). ### CloudFormation Template ```yaml AWSTemplateFormatVersion: '2010-09-09' Description: Database Stack Deployment Parameters: InstanceType: Type: String Default: t3.medium KeyName: Type: AWS::EC2::KeyPair::KeyName VolumeSize: Type: Number Default: 50 MongoRootPassword: Type: String NoEcho: true MongoAppPassword: Type: String NoEcho: true InfluxPassword: Type: String NoEcho: true MySQLRootPassword: Type: String NoEcho: true MySQLAppPassword: Type: String NoEcho: true Resources: DBStackInstance: Type: AWS::EC2::Instance Properties: ImageId: ami-xxxxxxxxx # Your AMI ID InstanceType: !Ref InstanceType KeyName: !Ref KeyName SecurityGroups: - !Ref DBStackSecurityGroup UserData: Fn::Base64: !Sub | { "mongodb": { "root_user": "admin", "root_password": "${MongoRootPassword}", "database": "myapp", "app_user": "appuser", "app_password": "${MongoAppPassword}" }, "influxdb": { "org": "myorg", "bucket": "metrics", "user": "admin", "password": "${InfluxPassword}" }, "mysql": { "root_password": "${MySQLRootPassword}", "database": "myapp", "user": "appuser", "password": "${MySQLAppPassword}" } } DataVolume: Type: AWS::EC2::Volume Properties: AvailabilityZone: !GetAtt DBStackInstance.AvailabilityZone Size: !Ref VolumeSize VolumeType: gp3 Tags: - Key: Name Value: dbstack-data DataVolumeAttachment: Type: AWS::EC2::VolumeAttachment Properties: Device: /dev/xvdf InstanceId: !Ref DBStackInstance VolumeId: !Ref DataVolume DBStackSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Database Stack Security Group SecurityGroupIngress: - IpProtocol: tcp FromPort: 22 ToPort: 22 CidrIp: 0.0.0.0/0 - IpProtocol: tcp FromPort: 27017 ToPort: 27017 CidrIp: 10.0.0.0/8 # VPC only - IpProtocol: tcp FromPort: 8086 ToPort: 8086 CidrIp: 10.0.0.0/8 - IpProtocol: tcp FromPort: 3306 ToPort: 3306 CidrIp: 10.0.0.0/8 Outputs: InstanceIP: Value: !GetAtt DBStackInstance.PublicIp MongoDBURI: Value: !Sub mongodb://${DBStackInstance.PublicIp}:27017 InfluxDBURL: Value: !Sub http://${DBStackInstance.PublicIp}:8086 MySQLHost: Value: !Sub ${DBStackInstance.PublicIp}:3306 ``` ## Management Commands ```bash # Service management dbstack-cli status # Show status of all databases dbstack-cli start # Start all services dbstack-cli stop # Stop all services dbstack-cli restart # Restart all services # Logs dbstack-cli logs # All container logs dbstack-cli logs mongodb # MongoDB logs only dbstack-cli logs influxdb # InfluxDB logs only dbstack-cli logs mysql # MySQL logs only # Configuration dbstack-cli info # Show configuration info sudo dbstack-cli credentials # Show passwords and connection strings # Maintenance dbstack-cli update # Pull latest images and restart sudo dbstack-cli backup # Backup all databases sudo dbstack-cli restore # Restore from backup # Database shells dbstack-cli mongo-shell # MongoDB shell dbstack-cli mysql-shell # MySQL shell dbstack-cli influx-shell # InfluxDB CLI ``` ## Connection Examples ### MongoDB ```python # Python (pymongo) from pymongo import MongoClient # Using root credentials client = MongoClient("mongodb://admin:password@your-ip:27017") # Using app credentials client = MongoClient("mongodb://appuser:password@your-ip:27017/mydb") db = client.mydb ``` ```javascript // Node.js const { MongoClient } = require('mongodb'); const client = new MongoClient('mongodb://appuser:password@your-ip:27017/mydb'); ``` ### InfluxDB ```python # Python (influxdb-client) from influxdb_client import InfluxDBClient client = InfluxDBClient( url="http://your-ip:8086", token="your-api-token", org="myorg" ) ``` ```bash # CLI influx write -b mybucket -o myorg -t your-token \ 'temperature,location=office value=72.5' ``` ### MySQL ```python # Python (mysql-connector) import mysql.connector conn = mysql.connector.connect( host="your-ip", port=3306, user="appuser", password="password", database="mydb" ) ``` ```bash # CLI mysql -h your-ip -P 3306 -u appuser -p mydb ``` ## Backup & Recovery ### Manual Backup ```bash # Create backup of all databases sudo dbstack-cli backup # Backups stored in: /mnt/dbstack-data/backups/ ``` ### EBS Snapshots (Recommended) For production, use EBS snapshots for point-in-time recovery: ```bash # Stop services before snapshot for consistency dbstack-cli stop # Create snapshot via AWS Console or CLI aws ec2 create-snapshot --volume-id vol-xxxxx --description "dbstack-backup-$(date +%Y%m%d)" # Start services dbstack-cli start ``` ### Restore from Backup ```bash sudo dbstack-cli restore # Follow prompts to select backup timestamp ``` ## Architecture ``` ┌─────────────────────────────────────────────────────┐ │ EC2 Instance │ │ ┌─────────────────────────────────────────────────┐ │ │ │ Docker Network │ │ │ │ ┌───────────┐ ┌───────────┐ ┌───────────────┐ │ │ │ │ │ MongoDB │ │ InfluxDB │ │ MySQL │ │ │ │ │ │ :27017 │ │ :8086 │ │ :3306 │ │ │ │ │ └─────┬─────┘ └─────┬─────┘ └───────┬───────┘ │ │ │ └────────┼─────────────┼───────────────┼──────────┘ │ │ │ │ │ │ │ └─────────────┼───────────────┘ │ │ │ │ │ ┌──────────────────────┼──────────────────────────┐ │ │ │ /mnt/dbstack-data (EBS Volume) │ │ │ │ ┌─────────┐ ┌─────────────┐ ┌─────────────┐ │ │ │ │ │ mongodb │ │ influxdb │ │ mysql │ │ │ │ │ └─────────┘ └─────────────┘ └─────────────┘ │ │ │ └─────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────┘ ``` ## Troubleshooting ### Service Won't Start ```bash # Check Docker status systemctl status docker # Check container logs docker logs dbstack-mongodb docker logs dbstack-influxdb docker logs dbstack-mysql # Check EBS mount df -h /mnt/dbstack-data ``` ### Connection Refused 1. Check security group allows the port 2. Verify UFW rules: `sudo ufw status` 3. Check service is running: `dbstack-cli status` ### Out of Disk Space ```bash # Check disk usage dbstack-cli status # Extend EBS volume in AWS Console, then: sudo growpart /dev/xvdf 1 sudo resize2fs /dev/xvdf ``` ### Reset Everything ```bash # Complete reset (DESTROYS ALL DATA) sudo /opt/dbstack/cleanup-for-ami.sh # Then reconfigure: sudo /opt/dbstack/configure-dbstack.sh ``` ## Security Recommendations 1. **Use VPC**: Keep databases in private subnet 2. **Security Groups**: Restrict ports to application servers only 3. **Strong Passwords**: Use 16+ character passwords 4. **Enable Encryption**: Use encrypted EBS volumes 5. **Regular Backups**: Automate EBS snapshots 6. **Update Regularly**: `dbstack-cli update` for security patches ## Support - **Logs**: `/var/log/dbstack-firstboot.log` - **Credentials**: `sudo cat /opt/dbstack/.credentials` - **Configuration**: `/opt/dbstack/config-info.txt` ## Version History - **1.0.0**: Initial release with MongoDB 7, InfluxDB 2, MySQL 8 --- **Built for AWS Marketplace** | Ubuntu 24.04 | Docker-based