Here is my scenario:
I am deploying an Azure Linux Web App, it is a demo environment and the goal is to deploy via Terraform in its entirity - use it for the demo, then destroy it via Terraform (to save on costs as some of the resources are high-end SKUs for various reasons)
In order to complete the deployment of the Web App (which I do not have direct control over) - I normally would need to go to the Kudu CLI shell and run a few commands.
I understand that I can do this via the Kudu REST API, via Powershell or via Azure CLI - but I am not sure how I caqn invoke one of those methods via Terraform.
How I can invoke one of those methods via Terraform:
To access and invoke Kudu CLI commands, you can use the null_resource available in Terraform which mainly focusses on the execution of the script commands post deployment of the resource.
I have taken a sample command and tried executing it once the kudu API URI has been invoked as shown below.
main.tf
:
provider "azurerm" {
features {}
subscription_id = "xxxx"
}
resource "azurerm_resource_group" "example" {
name = "kudurg"
location = "West Europe"
}
resource "azurerm_service_plan" "example" {
name = "kudupplan"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
os_type = "Linux"
sku_name = "P1v2"
}
resource "azurerm_linux_web_app" "example" {
name = "kudutestap"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_service_plan.example.location
service_plan_id = azurerm_service_plan.example.id
site_config {}
}
resource "null_resource" "commands" {
provisioner "local-exec" {
command = <<EOT
export KUDU_URL="https://${azurerm_linux_web_app.example.name}.scm.azurewebsites.net"
export TOKEN=$(az account get-access-token --query accessToken -o tsv)
curl -X POST "$KUDU_URL/api/command" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"command": "ls"}'
EOT
}
depends_on = [azurerm_linux_web_app.example]
}
Deployment succeeded:
Reference Terraform registry azurerm_linux_web_app
.