diff --git a/terraform/modules/ec2/main.tf b/terraform/modules/ec2/main.tf index 2e47067..af72cef 100644 --- a/terraform/modules/ec2/main.tf +++ b/terraform/modules/ec2/main.tf @@ -45,35 +45,109 @@ resource "aws_instance" "mongodb" { volume_type = "gp3" } + ebs_block_device { + device_name = "/dev/sdf" + volume_size = var.mongodb_data_volume_size + volume_type = "gp3" + encrypted = true + delete_on_termination = false + } + user_data = <<-EOF #!/bin/bash - set -e + exec > >(tee /var/log/user-data.log) 2>&1 + set -x + + echo "Starting MongoDB installation at $(date)" # Update system yum update -y - # Add MongoDB repository + # Setup MongoDB data volume + echo "Setting up MongoDB data volume" + + # Wait for the EBS volume to be available + sleep 10 + + # Identify the device (it might be /dev/sdf, /dev/xvdf, or /dev/nvme1n1 depending on instance type) + DATA_DEVICE="" + if [ -e /dev/nvme1n1 ]; then + DATA_DEVICE="/dev/nvme1n1" + elif [ -e /dev/xvdf ]; then + DATA_DEVICE="/dev/xvdf" + elif [ -e /dev/sdf ]; then + DATA_DEVICE="/dev/sdf" + fi + + if [ -z "$DATA_DEVICE" ]; then + echo "ERROR: Could not find the MongoDB data volume" + exit 1 + fi + + echo "Found data device: $DATA_DEVICE" + + # Format the volume with XFS (recommended for MongoDB) + mkfs.xfs $DATA_DEVICE + + # Create mount point + mkdir -p /data/mongodb + + # Mount the volume + mount $DATA_DEVICE /data/mongodb + + # Add to fstab for persistent mounting + UUID=$(blkid -s UUID -o value $DATA_DEVICE) + echo "UUID=$UUID /data/mongodb xfs defaults,nofail 0 2" >> /etc/fstab + + # Set ownership to mongodb user (will be created during installation) + echo "MongoDB data volume mounted at /data/mongodb" + + # Add MongoDB repository (using Amazon Linux 2 repo which is compatible) cat > /etc/yum.repos.d/mongodb-org-7.0.repo <<'REPO' [mongodb-org-7.0] name=MongoDB Repository -baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/7.0/x86_64/ +baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/7.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://pgp.mongodb.com/server-7.0.asc REPO + echo "MongoDB repository added" + # Install MongoDB yum install -y mongodb-org - # Configure MongoDB to listen on all interfaces + if [ $? -ne 0 ]; then + echo "ERROR: Failed to install MongoDB" + exit 1 + fi + + echo "MongoDB installed successfully" + + # Set correct ownership for MongoDB data directory + chown -R mongod:mongod /data/mongodb + + # Configure MongoDB to listen on all interfaces and use the new data directory sed -i 's/bindIp: 127.0.0.1/bindIp: 0.0.0.0/' /etc/mongod.conf + sed -i 's|dbPath: /var/lib/mongo|dbPath: /data/mongodb|' /etc/mongod.conf + + echo "MongoDB configured to listen on all interfaces and use /data/mongodb for data" # Start and enable MongoDB systemctl start mongod systemctl enable mongod - # Log completion - echo "MongoDB installation completed" > /var/log/mongodb-install.log + # Verify MongoDB is running + sleep 5 + systemctl is-active mongod + + if [ $? -eq 0 ]; then + echo "MongoDB installation completed successfully at $(date)" + else + echo "ERROR: MongoDB service failed to start" + systemctl status mongod + exit 1 + fi EOF tags = { diff --git a/terraform/modules/ec2/variables.tf b/terraform/modules/ec2/variables.tf index 4c8710a..d9680ec 100644 --- a/terraform/modules/ec2/variables.tf +++ b/terraform/modules/ec2/variables.tf @@ -38,3 +38,9 @@ variable "root_volume_size" { type = number default = 8 } + +variable "mongodb_data_volume_size" { + description = "MongoDB data volume size in GB" + type = number + default = 20 +} diff --git a/terraform/stacks/production/terraform.tfvars b/terraform/stacks/production/terraform.tfvars index 676039a..9f1f0b8 100644 --- a/terraform/stacks/production/terraform.tfvars +++ b/terraform/stacks/production/terraform.tfvars @@ -4,4 +4,4 @@ instance_type = "t3.large" key_name = "ashish-test-kp" vpc_id = "vpc-0c4669393c94b8f86" ami_id = "ami-075b5421f670d735c" # Amazon Linux 2023 -root_volume_size = 20 +root_volume_size = 8 diff --git a/terraform/stacks/staging/terraform.tfvars b/terraform/stacks/staging/terraform.tfvars index f2f6dfe..5f79e89 100644 --- a/terraform/stacks/staging/terraform.tfvars +++ b/terraform/stacks/staging/terraform.tfvars @@ -4,4 +4,4 @@ instance_type = "t3.small" key_name = "ashish-test-kp" vpc_id = "vpc-0c4669393c94b8f86" ami_id = "ami-075b5421f670d735c" # Amazon Linux 2023 -root_volume_size = 10 +root_volume_size = 8