|
| 1 | +# Terraform Infrastructure |
| 2 | + |
| 3 | +This directory contains Terraform configurations that are equivalent to the Bicep templates in this repository. You can choose to use either Bicep or Terraform for deploying the infrastructure. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +Each deployment option has its own directory with Terraform configurations: |
| 8 | + |
| 9 | +- **aca/** - Azure Container Apps deployment |
| 10 | +- **aci/** - Azure Container Instance deployment |
| 11 | +- **aks/** - Azure Kubernetes Service deployment |
| 12 | +- **acr/** - Standalone Azure Container Registry module |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +1. [Terraform](https://www.terraform.io/downloads) >= 1.0 |
| 17 | +2. [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) |
| 18 | +3. An active Azure subscription |
| 19 | + |
| 20 | +## Authentication |
| 21 | + |
| 22 | +Before running Terraform, authenticate with Azure: |
| 23 | + |
| 24 | +```bash |
| 25 | +az login |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Azure Container Apps (ACA) |
| 31 | + |
| 32 | +```bash |
| 33 | +cd azd/aca |
| 34 | + |
| 35 | +# Initialize Terraform |
| 36 | +terraform init |
| 37 | + |
| 38 | +# Review the execution plan |
| 39 | +terraform plan -var="environment_name=myenv" -var="location=eastus" |
| 40 | + |
| 41 | +# Apply the configuration |
| 42 | +terraform apply -var="environment_name=myenv" -var="location=eastus" |
| 43 | +``` |
| 44 | + |
| 45 | +### Azure Container Instance (ACI) |
| 46 | + |
| 47 | +```bash |
| 48 | +cd azd/aci |
| 49 | + |
| 50 | +# Initialize Terraform |
| 51 | +terraform init |
| 52 | + |
| 53 | +# Review the execution plan |
| 54 | +terraform plan -var="environment_name=myenv" -var="location=eastus" |
| 55 | + |
| 56 | +# Apply the configuration |
| 57 | +terraform apply -var="environment_name=myenv" -var="location=eastus" |
| 58 | +``` |
| 59 | + |
| 60 | +### Azure Kubernetes Service (AKS) |
| 61 | + |
| 62 | +```bash |
| 63 | +cd azd/aks |
| 64 | + |
| 65 | +# Initialize Terraform |
| 66 | +terraform init |
| 67 | + |
| 68 | +# Review the execution plan |
| 69 | +terraform plan -var="environment_name=myenv" -var="location=eastus" |
| 70 | + |
| 71 | +# Apply the configuration |
| 72 | +terraform apply -var="environment_name=myenv" -var="location=eastus" |
| 73 | +``` |
| 74 | + |
| 75 | +## Variables |
| 76 | + |
| 77 | +Each deployment option has configurable variables. Here are the common ones: |
| 78 | + |
| 79 | +### Required Variables |
| 80 | + |
| 81 | +- `environment_name` - Name of the environment (1-64 characters) |
| 82 | +- `location` - Azure region (e.g., "eastus", "westus2") |
| 83 | + |
| 84 | +### Optional Variables (ACA) |
| 85 | + |
| 86 | +- `container_image` - Container image to deploy (default: "docker-app") |
| 87 | +- `container_port` - Port the container listens on (default: 80) |
| 88 | +- `cpu_cores` - CPU cores allocated (default: "0.5") |
| 89 | +- `memory_in_gb` - Memory allocated (default: "1Gi") |
| 90 | + |
| 91 | +### Optional Variables (ACI) |
| 92 | + |
| 93 | +- `container_image` - Container image to deploy (default: "mcr.microsoft.com/azuredocs/aci-helloworld") |
| 94 | +- `container_port` - Port the container listens on (default: 80) |
| 95 | +- `cpu_cores` - CPU cores allocated (default: "1.0") |
| 96 | +- `memory_in_gb` - Memory in GB (default: "1.5") |
| 97 | + |
| 98 | +### Optional Variables (AKS) |
| 99 | + |
| 100 | +- `agent_count` - Number of agent nodes (default: 3, range: 1-100) |
| 101 | +- `agent_vm_size` - VM size for nodes (default: "Standard_D2s_v3") |
| 102 | +- `os_disk_size_gb` - OS disk size in GB (default: 0, range: 0-1023) |
| 103 | +- `os_type` - Operating system type (default: "Linux", options: "Linux", "Windows") |
| 104 | + |
| 105 | +## Using Variable Files |
| 106 | + |
| 107 | +You can create a `terraform.tfvars` file to store your variables: |
| 108 | + |
| 109 | +```hcl |
| 110 | +environment_name = "myenv" |
| 111 | +location = "eastus" |
| 112 | +container_image = "myregistry.azurecr.io/myapp" |
| 113 | +container_port = 8080 |
| 114 | +``` |
| 115 | + |
| 116 | +Then run: |
| 117 | + |
| 118 | +```bash |
| 119 | +terraform apply |
| 120 | +``` |
| 121 | + |
| 122 | +## Outputs |
| 123 | + |
| 124 | +Each configuration provides outputs that can be used for further automation: |
| 125 | + |
| 126 | +- `AZURE_LOCATION` - The Azure region where resources are deployed |
| 127 | +- `AZURE_CONTAINER_REGISTRY_ENDPOINT` - Container registry login server |
| 128 | +- `AZURE_CONTAINER_REGISTRY_NAME` - Container registry name |
| 129 | +- Additional outputs specific to each deployment type |
| 130 | + |
| 131 | +## Comparison with Bicep |
| 132 | + |
| 133 | +| Feature | Bicep | Terraform | |
| 134 | +|---------|-------|-----------| |
| 135 | +| Language | ARM Template DSL | HCL (HashiCorp Configuration Language) | |
| 136 | +| Provider | Azure-specific | Multi-cloud (Azure, AWS, GCP, etc.) | |
| 137 | +| State Management | Server-side (Azure) | Client-side (local or remote backend) | |
| 138 | +| Module System | Bicep modules | Terraform modules | |
| 139 | +| Validation | `az bicep build` | `terraform validate` | |
| 140 | +| Preview Changes | `az deployment what-if` | `terraform plan` | |
| 141 | +| Apply Changes | `az deployment create` | `terraform apply` | |
| 142 | + |
| 143 | +## Clean Up |
| 144 | + |
| 145 | +To destroy all resources created by Terraform: |
| 146 | + |
| 147 | +```bash |
| 148 | +terraform destroy -var="environment_name=myenv" -var="location=eastus" |
| 149 | +``` |
| 150 | + |
| 151 | +Or if using a tfvars file: |
| 152 | + |
| 153 | +```bash |
| 154 | +terraform destroy |
| 155 | +``` |
| 156 | + |
| 157 | +## Notes |
| 158 | + |
| 159 | +- The Terraform configurations are functionally equivalent to their Bicep counterparts |
| 160 | +- Both Bicep and Terraform files are maintained in this repository for flexibility |
| 161 | +- The `.terraform/` directory and state files are git-ignored for security |
| 162 | +- Always review the `terraform plan` output before applying changes |
0 commit comments