Using multiple providers (2024)

expected timerequirements
120 minutesA 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.

  • Azure: Maybe you’ve got resources running on Azure and would like to add Cloudflare as a CDN.
  • GCP: You can combine GCP resources with Docker running locally.

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 networkresource "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

  • In the Terraform code that you have, add a checkly resource to monitor your instance. Use references to named values to dynamically configure checkly.
  • If time permits it, experiment with the uptime robot status page.

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.

Using multiple providers (1)

  • Create an instance with a public IP.
  • Install apache
  • Find an image and create the container. Use the image: robertdebock/mirror:latest. Expose a unique port.
  • Set the env (enviroment variable) URL to point to the IP-address of the GCP instance.

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

Using multiple providers (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5980

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.