Use ARM templates with Terraform
Templates in Terraform are forms of work with string. In the example described in this task we may use an ARM template generated in Azure and use variable interpolation to create a template for future deployments.
In Azure portal UI select the component which will be deployed in the future many times but with different name, in different resource group or different location. Navigate to “Export template” section and configure how would you like your template: with parameters or without parameters, select section which will be put in template. Select “Download” or highlight section and copy and paste into the future template file. After that this template will require modifications like adding dynamical parameters which will be replaced later to new environment variables.:
Defining a template in terraform can be done two ways: template as data-source or call template function. Template function requires a template provider. Preferred way is data source:
Data source example:
data "template_file" "public_subnet" {
template = file("publicSubnet.tpl")
vars = {
name = var.subnetname
addresPrefix = var.int_subnet_ip_range
}
}
In the example above we have an external template file publicSubnet.tpl which contains variables $(name) and $(addressPrefix). Those variables will be replaced by values from var.subnetname and var.int_subnet_ip_range defined in vars or defined during terraform execution.
To get this template ready to be used need to call it with rendered attribute:
data.template_file.public_subnet.rendered
If the plan is to use Azure ARM templates, the approach is slightly different. AzureRm provider has arm template deployment ability via azure_resoyrce_group_template_deployment. It will allow the use of ARM templates with parameters, defined template body and required dependency (resources already deployed).