To set up SSH with Google Cloud Platform (GCP) for use with Terraform, follow these steps:
-
Generate SSH Keys: If you don't already have an SSH key pair, generate one using the following command:
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
-
Add SSH Key to GCP: Add your public SSH key to your GCP project metadata. This allows you to SSH into instances created by Terraform.
gcloud compute project-info add-metadata --metadata ssh-keys="$(whoami):$(cat ~/.ssh/id_rsa.pub)"
-
Configure Terraform: Ensure your
variables.tf
file includes thessh_user
variable:variable "ssh_user" { description = "The SSH username to access the instance" type = string default = "your-username" # Replace with your SSH username }
-
Reference SSH Key in Terraform: In your
main.tf
file, configure thegoogle_compute_instance
resource to use the SSH key (in source code):resource "google_compute_instance" "rails_app" { metadata = { ssh-keys = "${var.ssh_user}:${file("~/.ssh/id_rsa.pub")}" } }
-
SSH into the Instance: Once the instance is created, you can SSH into it using:
gcloud compute ssh your-username@instance-name --zone=us-central1-a
Replace your-username
and instance-name
with your actual SSH username and instance name.
Or you can use the ssh
command directly:
ssh your-username@instance-ip
The instance-ip can be found in the config/deploy.yml
which is updated after the Terraform setup done via terraform-gcloud/bin/stand-up
.