On the subject of learning Terraform
Tags: terraform, googlecloud
Published: 2019-10-15 07:14:45

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

Now I'm playing with [Terraform](https://www.terraform.io/). And by playing, I mean I spent the afternoon trying to work out if I could slap some [IaC](https://en.wikipedia.org/wiki/Infrastructure_as_code) 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](https://github.com/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](https://github.com/hashicorp/terraform/issues/15608). It would be amazing if this ever lands. 

**Update**: turns out there's [GoogleCloudPlatform/terraformer](https://github.com/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](https://www.terraform.io/docs/providers/google/r/cloud_run_service.html#example-usage-cloud-run-service-basic) 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](https://github.com/GoogleCloudPlatform/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](https://glasnt.com/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.