This Terraform script allows you to easily create a virtual machine (VM) on Google Compute Engine with the MongoDB image installed. You can customize various aspects of the VM by passing variables to the script, such as the username, password, IP address, machine type, project ID, and more.
Before using this Terraform script, ensure that you have the following prerequisites:
- Terraform installed on your local machine.
- A Google Cloud Platform (GCP) account with the necessary permissions to create and manage resources.
- The GCP credentials JSON file (
service_account.json
) for the account or logged on your Google account using thegcloud cli
on your desktop.
To use this Terraform script, follow these steps:
-
Clone or download this repository to your local machine.
git clone https://github.com/kalogs-c/gce-mongodb-vm.git
-
Navigate to the project directory:
cd gce-mongodb-vm
-
Copy your GCP credentials JSON file (
service_account.json
) into the project directory (ignore if you are using thegcloud cli
and are already logged in with your correct Google account). -
Initialize the Terraform configuration:
terraform init
-
Customize the variables:
Open the
variables.tf
file in a text editor and modify the values according to your requirements or renameterraform.tfvars.example
to justterraform.tfvars
. You can change the following variables:project_id
: Your GCP project ID.region
: The GCP region where the VM should be created (default:us-central1
).zone
: The GCP zone where the VM should be created (default:us-central1-a
).mongo_username
: The desired username for MongoDB.mongo_password
: The desired password for MongoDB.ip_name
: Name of the static IP address to be assigned to the VM, if you already have one create on VPC you can use it.vm_name
: Name of the VMmachine_type
: The desired machine type for the VM (default:e2-medium
).
-
Review the other settings in the Terraform files and make any necessary changes.
-
Deploy the infrastructure by running:
terraform apply
Terraform will now create the VM with MongoDB using the provided variables.
-
Once the deployment is complete, Terraform will display the IP address and other relevant information about the created VM. You can use this information to connect to the VM and interact with MongoDB.
-
To tear down the infrastructure and delete all resources created by Terraform, run:
terraform destroy
Confirm the destruction when prompted.
-
If you want to enable additional features or customize the MongoDB installation further, refer to the official MongoDB documentation for more information.
-
Make sure to keep your
service_account.json
file secure and do not commit it to version control systems.
-
If you encounter any issues or have questions, please open an issue in the repository's issue tracker.
-
For general troubleshooting and assistance with Terraform, consult the official Terraform documentation.