Here’s a detailed explanation of how to launch a new Compute Engine instance in GCP using the Console, gcloud CLI, and Terraform:
1. Using GCP Console:
a. Go to the GCP Console: https://console.cloud.google.com/
b. Navigate to Compute Engine > VM instances.
c. Click on the “Create” button to start creating a new instance.
d. Fill in the required fields such as instance name, region, zone, and machine type.
e. Choose a boot disk by clicking on the “Change” button under the “Boot disk” section. Select the desired OS image and disk size.
f. Configure additional settings like network, external IP, and firewall rules, as required.
g. Click the “Create” button to launch the instance.
2. Using gcloud CLI:
First, install the Google Cloud SDK (https://cloud.google.com/sdk/docs/install) if you haven’t already.
a. Open the terminal (Command Prompt or PowerShell on Windows, Terminal on macOS or Linux).
b. Authenticate with your Google Cloud account using the following command:
gcloud auth login
c. Set your GCP project:
gcloud config set project PROJECT_ID
d. Launch a new instance with the desired configuration:
gcloud compute instances create INSTANCE_NAME \
--image-family IMAGE_FAMILY \
--image-project IMAGE_PROJECT \
--machine-type MACHINE_TYPE \
--boot-disk-size DISK_SIZE_GB \
--zone ZONE \
--tags TAGS
Replace INSTANCE_NAME, IMAGE_FAMILY, IMAGE_PROJECT, MACHINE_TYPE, DISK_SIZE_GB, ZONE, and TAGS with appropriate values.
3. Using Terraform:
First, install Terraform (https://learn.hashicorp.com/tutorials/terraform/install-cli) if you haven’t already.
a. Create a new directory for your Terraform configuration:
mkdir gcp-instance
cd gcp-instance
b. Create a main.tf
file with the following content:
provider "google" {
credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_JSON>")
project = "<YOUR_PROJECT_ID>"
region = "us-central1"
}
resource "google_compute_instance" "default" {
name = "terraform-instance"
machine_type = "n1-standard-1"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
network = "default"
access_config {
// Ephemeral external IP
}
}
}
Replace <PATH_TO_YOUR_SERVICE_ACCOUNT_JSON>
and <YOUR_PROJECT_ID>
with appropriate values.
c. Initialize Terraform:
terraform init
d. Review the execution plan:
terraform plan
e. Apply the changes to create the instance:
terraform apply
Leave a Reply