Skip to content

Commit d2dc265

Browse files
Merge pull request #4 from CloudNinjaDev/infra
Enhance MongoDB installation script: add logging, error handling, and…
2 parents 6495862 + 52d3b24 commit d2dc265

4 files changed

Lines changed: 88 additions & 8 deletions

File tree

terraform/modules/ec2/main.tf

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,109 @@ resource "aws_instance" "mongodb" {
4545
volume_type = "gp3"
4646
}
4747

48+
ebs_block_device {
49+
device_name = "/dev/sdf"
50+
volume_size = var.mongodb_data_volume_size
51+
volume_type = "gp3"
52+
encrypted = true
53+
delete_on_termination = false
54+
}
55+
4856
user_data = <<-EOF
4957
#!/bin/bash
50-
set -e
58+
exec > >(tee /var/log/user-data.log) 2>&1
59+
set -x
60+
61+
echo "Starting MongoDB installation at $(date)"
5162
5263
# Update system
5364
yum update -y
5465
55-
# Add MongoDB repository
66+
# Setup MongoDB data volume
67+
echo "Setting up MongoDB data volume"
68+
69+
# Wait for the EBS volume to be available
70+
sleep 10
71+
72+
# Identify the device (it might be /dev/sdf, /dev/xvdf, or /dev/nvme1n1 depending on instance type)
73+
DATA_DEVICE=""
74+
if [ -e /dev/nvme1n1 ]; then
75+
DATA_DEVICE="/dev/nvme1n1"
76+
elif [ -e /dev/xvdf ]; then
77+
DATA_DEVICE="/dev/xvdf"
78+
elif [ -e /dev/sdf ]; then
79+
DATA_DEVICE="/dev/sdf"
80+
fi
81+
82+
if [ -z "$DATA_DEVICE" ]; then
83+
echo "ERROR: Could not find the MongoDB data volume"
84+
exit 1
85+
fi
86+
87+
echo "Found data device: $DATA_DEVICE"
88+
89+
# Format the volume with XFS (recommended for MongoDB)
90+
mkfs.xfs $DATA_DEVICE
91+
92+
# Create mount point
93+
mkdir -p /data/mongodb
94+
95+
# Mount the volume
96+
mount $DATA_DEVICE /data/mongodb
97+
98+
# Add to fstab for persistent mounting
99+
UUID=$(blkid -s UUID -o value $DATA_DEVICE)
100+
echo "UUID=$UUID /data/mongodb xfs defaults,nofail 0 2" >> /etc/fstab
101+
102+
# Set ownership to mongodb user (will be created during installation)
103+
echo "MongoDB data volume mounted at /data/mongodb"
104+
105+
# Add MongoDB repository (using Amazon Linux 2 repo which is compatible)
56106
cat > /etc/yum.repos.d/mongodb-org-7.0.repo <<'REPO'
57107
[mongodb-org-7.0]
58108
name=MongoDB Repository
59-
baseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/7.0/x86_64/
109+
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/7.0/x86_64/
60110
gpgcheck=1
61111
enabled=1
62112
gpgkey=https://pgp.mongodb.com/server-7.0.asc
63113
REPO
64114
115+
echo "MongoDB repository added"
116+
65117
# Install MongoDB
66118
yum install -y mongodb-org
67119
68-
# Configure MongoDB to listen on all interfaces
120+
if [ $? -ne 0 ]; then
121+
echo "ERROR: Failed to install MongoDB"
122+
exit 1
123+
fi
124+
125+
echo "MongoDB installed successfully"
126+
127+
# Set correct ownership for MongoDB data directory
128+
chown -R mongod:mongod /data/mongodb
129+
130+
# Configure MongoDB to listen on all interfaces and use the new data directory
69131
sed -i 's/bindIp: 127.0.0.1/bindIp: 0.0.0.0/' /etc/mongod.conf
132+
sed -i 's|dbPath: /var/lib/mongo|dbPath: /data/mongodb|' /etc/mongod.conf
133+
134+
echo "MongoDB configured to listen on all interfaces and use /data/mongodb for data"
70135
71136
# Start and enable MongoDB
72137
systemctl start mongod
73138
systemctl enable mongod
74139
75-
# Log completion
76-
echo "MongoDB installation completed" > /var/log/mongodb-install.log
140+
# Verify MongoDB is running
141+
sleep 5
142+
systemctl is-active mongod
143+
144+
if [ $? -eq 0 ]; then
145+
echo "MongoDB installation completed successfully at $(date)"
146+
else
147+
echo "ERROR: MongoDB service failed to start"
148+
systemctl status mongod
149+
exit 1
150+
fi
77151
EOF
78152

79153
tags = {

terraform/modules/ec2/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ variable "root_volume_size" {
3838
type = number
3939
default = 8
4040
}
41+
42+
variable "mongodb_data_volume_size" {
43+
description = "MongoDB data volume size in GB"
44+
type = number
45+
default = 20
46+
}

terraform/stacks/production/terraform.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ instance_type = "t3.large"
44
key_name = "ashish-test-kp"
55
vpc_id = "vpc-0c4669393c94b8f86"
66
ami_id = "ami-075b5421f670d735c" # Amazon Linux 2023
7-
root_volume_size = 20
7+
root_volume_size = 8

terraform/stacks/staging/terraform.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ instance_type = "t3.small"
44
key_name = "ashish-test-kp"
55
vpc_id = "vpc-0c4669393c94b8f86"
66
ami_id = "ami-075b5421f670d735c" # Amazon Linux 2023
7-
root_volume_size = 10
7+
root_volume_size = 8

0 commit comments

Comments
 (0)