learn-terraform

Course outlines for learning terraform.

View the Project on GitHub robertdebock/learn-terraform

Using multiple providers

expected time requirements
120 minutes A computer with Terraform installed, terraform knowledge.

Goal: Using multiple (related) resources and providers.

Explanation

Sometimes you need to build infrastructure on more than 1 provider.

Terraform can deal with multiple providers and basically becomes an orchestrator.

Howto

Multiple providers

When using multiple providers, it’s time to move the provider block into it’s own file, something like this:

providers.tf:

terraform {
  required_version = ">= 0.13"
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "2.45.1"
    }
    cloudflare = {
      source = "cloudflare/cloudflare"
      version = "2.13.2"
    }
  }
}

provider "azurerm" {
  features {}
}

Referring

In Terraform you can refer to resources:

resource "azurerm_resource_group" "rg" {
  name     = "rf-robertdebock-sbx"
  location = "west europe"
}

# Create a virtual network
resource "azurerm_virtual_network" "vnet" {
    name                = "myTFVnet-robert"
    address_space       = ["10.0.0.0/16"]
    location            = "west europe"
    resource_group_name = azurerm_resource_group.rg.name
}

In the example above, the value of the parameter resource_group_name refers to azurerm_resource_group.rg.name.

You can also refer to resources hosted on different cloud providers:

resource "azurerm_public_ip" "publicip" {
  name                = "myTFPublicIP-robert"
  location            = "west europe"
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Static"
}

resource "cloudflare_record" "foobar" {
  zone_id = "example.com"
  name    = "www"
  value   = azurerm_public_ip.publicip.ip_address
  type    = "A"
  ttl     = 3600
}

In the exampe above, the azurerm_public_ip.publicip.ip_address is used to configure a cloudflare_record.

There are many providers that have something to offer.

Multiple variants of the same provider

Sometimes you may need to create resources on one provider, but using different account or subscriptions. In such a case alias can be used uin the provider block:

provider "azurerm" {
  features {}
}

provider "azurerm" {
  features {}
  alias       = "foo"
  environment = "german"
}

resource "azurerm_resource_group" "default" {
  name     = "something"
  location = "westeurope"
}

resource "azurerm_resource_group" "foo" {
  name     = "someotherthing"
  location = "westeurope"
  provider = azurerm.foo
}

The example above use the german environment and can be use by adding provider = NAME.

Assignment Azure

You need to sign-up to checkly. You can use your github account or email.

Assignment GCP

You are going to create an instance on GCP and a Docker container that proxies traffic to the GCP instance.

architecture

You can find the solution in case you are stuck.

Questions

  1. Browse through the registry. Do you see any providers that could help you?

Solution