Simple GCE setup with Terraform

 

by Carlos Leon

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

Software Engineer at Container Solutions
Carlos is a Colombian nerd working in tech for 8 years. He works as a software engineer at Container Solutions busy with all things Cloud Native and programmable infrastructure in general. He's a passionate hang glider pilot.

6 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!

    • Hello, Satyadeep

      Even though it is possible to run commands in your machine with Terraform like git, npm, apt-get, etc. (via the remote-exec provisioner) this is not a responsibility that you want to give to Terraform since now you’re talking about the configuration of your machines.

      Let Terraform manage your infrastructure since it knows how to do so very well but use a different tool that knows best how to configure your infrastructure (Ansible, SaltStack, Puppet, Chef).

Leave a Reply

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