Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 80 additions & 6 deletions terraform/modules/ec2/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 6 additions & 0 deletions terraform/modules/ec2/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion terraform/stacks/production/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion terraform/stacks/staging/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -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