Terraform Azure
Overview
HashiCorp Terraform is an open-source Infrastructure-as-Code solution that translates infrastructure into configuration files, enabling the management of various infrastructure types, including public clouds, private clouds, and SaaS services.
What is IaC?
Infrastructure as Code (IaC) uses files instead of manual interfaces, with tools like Terraform employing the HashiCorp Configuration Language (HCL) to automate resource creation. It involves declarative provisioning and post-provisioning configuration adjustments.
What is Terraform?
HashiCorp Terraform is a free tool for managing cloud infrastructure through configuration files. It supports various environments, utilizing "Terraform providers."
Benefits 1. Unified IaC Tool: Terraform Azure providers simplify resource configurations and efficiently automate Azure DevOps within the Cloud Adoption Framework for Enterprise-scale.
2. Automated Infrastructure Management: Terraform Azure's templates ensure reliable and predictable Azure resource creation, minimizing errors, enabling reuse, and reducing costs.
3. Preview Infrastructure Changes: Terraform Azure's CLI previews changes, enhancing teamwork, detecting unintended modifications early, and providing a clearer grasp of infrastructure alterations.
How does it Work?
Terraform Azure Workflow in Four Steps:
1. Define Resources:
- Specify cloud resources according to enviroment
- Create resource list
2. Create Configuration Files: Generate configuration files based on the resource list, acting as infrastructure blueprints.
3. Initialize: Execute terraform init to set up Terraform in the project directory, configuring it for your files.
4. Plan & Apply: Use terraform plan to review changes, confirm expectations, and then employ terraform apply to create resources. This process maintains a state file, enabling Terraform to track the current environment state.
Typical terraform azure configuration file:
How to Install Terraform?
Steps to install and set up the Terraform CLI for local usage:
- Terraform Executable Zip File: Download the Terraform azure executable by acquiring the zip package from the official HashiCorp website.
- Package Extraction: Extract package
-
Move terraform.exe to C:\Windows for universal PowerShell or CMD access.
-
Alternative Path Configuration: To place the file anywhere, update 'Environment Variables' with the Terraform executable path.
Verify correct installation:
Terraform Commands
After installing Terraform azure CLI, restart your terminal if you change the path or executable location to ensure correct configuration. Confirm recognition by running the help command to validate the installation.
Terraform Help
After validating the installation, run the command below to access help information for various Terraform azure commands.
Review the execution plan to understand Terraform Azure's actions.
Terraform Init
"terraform init" ensures you have all project components and plugins before deploying infrastructure.
Use "terraform plan" to review and re-run for module or backend config changes.
Terraform Plan
After "terraform init," your project is ready for testing and deployment. "terraform plan" generates an execution plan with symbols indicating infrastructure changes for "terraform apply."
Let's take a look at the output of the terraform plan.
Use "terraform plan -out" to export the plan to a file for later infrastructure changes.
In CI/CD with Terraform azure, store the Plan file (tfplan) separately for distinct planning and applying stages.
Terraform Apply
After the execution plan, run 'terraform apply' command to do the planning and deployment. Respond yes to the prompt to confirm the changes.
Terraform Azure generates an execution plan by comparing the desired configuration state to the current state, stored either locally in terraform.tfstate or remotely in a state backend.
Terraform Destroy
Terraform destroy functions as the opposite of terraform apply, allowing you to terminate resources managed by your project. Execute the destruction plan by typing "yes"
The '-' prefix in Terraform Azure output means the instance will be deleted. Terraform, like with apply, shows the proposed action and waits for approval. It plans the destruction sequence to account for resource dependencies in complex scenarios.
Terraform State
Terraform manages configurations with plans, applies, and destructions. Securely store resulting files in remote storage for collaboration, and check the current state using terraform show.
Terraform providers for Azure
There are several Terraform azure providers that enable the management of Azure infrastructure:
S.No | Providers | Function |
---|---|---|
1 | AzureRM | Manages stable resources and functionality of azure like virtual machines, storage accounts,and networking interfaces. |
2 | AzureDevops | Manages Azure DevOps resources such as agents, pipelines, queries etc |
3 | Azure Stack | Manages Azure stack resources such as virtual machines, DNS, VNet, and storag. |
4 | AzAPI | The Azure Resource Manager APIs boost AzureRM provider capabilities by enabling direct management of unreleased Azure resources and functionality. |
5 | AzureAD | Manages Azure active directory resources like users, service and, groups etc. |
FAQ
Q. What is Infrastructure as Code (IaC), and how does it differ from manual configuration?
A. It involves the management of infrastructure through files, as opposed to manual configuration via a graphical interface.
Q. Can Terraform azure handle complex Azure setups involving Azure Active Directory and Azure DevOps?
A. Yes, Terraform azure is adept at managing complex Azure setups, including Azure Active Directory and Azure DevOps with specialized providers for Azure services.
Q. How does Terraform azure ensure infrastructure validity and resource management?
A. Terraform azure streamlines management through declarative code, defining and deploying resources to reduce errors and maintain clarity in requirements.
Conclusion
- Infrastructure as Code (IaC) involves the management of infrastructure through files, as opposed to manual configuration via a graphical interface.
- This file contains the IDs and properties of the resources Terraform azure created so that it can manage or destroy those resources going forward.
- Terraform plan does not deploy any resources in the cloud whereas it simply creates a local execution plan of what your infrastructure will look like once deployed in the cloud.