This README provides instructions on how to use Terraform to deploy infrastructure on Microsoft Azure. Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision your infrastructure using declarative configuration files.
In this guide, you will learn how to set up Terraform, configure Azure authentication, create a simple Azure resource, and deploy it using Terraform.
Before you begin, make sure you have the following prerequisites in place:
-
Terraform: Install Terraform on your local machine by following the instructions provided at Terraform Downloads.
-
Azure Subscription: You will need an active Azure subscription. If you don't have one, you can sign up for a free trial at Azure Free Account.
-
Azure CLI: Install the Azure Command-Line Interface (CLI) from Azure CLI Installation Guide.
-
Azure Service Principal: Create an Azure Service Principal to authenticate Terraform to Azure. You can do this using the Azure CLI or Azure Portal. Make sure to note down the following details:
- Client ID
- Client Secret
- Subscription ID
- Tenant ID
-
Azure Authentication: Set up Azure authentication using your Azure Service Principal credentials. You can configure it by setting the following environment variables or using a
provider
block in your Terraform configuration files:export ARM_CLIENT_ID="YOUR_CLIENT_ID" export ARM_CLIENT_SECRET="YOUR_CLIENT_SECRET" export ARM_SUBSCRIPTION_ID="YOUR_SUBSCRIPTION_ID" export ARM_TENANT_ID="YOUR_TENANT_ID"
Alternatively, you can use a
provider
block in your Terraform configuration file (main.tf
):provider "azurerm" { features {} }
Terraform will use the environment variables or the provider block to authenticate with Azure.
-
Terraform Configuration: Create a Terraform configuration file (e.g.,
main.tf
) to define your infrastructure resources. Here is an example that creates a simple Azure resource group:provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = "example-resources" location = "East US" }
-
Initialize Terraform: Run the following command in the directory containing your Terraform configuration file to initialize your working directory:
terraform init
-
Deploy Infrastructure: After initializing, apply your Terraform configuration to create the Azure resources:
terraform apply
Confirm the action by typing "yes" when prompted.
To remove the resources created by Terraform, run:
terraform destroy
Confirm the action by typing "yes" when prompted.
You have now successfully set up Terraform to work with Azure and deployed a simple resource group. You can extend your Terraform configuration to create more complex infrastructure on Azure. Explore the Terraform documentation and Azure Resource Manager (ARM) templates to define and manage your infrastructure efficiently.