Simple GCE setup with Terraform

Simple GCE setup with Terraform

Here you will find a Terraform configuration file that will create a single virtual machine in the Google Cloud Engine (GCE) platform using Terraform.

I wanted to run a straightforward experiment in a virtual machine in Google Cloud and I couldn’t find a Terraform snippet to get me up & running. The truth is that I had never created a GCE environment myself, so I was not familiar whatsoever with the concepts or how everything was glued together. After some head banging against my keyboard and experimenting with different approaches for the Terraform config file, this is what finally got me in the game:

For this to work you’re going to need:

  1. Service Account Key file.
    This contains your authentication required for Terraform to talk to the Google API.
    You can get it under Google Cloud Platform -> API Manager -> Credentials -> Create Credentials -> Service account key.
    For the Key type field chose JSON. Put the downloaded file right were your Terraform config file is and name it account.json.
  2. Make sure that you have an SSH key setup for your project: Compute Engine -> Metadata -> SSH Keys
  3. Chose a valid machine image and machine type for your VM. Find an up-to-date list of available images here and available types here.
    <rant>This is where I have seen Terraform (or its providers) falling short: they expect you to know what are the valid types for their base image names (already had this issue with not only GCE but also with DigitalOcean, Packet, Triton) so you have to basically create developer accounts all over to get an API token, read through the documentation, get familiar with the concepts, curl all over until you find that name that could be easily listed somewhere else ~_~ </rant>
  4. terraform plan to make sure everything is looking good
  5. terraform apply
  6. Find the IP of the box with terraform show | grep assigned_nat_ip

You’re good to go!

The following two tabs change content below.

Carlos Leon

Latest posts by Carlos Leon (see all)

2 Comments

  1. Great post! Two quick nits: 1) on line 1 of the example: s/var/variable/ , and 2) suggest the use of terraform plan before terraform apply. Cheers!

Leave a Reply

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