GCP Free Learning
,

How to connect to a Compute Engine instance using Google Console/ gcloud CLI and Terraform?

by

GCP Free Learning

Here’s a detailed explanation of how to connect to a Compute Engine instance in GCP using the Console, gcloud CLI, and SSH key setup with Terraform:

1. Using GCP Console:

a. Go to the GCP Console: https://console.cloud.google.com/

b. Navigate to Compute Engine > VM instances.

c. You will see a list of your VM instances. Locate the instance you want to connect to.

d. In the “Connect” column, click on the “SSH” button. The console will open a new browser window and establish an SSH connection to the instance.

2. Using gcloud CLI:

First, make sure you have the Google Cloud SDK (https://cloud.google.com/sdk/docs/install) installed.

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. Connect to your instance using SSH:

gcloud compute ssh INSTANCE_NAME --zone ZONE

Replace INSTANCE_NAME and ZONE with appropriate values.

3. Using Terraform with SSH key setup:

First, make sure you have Terraform installed (https://learn.hashicorp.com/tutorials/terraform/install-cli).

a. Generate an SSH key pair if you don’t have one. You can use ssh-keygen on Linux or macOS, or PuTTYgen on Windows.

b. Create a new directory for your Terraform configuration:

mkdir gcp-instance
cd gcp-instance

c. 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"

  metadata = {
    ssh-keys = "USERNAME:${file("<PATH_TO_PUBLIC_KEY>")}"
  }

  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>, <YOUR_PROJECT_ID>, USERNAME, and <PATH_TO_PUBLIC_KEY> with appropriate values.

d. Initialize Terraform:

terraform init

e. Review the execution plan:

terraform plan

f. Apply the changes to create the instance:

terraform apply

g. After the instance is created, connect to it using your SSH key:

ssh -i <PATH_TO_PRIVATE_KEY> USERNAME@EXTERNAL_IP

Replace <PATH_TO_PRIVATE_KEY>, USERNAME, and EXTERNAL_IP with appropriate values. You can find the external IP in the GCP Console or in the output of the terraform apply command.

Now that you have created the Compute Engine instance and set up the SSH connection using the GCP Console, gcloud CLI, and Terraform, you can interact with the instance using the SSH terminal.

When you’re connected to the instance via SSH, you can perform various tasks such as:

  1. Update the system packages:

For Debian-based systems (e.g., Debian, Ubuntu):

sudo apt-get update && sudo apt-get upgrade

For RHEL-based systems (e.g., CentOS, RHEL):

sudo yum update
  1. Install software or packages:

For Debian-based systems:

sudo apt-get install PACKAGE_NAME

For RHEL-based systems:

sudo yum install PACKAGE_NAME

Replace PACKAGE_NAME with the name of the package you want to install.

  1. Transfer files between your local machine and the instance:

To copy a file from your local machine to the instance:

scp -i <PATH_TO_PRIVATE_KEY> LOCAL_FILE_PATH USERNAME@EXTERNAL_IP:REMOTE_FILE_PATH

To copy a file from the instance to your local machine:

scp -i <PATH_TO_PRIVATE_KEY> USERNAME@EXTERNAL_IP:REMOTE_FILE_PATH LOCAL_FILE_PATH

Replace <PATH_TO_PRIVATE_KEY>, LOCAL_FILE_PATH, USERNAME, EXTERNAL_IP, and REMOTE_FILE_PATH with appropriate values.

  1. Run custom scripts, applications, or services:

Upload and execute your custom script or application on the instance. Make sure to set the appropriate permissions and configurations.

  1. Monitor instance resources:

Use tools like top, htop, or vmstat to monitor CPU, memory, and other resource usage on the instance.

  1. Configure the instance to start services automatically:

Use systemd or init scripts to ensure that your services start automatically when the instance boots up.

  1. Manage firewall rules:

Configure the instance’s firewall rules using ufw (Uncomplicated Firewall) or iptables to control incoming and outgoing network traffic.

Remember to save your work and close the SSH session when you’re done by typing exit or pressing Ctrl+D.

Glance and Google’s Next-Level Gaming Recommendation Engine

Collaborative Excellence: Glance and Google’s Next-Level Gaming Recommendation Engine Introduction: In the dynamic gaming industry, personalized recommendations are crucial for..

gcp_ml gcp_ml

Digits and Google Cloud ML

How Digits is Transforming the Accounting Landscape Using Google Cloud ML The finance and accounting industry is experiencing a significant..

GCP AI GCP AI

Google Cloud’s Vertex AI Model Garden and the Launch of Generative AI Studio

Google Cloud’s Vertex AI Model Garden and the Launch of Generative AI Studio Artificial Intelligence (AI) and Machine Learning (ML)..

GCP AI/ML GCP AI/ML

Google Cloud’s Pioneering AI Models and the Launch of Generative AI Studio

 Google Cloud’s Pioneering AI Models and the Launch of Generative AI Studio Artificial Intelligence (AI) continues to break new grounds,..

GCP App Engine GCP App Engine

How to scale an App Engine application in GCP?

Scaling an App Engine application involves configuring the scaling settings in the app.yaml file and deploying the changes. I’ll provide..

Leave a Reply

Your email address will not be published. Required fields are marked *