I want to deploy and configure Azure App Service into 3 different subscriptions using Azure Terraform and Azure DevOps pipeline. Can I get some advice on how this can be done.
NOTE: For a single subscription I can make it work with out any issues.For multiple subscriptions I cannot do it.
So far what I tried in VS code is:
app-deployment:
vars.tf - Contains all variables for all 3 different variables
providers.tf - Contains all providers for all 3 different variables
resources.tf - Contains terraform for creation of resource group, app service etc...
main.yaml - Pipeline to trigger init, validate, plan, apply
main.yaml:
trigger: none
pr: none
pool:
vmImage: Ubuntu-latest
stages:
- stage: deployAppService
jobs:
- job: to_Dev
steps:
- template: dev/deployTodev.yml
parameters:
serviceconnection: Azure-Deployment-dev
- job: to_Acceptance
steps:
- template: acc/deployToacc.yml
parameters:
serviceconnection: Azure-Deployment-Acc
- job: to_Production
steps:
- template: prod/deployToprod.yml
parameters:
serviceconnection: Azure-Deployment-prod
But I am facing issues with state files (.tfstate) as they are 3 different subscriptions.
Terraform workspaces I also read about terraform workspaces but using Terraform with Azure DevOps pipelines I am having trouble configuring , can I please get some advice on how can I do this. Thanks
In order to deploy terraform resources to two or more subscriptions, using multiple providers by passing alias parameter is an efficient way.
alias
is used to pass the subscription name as shown below. Using that you can pass multiple subscriptions for deployment and reference it in the resource as azurerm.<aliasname>
.
Refer blog by @Jeff Brown Tech for the relevant information.
Below is the complete code for your reference
provider "azurerm" {
subscription_id = "f7bxxxxx014"
tenant_id = "93xxxxf6d"
}
provider "azurerm" {
alias = subscription2
subscription_id = "subscription_id"
tenant_id = "xxxx" //Provide tenant ID if the 2nd subscription existed in another tenant
}
provider "azurerm" {
alias = subscription3
subscription_id = "subscription_id"
tenant_id = "xxxx" //Provide tenant ID if the 3rd subscription existed in another tenant
}
data "azurerm_resource_group" "example" {
name = "Jahnavi"
}
resource "azurerm_app_service_plan" "example" {
provider = "azurerm"
name = "examplejahserviceplan"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_app_service" "example" {
provider = "azurerm"
name = "jahapservice"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
site_config {
dotnet_framework_version = "v4.0"
scm_type = "LocalGit"
}
app_settings = {}
}
resource "azurerm_app_service_plan" "example" {
provider = "azurerm.subscription2"
name = "examplejahserviceplan"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
sku {
tier = "Standard"
size = "S1"
}
}
resource "azurerm_app_service" "example" {
provider = "azurerm.subscription2"
name = "jahapservice"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
app_service_plan_id = azurerm_app_service_plan.example.id
site_config {
dotnet_framework_version = "v4.0"
scm_type = "LocalGit"
}
app_settings = {}
}