Providers in Detail
This lesson will cover what Terraform providers are and how they work.
Terraform providers#
A provider in Terraform is a connection that allows Terraform to manage infrastructure using a pre-defined interface. This abstraction means the provider is completely separate from the Terraform engine - which understands how to read state from a provider, read HCL code, and work out how to get to the desired state. This allows you to write a provider to connect to anything that has a programmable way to talk to it. All the writer of a provider has to do for each resource they want Terraform to control is provide Terraform with a way to create it, read it, and delete it.
📝Note: Updates are actually optional as Terraform can always delete and then create the resource if the update is not provided.
Provider model#
Due to the provider model that Terraform employs, providers are not part of the main Terraform source code. They are separate binaries that live in their own repositories and can move at their own speed. This means that if a provider needs to release a bug fix or new feature, they can release it. They do not need to coordinate a release of the main Terraform code base.
Provider examples#
A provider is defined using a provider block. You have already used a provider block in the examples covered so far in this course. A provider block is shown below:
The provider block is very simple. It starts with the keyword “provider” to indicate that this is a
provider block. You then have to give the name of the provider that you are using. In this case we
are using the AWS provider, so we put "aws"
. You then use a {
to open the provider block. You can put all of the configurations you want for the provider inside the provider block. For the AWS provider, the only property that we are configuring is the region. This will be the region in which we are going to create our AWS resources. You then end the provider block with a closing }
.
Downloading AWS provider#
If you connect terminals and run terraform init
, you will notice that Terraform downloads the AWS provider automatically. It will print the following message: Downloading plugin for provider "aws" (hashicorp/AWS) 2.27.0...
. How does this happen, though? Hashicorp (the company that makes Terraform) hosts a registry that
contains the most popular providers. If the provider you use is in the registry, then all you
have to do to use it is define a provider block that sets up that provider and run terraform init
to download it. You can see a full list of providers in the registry on the Hashicorp Terraform
site.
Where Terraform puts the provider#
What happens to the provider then? Terraform simply puts it inside the project where you are currently working in a special folder called .terraform
.
The provider is actually a separate binary that Terraform calls out at a run time to do its work. As an interesting aside, the name of the provider
binary is always in the format terraform-provider-<NAME>_vX.Y.Z
. Terraform uses this convention
to search for providers on your machine so that it knows if you have a particular version of a provider
when you run terraform init
.Based on this, it decides whether or not to download it.