Day 57 : Terraform Providers #90DaysofDevops

Day 57 : Terraform Providers #90DaysofDevops

In this article, we will discuss Terraform Providers. First, we will look at the purpose of a provider in Terraform, and how they can be used and referenced. We will then look at some of the available providers with examples.

What Are the Providers in Terraform?

A provider in Terraform is a plugin that enables interaction with an API. This includes Cloud providers and Software-as-a-service providers. The providers are specified in the Terraform configuration code. They tell Terraform which services it needs to interact with.

How many providers does Terraform have?

There are hundreds of available providers that can be used with Terraform, making it a hugely versatile tool. A lot of Terraform’s popularity stems from the fact that it is platform agnostic and can be used so widely, as opposed to languages that may be platform-specific, such as Microsoft Azure ARM templates or Bicep (which interact with the Azure API only).

How to use Terraform Providers?


Once a provider is specified, each provider makes a list of?resources?and?data?types available for use in the Terraform code. These are listed in the documentation which can be found on the?Terraform Registry.

Each provider is released independently from Terraform itself with each version providing additional features and bug fixes. Each provider has its own release cadence. Providers are usually managed by either Hashicorp, the dedicated teams from the company making the provider (e.g. Mongo for the?mongodb provider), or by community groups, users, and volunteers with an interest in the product or platform the provider utilizes.

  • Providers maintained by Hashicorp are marked as ‘official’ on the documentation pages.
  • Providers marked as ‘verified’ are owned by official third-party technology vendors. Vendors are also a member of the HashiCorp Technology Partner Program.
  • Providers marked as ‘community’ are published by members or groups in the Terraform community.

You can develop a provider in the unlikely event that it doesn’t already exist for the infrastructure you are trying to provision using Terraform. Or you can develop it for your product. Providers are written in Go and utilize the Terraform Plugin SDK.

How to Download & Install Terraform Provider?

Terraform providers are downloaded and installed during the?terraform init stage of the Terraform workflow. They can be downloaded from the public registry, or a local registry or mirror. The provider is downloaded, when it’s not present in the .terraform subdirectory. When it is already present, it is not downloaded again. The version number is also checked. Changing the version number in the configuration files would cause that version of the provider to be downloaded.

To save bandwidth and speed up the runtime, however, the use of an optional plugin cache can be specified using? plugin_cache_dir in the CLI configuration file which configures per user CLI behaviors (terraform.rc?or?.terraformrc). E.g.

plugin_cache_dir   = "$HOME/.terraform.d/plugin-cache"        

Plugins are always downloaded in the case of runs in Terraform Cloud or Terraform Enterprise.

Terraform Provider Configuration

Provider configurations should be declared in the root module of your Terraform project. They can be declared in any .tf file. I would recommend either putting them in the main.tf?, creating a?providers.tf?file specifically for the Providers and nothing else, or alternatively a?versions.tf file which would include the?required_providers?block and specify the Terraform version. Any child modules receive their provider configuration from the root module.

Terraform Provider Block Example

Providers are configured using a?providers?block. Some providers require certain information to be configured such as endpoint URLs or cloud regions. Depending on which provider you need to use, the associated documentation should detail which settings you need to configure along with examples. To find this for each provider, click on the ‘use provider’ button top right of the documentation page.

In the example shown above for the Microsoft Azure provider?azurerm?, the provider source and version are specified in the?required_providers?section. The?providers?block then contains the configuration options required by the provider. For example,?azurerm?includes?features?,?clientid?,?subscription_id?,?tenant_id?amongst others. Usually, provider configuration options include various ways to authenticate to the platform.

Two meta-arguments can also be specified in the Providers block,?version?and?alias?.?version?is deprecated and should no longer be used, this should be specified in the?required_providers?block instead. The?alias?meta-argument is useful for using the same provider, but with different configuration options. Again using our?azurerm?example, a classic case for this is when your Terraform code needs to reference multiple subscriptions.

Different?subscription_id?values can be specified here, and the calling resource or module can reference the specific provider using the alias name. For example, the provider blocks could be configured like this:

provider "azurerm" {
  aubscription_id = "4321-4321-4321-4321"
}
provider "azurerm" {
  alias = "subscriptionA"
  subscription_id = "1234-1234-1234-1234"
}        

The resource itself references the specific provider to access the different subscriptions:

resource "azurerm_resource_group" "rg1" {
  provider =  azurerm.subscriptionA
  .
  .
}        

If a child module requires a specific provider it can be referenced using the?providers?meta-argument.

If a provider is configured without the?alias option, it is considered the default and is used by all configurations unless another provider is explicitly specified.

Commonly Used Terraform Providers List

1. Azure

A cloud computing platform and service provided by Microsoft. It offers a wide range of cloud-based services and solutions, including infrastructure as a service (IaaS), platform as a service (PaaS), and software as a service (SaaS) that can be used for various computing, storage, analytics, databases, networking, machine learning, and application deployment needs.

Azure Resource Providers are components that Terraform uses to interact with Azure resources. Azure Resource Providers in Terraform correspond to different Azure services or resource types that you can manage and provision using Terraform configurations.

Each Azure service or resource type has its own resource provider in Terraform, and these providers are responsible for defining the Terraform resources and data sources that map to the corresponding Azure resources. The Azure provider for Terraform acts as the bridge between your Terraform configurations and the Azure API.

terraform {
 required_providers {
   azurerm = {
     source = "hashicorp/azurerm"
     version = "3.78.0"
   }
 }
}

provider "azurerm" {
 # Configuration options
}        

2. AWS

Amazon Web Services (AWS) is a comprehensive and widely used cloud computing platform and service provided by Amazon.com. AWS offers a vast array of cloud-based computing resources, services, and tools that enable individuals, businesses, and organizations to build, deploy, and manage a wide range of applications, websites, and services in a highly scalable and cost-effective manner.

Each AWS service or resource type corresponds to a specific resource provider in Terraform. These providers define Terraform resources and data sources that map to the corresponding AWS resources. The AWS provider for Terraform acts as the intermediary between your Terraform configurations and the AWS API.

terraform {
 required_providers {
   aws = {
     source = "hashicorp/aws"
     version = "5.23.0"
   }
 }
}

provider "aws" {
 # Configuration options
}        

3. Google Cloud

Google Cloud, often referred to as Google Cloud Platform (GCP), is a comprehensive suite of cloud computing services provided by Google. It offers a wide range of cloud-based solutions for computing, storage, databases, machine learning, data analytics, and more.

The Google Cloud Terraform Provider is used to configure your Google Cloud Platform infrastructure. It is collaboratively maintained by the Google Terraform team at Google and the Terraform team at HashiCorp.

terraform {
 required_providers {
   google = {
     source = "hashicorp/google"
     version = "5.3.0"
   }
 }
}

provider "google" {
 # Configuration options
}        

4. Kubernetes

Kubernetes, often abbreviated as K8s, is an open-source container orchestration platform for automating the deployment, scaling, and management of containerized applications. It was originally developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF). Kubernetes provides a powerful and flexible framework for container management, making it easier to deploy and operate applications in a distributed and scalable manner.

The official Kubernetes Terraform provider is provided by HashiCorp. This provider allows you to define Kubernetes resources in your Terraform configurations, such as deployments, services, and config maps.

terraform {
 required_providers {
   kubernetes = {
     source = "hashicorp/kubernetes"
     version = "2.23.0"
   }
 }
}

provider "kubernetes" {
 # Configuration options
}        

5. Oracle Cloud

Oracle Cloud, often referred to as Oracle Cloud Infrastructure (OCI), is a comprehensive cloud computing platform and infrastructure service offered by Oracle Corporation. It provides a wide range of cloud-based services, including infrastructure as a service (IaaS), platform as a service (PaaS), and software as a service (SaaS).

This provider is managed by Oracle open source directly (not Hashicorp directly?—?hence the source being oracle/oci).

terraform {
 required_providers {
   oci = {
     source = "oracle/oci"
     version = "5.18.0"
   }
 }
}

provider "oci" {
 # Configuration options
}        

6. Alibaba Cloud

Alibaba Cloud, also known as Alibaba Cloud Computing or Aliyun, is the cloud computing and data intelligence arm of Alibaba Group, one of the world’s largest e-commerce and technology companies. Alibaba Cloud provides a comprehensive range of cloud-based computing services, data storage, artificial intelligence (AI), and machine learning (ML) capabilities, as well as big data analytics and more. It is one of the leading cloud service providers in China and has a growing global presence.

Alibaba Cloud, also known as Aliyun, has an official provider for HashiCorp Terraform called alicloud.

terraform {
 required_providers {
   alicloud = {
     source = "aliyun/alicloud"
     version = "1.211.2"
   }
 }
}

provider "alicloud" {
 # Configuration options
}        

Other Useful providers

要查看或添加评论,请登录

社区洞察

其他会员也浏览了