Skip to content

Instantly share code, notes, and snippets.

@mildwonkey
Last active April 28, 2020 17:59
Show Gist options
  • Save mildwonkey/54ce5cf5283d9ea982d952e3c04a5956 to your computer and use it in GitHub Desktop.
Save mildwonkey/54ce5cf5283d9ea982d952e3c04a5956 to your computer and use it in GitHub Desktop.
Provider source usage examples

Provider Source Usage Examples

Config with no changes required

Example:

# with no provider configuration at all, terraform will identify this by the 
# it's resource type "aws" and assume that the user meant "hashicorp/aws" 
resource "aws_instance" {
  # resource config
}

Example:

# With no required_providers entries, terraform will identify the provider type as "aws" 
# and assume the user meant "hashicorp/aws"
provider "aws" {
  # provider config
}

resource "aws_instance" {
  # resource config
}

Example:

terraform {
    required_providers {
        # the original syntax (version string) is still supported
        # with no "source" attribute, terraform assumes the user meant "hashicorp/aws"
        aws = "2.0"
     }
     
}

# terraform maps the resource type name "aws" to the map key "aws" and uses "hashicorp/aws"
resource "aws_instance" "example" {
  # resource config
}

Best practices example

Easy Case

When the localname (the map keys in required_providers) match the provider type, the user doesn't need to specify the provider for resources.

terraform { 
    required_providers {
        google = { 
          # Why? For e.g., perhaps they forked the google provider to enforce security requirements
          source = "tfe.example.com/myorg/google"
        }
        happycloud = { 
          source = "tfe.example.com/myorg/happycloud"
        }
    }
 }
 
 resource "google_thing" "example" {
    # will use tfe.example.com/myorg/google
 }
 
  resource "happycloud_thing" "example" {
    # will use tfe.example.com/myorg/happycloud
 }

More complicated

terraform { 
    required_providers {
        # this "google" entry is entirely optional, since it's a default (HashiCorp) provider,
        # but helps readability.
        google = { 
          # This is short hand for "registry.terraform.io/hashicorp/google"
          # If the host portion is omitted, terraform assumes "registry.terraform.io"
          source = "hashicorp/google"
        }
        google-beta = { 
          source = "hashicorp/google-beta"
        }
    }
 }
 
 resource "google_thing" "example_1" {
    # With no "provider" attribute, this resource will default to hashicorp/google.
 }
 
  resource "google_thing" "example_2" {
    # When the localname (google-beta) does not match the resource type name (google),
    # the "provider" attribute is always required.
    provider = google-beta
 }

Anti-Patterns

Confusing but entirely valid:

terraform { 
    required_providers {
        // This is actively confusing.
        google = { 
          source = "hashicorp/google-beta"
        }
    }
 }
 
 resource "google_thing" "example" {
    # this will use hashicorp/google-beta, 
    # since the localname google matches the resource type name
    # This will confuse everyone. Please let's not.
 }
@apparentlymart
Copy link

This looks good! Just wanted to note that having the provider argument value in quotes is a 0.11-style thing we still support for backward compatibility, but the current canonical syntax would be like this:

  provider = google-beta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment