Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.
You can see many options to install Terraform on your machine on this document
We choose chocolatey because is the easiest way.
choco install terraformIf possible, all variables must be inside this file.
Terraform variable is defined on variables.tf file as:
variable "variable_name" {
type = string
default = "value"
}The syntax for get any value from variables.tf is:
var.variable_nameSecrets can store here but it's not recommended because this file maybe will be store on control version system and any user can reveal the secret.
This secret (or any variable) can be replaced when apply command is executed as:
terraform apply -var="variable_name=value"On this file we can define different providers that we will use on our process.
provider "azurerm" {
subscription_id = var.azure_subscription_id
tenant_id = var.azure_tenant_id
client_id = var.azure_client_id
client_secret = var.azure_client_secret
features {}
}Note: you can generate variables like client ID and client secret using this guide
This file contains all resources that Terraform will create, update or delete on specific vendor infrastructure.
With Terraform you can create on Azure all services defined on this document.
For example, to create an App Service with related App Service Plan.
resource "azurerm_app_service_plan" "namespace" {
name = "service_plan_name"
location = var.azure_location
resource_group_name = var.azure_resource_group
kind = "Windows"
sku {
tier = "Free"
size = "F1"
}
tags = var.azure_common_tags
}
resource "azurerm_app_service" "namespace" {
name = "app-service-name"
resource_group_name = var.azure_resource_group
location = var.azure_location
app_service_plan_id = azurerm_app_service_plan.namespace.id
tags = var.azure_common_tags
}
Output values is a way to capture information from created or update resources
output "app_service_id" {
value = azurerm_app_service.demo.id
}
output "app_service_default_site_hostname" {
value = azurerm_app_service.demo.default_site_hostname
}
output "app_service_username" {
value = azurerm_app_service.demo.site_credential[0].username
}
output "app_service_password" {
value = azurerm_app_service.demo.site_credential[0].password
}When Terraform executes successfully throught terraform apply command, it returns a set of data with resource information
app_service_id = /subscriptions/416268c2-d33e-4407-9e28-6db4446ae43f/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/demo-terraform-app-service
app_service_default_site_hostname = demo-terraform-app-service.azurewebsites.net
app_service_username = $demo-terraform-app-service
app_service_password = veEHEMQdnhql7cxw0HyNSZzP6SRB8yq2cadvqdaw4Y6hRedX6wzzTl1DPzScTo check, we can use this parameters to validate infrastructure creation
curl demo-terraform-app-service.azurewebsites.net
StatusCode : 200
StatusDescription : OK
...The complete list of Terraform commands is available here
Initialize a working directory containing Terraform configuration files
terraform initValidates configuration without accessing any remote services
terraform validateterraform validate --jsonAn example of successful validation
Success! The configuration is valid.{
"valid": true,
"error_count": 0,
"warning_count": 0,
"diagnostics": []
}An example of failed validation
Error: Invalid expression
on main.tf line 3, in resource "azurerm_app_service_plan" "terraformdemo":
3: location = #var.azure_location
4:
Expected the start of an expression, but found an invalid expression token.{
"valid": false,
"error_count": 1,
"warning_count": 0,
"diagnostics": [
{
"severity": "error",
"summary": "Invalid expression",
"detail": "Expected the start of an expression, but found an invalid expression token.",
"range": {
"filename": "main.tf",
"start": {
"line": 3,
"column": 29,
"byte": 142
},
"end": {
"line": 4,
"column": 1,
"byte": 164
}
}
}
]
}Applies the changes required to reach the desired state of the configuration. This means that Terraform can create, update or remove resources.
terraform applyRemove all resources managed by Terraform.
terraform destroyOn this document you can see the prepared demos.
You can contact me via Twitter @sergiobarriel