DEV Community

Katie McLaughlin
Katie McLaughlin

Posted on

On the subject of learning Terraform

Back in the day I was a Puppet user. Then I had the chance to use Ansible.

Now I'm playing with Terraform. And by playing, I mean I spent the afternoon trying to work out if I could slap some IaC on a project. And I have a collection of thoughts.

The default output is πŸ˜™πŸ‘Œ

The fact that the output is detailed and descriptive is amazing.

For example:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Like, as a first-time user, this is so incredibly useful. The fact that I can read the output that's given to me and work out what's going on is amazing.

The errors are.. less useful.

I was stuck for a while trying to work out why I was getting a 404 error:

Error 404: Requested entity was not found.

Turns out I had a zone in my location instead of a region. us-central1-a instead of us-central1. The line number of the error was the line of the resource, so that didn't help much.

Filetypes and formatting

The format type you're looking at in .tf files is "HCL" (Hashicorp Configuration Language). You can get a syntax highlighter for vim over at jvirtanen/vim-hcl

Also, you can auto-format your .tf files with terraform fmt

File scoping

It looks like terraform apply applies anything named .tf in the current folder, even if it's not called main.tf.

This is one thing I tried hard to find but never did: formatting conventions and best practices.

Whee beta providers!

If you've read my dev.to history, you'll know I work a lot around Google Cloud Run. Which is still in beta. Which means I need to specify the beta provider at times.

For example, to import a cloud run service (at the moment):

terraform import -provider=google-beta google_cloud_run_service.default PROJECT_ID/REGION/SERVICE

But then again,

OMG import 😍

The fact there's even an import option at all is amazing. It lets me see the state Terraform thinks things are, and from there I can write my resource manifest.

... I have to manually write my resource manifest 😭

It seems there's a quite old (locked, but not closed) GitHub issue on generating .tf files from imports. It would be amazing if this ever lands.

Update: turns out there's GoogleCloudPlatform/terraformer, which is brew installable, and generates .tf files for you!

brew install terraformer
echo 'provider "google" {}' > init.tf
terraform init
terraformer import google --projects=yourproject --resources=gcs,...

Beta providers again!

One thing I noted is that sample code for Cloud Run doesn't "just work", as the error I'm getting says I need to manually provide the project.

Well, I think to myself, I can submit a Pull Request to the documentation to fix that!

Only to find the "Edit this page" link redirects to a 404.

Because it's a beta provider, and the doc doesn't live in the main provider. It's in the beta provider.

But the docs are automatically generated, using magic-modules, which itself is quite neat, but it's not immediately clear if the beta doc is being generated here, or there's other magic going on. But that's an obstacle for another day.

Yay documentation!

I will say that the docs have been amazing so far for Terraform and GCP. I've been able to learn not only this automation mechanism but also get beyond just the sample code and start creating my own complex resource definitions in just a few hours.

What am I doing wrong?

I often post these sort of musings to my own blog which has the feature of no comments.

However, I'm posting here so if you have any useful helpful suggestions on any of the things I've mentioned, or any other starter tips, to comment below.

Top comments (2)

Collapse
 
david_j_eddy profile image
David J Eddy

Nice article Katie. Your struggle with importing existing resources is real. I was about to recommend https://github.com/dtan4/terraforming but say you found Googles implementation. It really would be nice if Hashi added the import->resource tooling themselves.

I as well have notices a lot of GCP is behind the beta provider. This fits Googles MO. Gmail was beta for over 10 years. :S

Glad you are finding TF interesting. If you have any questions feel free to reach out.

Collapse
 
booyaa profile image
Mark Sta Ana

Thanks for the β€œ terraformer” tip! I love seeing people get excited about it and IaC 😁