diff --git a/terraform/modules/ec2/main.tf b/terraform/modules/ec2/main.tf index 6ba3433..2e47067 100644 --- a/terraform/modules/ec2/main.tf +++ b/terraform/modules/ec2/main.tf @@ -45,6 +45,37 @@ resource "aws_instance" "mongodb" { volume_type = "gp3" } + user_data = <<-EOF + #!/bin/bash + set -e + + # Update system + yum update -y + + # Add MongoDB repository + 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/ +gpgcheck=1 +enabled=1 +gpgkey=https://pgp.mongodb.com/server-7.0.asc +REPO + + # Install MongoDB + yum install -y mongodb-org + + # Configure MongoDB to listen on all interfaces + sed -i 's/bindIp: 127.0.0.1/bindIp: 0.0.0.0/' /etc/mongod.conf + + # Start and enable MongoDB + systemctl start mongod + systemctl enable mongod + + # Log completion + echo "MongoDB installation completed" > /var/log/mongodb-install.log + EOF + tags = { Name = "MongoDB-${var.environment}" Environment = var.environment diff --git a/terraform/stacks/production/terraform.tfvars b/terraform/stacks/production/terraform.tfvars index 391204e..676039a 100644 --- a/terraform/stacks/production/terraform.tfvars +++ b/terraform/stacks/production/terraform.tfvars @@ -3,5 +3,5 @@ aws_region = "us-west-2" instance_type = "t3.large" key_name = "ashish-test-kp" vpc_id = "vpc-0c4669393c94b8f86" -ami_id = "ami-001fbc42d7df13f78" +ami_id = "ami-075b5421f670d735c" # Amazon Linux 2023 root_volume_size = 20 diff --git a/terraform/stacks/staging/terraform.tfvars b/terraform/stacks/staging/terraform.tfvars index f5e015d..f2f6dfe 100644 --- a/terraform/stacks/staging/terraform.tfvars +++ b/terraform/stacks/staging/terraform.tfvars @@ -3,5 +3,5 @@ aws_region = "us-west-2" instance_type = "t3.small" key_name = "ashish-test-kp" vpc_id = "vpc-0c4669393c94b8f86" -ami_id = "ami-001fbc42d7df13f78" +ami_id = "ami-075b5421f670d735c" # Amazon Linux 2023 root_volume_size = 10