For all the below, the directory before running terraform init
contains only main.tf
my-terraform-project/
├─ main.tf
This is sufficient as a Terraform config for GCP, assuming you're supplying some environment variables:
- Authentication, e.g. via a
GOOGLE_CREDENTIALS
environment variable. - Default values used for provisioning resources :
GOOGLE_PROJECT
,GOOGLE_REGION
,GOOGLE_ZONE
environment variables
# main.tf
resource "google_storage_bucket" "my-bucket" {
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
# main.tf
terraform {
required_providers {
google = {
version = "~>4.1.0"
}
}
}
resource "google_storage_bucket" "my-bucket" {
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
Config #3: Avoid default of looking for the preferred local name
(explicitly deciding how Terraform chooses which provider to use for a resource)
# main.tf
terraform {
required_providers {
# The official Google provider is using the preferred local name here
# We could change it to hashicorp-google, but then all resources will need a provider meta-argument
google = {
source = hashicorp/google
version = "~>4.1.0"
}
my-org-google = {
source = my-org/google
version = "=1.3.0"
}
}
}
resource "google_storage_bucket" "my-bucket" {
# No provider argument needed here
# We are knowingly using Terraform's default behaviour to look for a provider with a local name matching
# the first word in the resource type at the top of this resource block
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
resource "google_storage_bucket" "bucket-made-with-my-provider" {
provider = my-org-google # local name from `required_providers` block
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
# main.tf
terraform {
required_providers {
google = {
source = hashicorp/google
version = "~>4.1.0"
}
my-org-google = {
source = my-org/google
version = "=1.3.0"
}
}
}
# This is the default provider configuration
provider "google" {
project = "my-project-id"
region = "us-central1"
zone = "us-central1-c"
}
provider "google" {
alias = "europe" # alias needed!
project = "my-project-id"
region = "europe-west2"
zone = "europe-west2-a"
}
resource "google_storage_bucket" "my-US-bucket" {
# No provider argument needed here
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
resource "google_storage_bucket" "my-EU-bucket" {
provider = google.europe # <local name>.<alias>
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
resource "google_storage_bucket" "bucket-made-with-my-provider" {
provider = my-org-google
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
Config #4 : Avoiding using the public Registry by default (setting the hostname in registry source address)
Note: the config below requires the terraform CLI to be configured with credentials to access the specific private registry.
There's a section in the documentation describing how to configure credentials for the CLI here.
# main.tf
terraform {
required_providers {
# The official Google provider is using the preferred local name here
# We could change it to hashicorp-google, but then all resources will need a `provider` meta-argument
google = {
source = hashicorp/google
version = "~>4.1.0"
}
my-org-google = {
source = "app.terraform.io/my-org/google" # Using a private Registry hosted in Terraform Cloud
version = "=1.3.0"
}
}
}
# This is the default provider configuration
provider "google" {
project = "my-project-id"
region = "us-central1"
zone = "us-central1-c"
}
provider "google" {
alias = "europe" # alias needed!
project = "my-project-id"
region = "europe-west2"
zone = "europe-west2-a"
}
resource "google_storage_bucket" "my-US-bucket" {
# No provider argument needed here
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
resource "google_storage_bucket" "my-EU-bucket" {
provider = google.europe
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}
resource "google_storage_bucket" "bucket-made-with-my-provider" {
provider = my-org-google
name = "auto-expiring-bucket"
location = "US"
force_destroy = true
lifecycle_rule {
condition {
age = 3
}
action {
type = "Delete"
}
}