Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple Terraform Providers sequentially

Tags:

terraform

How can I get Terraform 0.10.1 to support two different providers without having to run 'terraform init' every time for each provider?

I am trying to use Terraform to

1) Provision an API server with the 'DigitalOcean' provider

2) Subsequently use the 'Docker' provider to spin up my containers

Any suggestions? Do I need to write an orchestrating script that wraps Terraform?

like image 333
cgiacomi Avatar asked Aug 17 '17 12:08

cgiacomi


People also ask

Can you use multiple providers together in Terraform?

Configuring Multiple AWS providersWe can't write two or more providers with the same name i.e. two AWS providers. If there's any such need the terraform has provided a way to do that which is to use alias argument. Note: A provider block without an alias argument is the default configuration for that provider.

Can multiple Terraform providers be used within a single Terraform configuration file?

The fact that Terraform is not tied to a specific infrastructure or cloud provider makes it a powerful tool in multi-provider deployments. You are able to manage all resources using the same set of configuration files, sharing variables or defining dependencies between resources across providers.


1 Answers

Terraform's current design struggles with creating "multi-layer" architectures in a single configuration, due to the need to pass dynamic settings from one provider to another:

resource "digitalocean_droplet" "example" {
  # (settings for a machine running docker)
}

provider "docker" {
  host = "tcp://${digitalocean_droplet.example.ipv4_address_private}:2376/"
}

As you saw in the documentation, passing dynamic values into provider configuration doesn't fully work. It does actually partially work if you use it with care, so one way to get this done is to use a config like the above and then solve the "chicken-and-egg" problem by forcing Terraform to create the droplet first:

$ terraform plan -out=tfplan -target=digitalocean_droplet.example

The above will create a plan that only deals with the droplet and any of its dependencies, ignoring the docker resources. Once the Docker droplet is up and running, you can then re-run Terraform as normal to complete the setup, which should then work as expected because the Droplet's ipv4_address_private attribute will then be known. As long as the droplet is never replaced, Terraform can be used as normal after this.

Using -target is fiddly, and so the current recommendation is to split such systems up into multiple configurations, with one for each conceptual "layer". This does, however, require initializing two separate working directories, which you indicated in your question that you didn't want to do. This -target trick allows you to get it done within a single configuration, at the expense of an unconventional workflow to get it initially bootstrapped.

like image 143
Martin Atkins Avatar answered Oct 12 '22 08:10

Martin Atkins