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.
- 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.
- 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
- Browse through the registry. Do you see any providers that could help you?