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:
- 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
- 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.
- 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.
- 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.
- Monitor instance resources:
Use tools like top
, htop
, or vmstat
to monitor CPU, memory, and other resource usage on the instance.
- Configure the instance to start services automatically:
Use systemd or init scripts to ensure that your services start automatically when the instance boots up.
- 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
.
Leave a Reply