> For the complete documentation index, see [llms.txt](https://data-engineering-zoomcamp-2025-t.gitbook.io/tinker0425/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://data-engineering-zoomcamp-2025-t.gitbook.io/tinker0425/module-1/1.3-setting-up-infrastructure-on-gcp-with-terraform/1.3.2-terraform-basics.md).

# 1.3.2 - Terraform Basics

Last updated Jan 25, 2025

Youtube Video | \~29 min

{% embed url="<https://www.youtube.com/watch?v=Y2ux7gq3Z0o&list=PL3MmuxUbc_hJed7dXYoJw8DoCuVHhGEQb&index=12&pp=iAQB>" %}
<https://www.youtube.com/watch?v=Y2ux7gq3Z0o&list=PL3MmuxUbc_hJed7dXYoJw8DoCuVHhGEQb&index=12&pp=iAQB>
{% endembed %}

:writing\_hand: This video we set up a service account in our Google Cloud. We will then create a Main.tf file to configure our Google Cloud Infrastructure.

{% hint style="info" %}
We create a keys/my-creds.json with private information. Be sure to not push this to github or anywhere public. Use a terraform gitignore to be safe <https://github.com/github/gitignore/blob/main/Terraform.gitignore>
{% endhint %}

### :scroll: Main.tf

**Terraform provider for Google Cloud** - The Google Cloud provider is used to configure your Google Cloud infrastructure.

{% embed url="<https://registry.terraform.io/providers/hashicorp/google/latest/docs>" %}
<https://registry.terraform.io/providers/hashicorp/google/latest/docs>
{% endembed %}

To install this provider, copy and paste this code into your Terraform configuration. Then, run `terraform init`.&#x20;

**Terraform 0.13+**

```bash
terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "6.17.0"
    }
  }
}

provider "google" {
  # Configuration options
  provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
}
}
```

Where `my-project-id` comes from your GCP Dashboard.&#x20;

:black\_medium\_square: Terminal you can now run `terraform init`

**+ Add a new bucket in Google cloud storage service (GCS) -** in the same script append

{% embed url="<https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket.html#example-usage---life-cycle-settings-for-storage-bucket-objects>" %}

```sh
resource "google_storage_bucket" "auto-expire" {
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
  }

  lifecycle_rule {
    condition {
      age = 1
    }
    action {
      type = "AbortIncompleteMultipartUpload"
    }
  }
}
```

Where `auto-expire & name` need to be changed to a unique value, where name needs to be globally unique

{% hint style="info" %}
:eyes: Note in 1.3.3 we will create variables for these inputs
{% endhint %}

:black\_medium\_square: Terminal you can now run `terraform plan` and then `terraform apply`

:eyes: We can also generate the proposed changes and auto-executing the plan by using `terraform apply -auto-approve`&#x20;

You should now see this bucket created on your GCP

:black\_medium\_square: Terminal you can now run `terraform destroy` once you are done


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://data-engineering-zoomcamp-2025-t.gitbook.io/tinker0425/module-1/1.3-setting-up-infrastructure-on-gcp-with-terraform/1.3.2-terraform-basics.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
