Terraform: Provisioning a GCP VM instance with ssh key access

To provision a GCP VM instance with ssh key access, add this section to your google_compute_instance resource in your *.tf defining your instance:

metadata = {
ssh-keys = "YOUR_USERNAME:${file("~/.ssh/YOUR_SSH_KEY.pub")}"
}

where username is your GCP username that you wish to logon to the instance with using the key.

Create a new ssh key if needed with:

ssh-keygen -t rsa

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.