Terraform with Azure and GCP
# CHAPTER 10
Terraform with Azure and GCP
1. Introduction
While AWS holds the largest market share, Microsoft Azure and Google Cloud Platform (GCP) are critical players, especially in enterprise and data-analytics environments. One of Terraform's greatest selling points is that it is "Cloud Agnostic." This does *not* mean you can write code once and run it on any cloud; the resources and APIs are fundamentally different. However, it *does* mean you use the exact same HCL syntax, the exact same workflow (init, plan, apply), and the exact same state management principles regardless of the provider. In this chapter, we will explore the nuances of the azurerm and google providers, allowing you to orchestrate multi-cloud deployments.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Authenticate the Azure (
azurerm) and Google Cloud (google) providers.
- Understand the hierarchical differences between AWS, Azure, and GCP resources.
- Provision a Virtual Machine in Microsoft Azure.
- Provision a Compute Engine instance in Google Cloud.
- Grasp the concept of Multi-Cloud provisioning within a single Terraform workspace.
3. Beginner-Friendly Explanation
Imagine renting a car from Hertz (AWS), Avis (Azure), or Enterprise (GCP).-
The Workflow (Terraform): No matter which company you go to, the process is exactly the same. You show your ID, sign a contract, and get the keys. (This is
terraform planandterraform apply).
- The Product (The Cloud): However, Hertz might give you a "Ford Focus," while Avis gives you a "Toyota Corolla." The steering wheel and buttons are in slightly different places.
You cannot use a Ford key to start a Toyota. In Terraform, you cannot use an awsinstance block to build a server in Azure; you must use the azurermlinuxvirtualmachine block. The workflow is identical, but the vocabulary changes.
4. Terraform with Microsoft Azure (azurerm)
Unlike AWS, Azure forces you to place almost every single resource inside a logical container called a Resource Group. If you don't create a Resource Group first, you cannot build a server.
5. Terraform with Google Cloud (GCP)
Google Cloud has its own hierarchy. Everything is contained within a Project.6. Mini Project: Multi-Cloud Deployment Concept
The ultimate power of Terraform is multi-cloud orchestration. You can define multiple providers in the same file and build a unified architecture spanning across competing tech giants.Step-by-Step Architecture Concept: Let's build a highly resilient architecture: A primary web server on AWS, and a backup server on Google Cloud.
*When you run terraform apply, Terraform talks to both Amazon and Google at the exact same time, building infrastructure on both clouds simultaneously.*
7. Real-World Scenarios
A global streaming service hosted their entire application on AWS. However, they found that Google Cloud's BigQuery data warehousing tool was vastly superior to AWS alternatives for analyzing user viewing habits. Instead of migrating their entire application to Google, the DevOps team used Terraform to orchestrate a Multi-Cloud environment. They used theaws provider to deploy their web servers, and in the exact same Terraform workspace, used the google provider to deploy their BigQuery datasets. Terraform managed the complex integration points, allowing the company to cherry-pick the best technologies from competing clouds effortlessly.
8. Best Practices
-
Authentication Methods: Avoid using
credentials = file("key.json")in your GCP provider block if possible. Hardcoding paths to secret files is dangerous. Instead, rely on standard cloud CLI tools. Rungcloud auth application-default loginin your terminal. Terraform will automatically detect the secure Google token on your machine, just as it does with AWS environment variables.
9. Security Recommendations
- Azure Service Principals: When automating Azure via CI/CD (like GitHub Actions), you must create a "Service Principal" (a robotic user). Never give this Service Principal "Contributor" access to the entire Azure Subscription. Use strict Role-Based Access Control (RBAC) to ensure Terraform can only modify resources within its specifically assigned Resource Group.
10. Troubleshooting Tips
-
Azure Provider Features Block: In the
azurermprovider, thefeatures {}block is mandatory, even if it is completely empty. If you omit this block, Terraform will fail to initialize. It is a quirk specific to the Azure provider's design.
11. Exercises
- 1. What is the fundamental organizational container required in Azure before you can provision any other resource (like a Virtual Network or Virtual Machine)?
- 2. How does Terraform achieve "Cloud Agnosticism"? Does it mean you write one block of code that works on all clouds?
12. FAQs
Q: Which cloud is easiest to manage with Terraform? A: AWS has the most mature and widely used Terraform provider, meaning you will find the most community support and examples for AWS. However, the HashiCorp teams work closely with Google and Microsoft to ensure all three primary providers are exceptionally robust.13. Interview Questions
- Q: Explain the paradigm of Multi-Cloud provisioning using Terraform. How would you architect a single Terraform workspace to manage an AWS EC2 instance that communicates with an Azure SQL Database?
- Q: Contrast the foundational organizational hierarchies of AWS, Azure, and Google Cloud Platform as they relate to Terraform resource provisioning (e.g., Accounts vs. Resource Groups vs. Projects).