Terraform Providers explained

terraform provider explained

At the heart of Terraform’s magic are providers – essential components that enable communication with cloud providers and other infrastructure systems. In this blog article, we will delve into what Terraform providers are, why they are crucial, and how to use them effectively to unleash the full power of Terraform in managing your cloud resources.

What are Terraform Providers?

In Terraform, a provider is a plugin that acts as an interface between Terraform and an external service or platform. Providers allow Terraform to manage resources, interact with APIs, and perform operations on various cloud platforms, infrastructure services, and third-party systems. Each provider specializes in managing resources within a specific technology or service.

Providers encapsulate the logic required to authenticate, create, update, and delete resources in the target environment. Terraform has a vast ecosystem of providers, which enables users to manage resources across different cloud providers, infrastructure technologies, and other platforms without needing to learn the intricacies of each API individually.

Providers designated as ‘official’ in the documentation are maintained by HashiCorp themselves. ‘Verified’ providers, on the other hand, are created by recognized third-party technology vendors who are also participants in the HashiCorp Technology Partner Program. Lastly, ‘community’ providers are developed by individuals or groups within the Terraform community and are not officially endorsed by HashiCorp.

Here are a few examples of popular Terraform providers:

  1. azurerm (Azure): This provider allows you to manage resources in Microsoft Azure, such as virtual machines, storage accounts, virtual networks, and more.
  2. aws (Amazon Web Services): The AWS provider enables you to manage resources in Amazon Web Services, including EC2 instances, S3 buckets, IAM roles, and more.
  3. google (Google Cloud Platform): The Google Cloud provider lets you manage resources in GCP, like virtual machines, Google Cloud Storage buckets, Kubernetes Engine clusters, and more.
  4. docker (Docker): This provider helps manage Docker containers and resources, enabling you to define and manage containerized applications.
  5. github (GitHub): The GitHub provider allows you to manage GitHub repositories, teams, and access permissions using Terraform.
  6. vsphere (VMware vSphere): This provider allows you to manage virtual machines, networks, and other resources in VMware vSphere environments.
  7. openstack (OpenStack): The OpenStack provider enables management of resources in an OpenStack cloud, including virtual machines, networks, and storage.
  8. oci (Oracle Cloud Infrastructure): This provider lets you manage resources in Oracle Cloud Infrastructure, including compute instances, object storage, networking components, and more.
  9. kubernetes (Kubernetes): The Kubernetes provider allows you to manage Kubernetes resources, such as pods, services, deployments, and namespaces.
  10. mysql (MySQL): This provider enables you to manage MySQL databases and users using Terraform.

These are just a few examples, and the Terraform provider ecosystem is continually growing. Providers allow you to define infrastructure and resources across different platforms consistently, enabling you to treat infrastructure as code and apply DevOps principles to your infrastructure management.

The Role of Terraform Providers

  • Resource Management: Providers enable Terraform to create, read, update, and delete resources in the supported cloud platforms. This allows you to manage everything from virtual machines to databases and networking components.
  • Dependency Tracking: Providers handle resource dependencies, ensuring that Terraform creates resources in the correct order based on their interdependencies.
  • State Management: When you apply your Terraform configurations, the state of the managed resources is stored by the provider. This state helps Terraform track changes and maintain the desired state of your infrastructure.

Using Terraform Providers

1. Declaring a Provider:

To use a provider in your Terraform configurations, you must first declare it in your .tf files. The provider block tells Terraform which plugin to use and how to authenticate with the cloud provider. For example:

In this example, we declare the AWS provider and set the region to “us-west-2”.

2. Resource Configuration:

Once you’ve declared the provider, you can start defining resources in your configurations that map to the corresponding cloud resources. For instance, creating an AWS EC2 instance:

In this example, we define an EC2 instance resource using the AWS provider. We specify the AMI ID and the instance type for the EC2 instance.

3. Initializing Terraform:

Before applying your configurations, run terraform init in your project directory. Terraform will download the necessary provider plugins for your configurations.

4. Applying Configurations:

To create the resources defined in your configurations, run terraform apply:

Terraform will display the changes that it plans to make and prompt you to confirm before applying them.

5. Modifying and Destroying Resources:

You can modify your resource configurations, and when you run terraform apply again, Terraform will make the necessary changes to your cloud resources. Similarly, when you’re done with your resources, you can run terraform destroy to delete them.

Example using Azure Terraform Provider to create a VM

  1. Install Terraform: Make sure you have Terraform installed on your local machine.
  2. Create a Terraform Configuration File: Create a file named main.tf and add the following content:
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "example-ipconfig"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "example" {
  name                  = "example-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_DS1_v2"

  storage_os_disk {
    name              = "example-osdisk"
    caching           = "ReadWrite"
    create_option    = "FromImage"
  }

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  os_profile {
    computer_name  = "examplevm"
    admin_username = "adminuser"
    admin_password = "P@ssw0rd123!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}
  1. Authenticate with Azure: You need to authenticate Terraform to interact with your Azure account. You can do this using Azure CLI or Service Principal.
  2. Initialize and Apply the Configuration: Open a terminal in the directory containing main.tf and execute the following commands
terraform init
terraform apply
  1. Terraform will show you a plan of the resources it will create and then prompt you to confirm before proceeding.
  2. Review and Destroy: After you’re done experimenting, you can run terraform destroy to remove all the resources created by your configuration.

Remember, this is just a simple example. The Azure Terraform provider supports a wide variety of Azure resources and configuration options, and you can customize the configuration to suit your needs.

Visualize your Terraform for every provider

Holori seamlessly integrates with the Terraform providers offered by all cloud providers. We’ve meticulously aligned with the complete Terraform repository, ensuring our compatibility spans across all providers and Terraform versions.

holori supported terraform provider

With Holori you can import a Terraform file and visualize the corresponding Terraform diagram. There is nothing better than a diagram to understand the complexity of an infra and the dependency between elements.

Go sign up here to try the tool : https://app.holori.com/register

Key Takeaways

Terraform providers are the bridge that connects your infrastructure as code configurations to your cloud resources. By leveraging providers, you can easily manage a wide range of cloud resources through a simple, declarative syntax. Whether you’re working with AWS, Azure, Google Cloud, or any other supported platform, Terraform providers empower you to create, modify, and manage your cloud infrastructure with ease and efficiency. To ensure that this powerfull actions remains under your full control, make use of Holori’s integration in your CI/CD to be more efficient than ever. Embrace the power of Terraform providers and unlock the true potential of Infrastructure as Code in managing your cloud resources. Happy provisioning!

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore