使用 Terraform 和 Azure DevOps 将 Azure 应用服务部署到租户中的多个订阅 - 需要建议

问题描述 投票:0回答:1

我想使用 Azure Terraform 和 Azure DevOps 管道将 Azure 应用服务部署和配置到 3 个不同的订阅中。我可以得到一些关于如何做到这一点的建议吗?

注意:对于单个订阅,我可以使其正常工作而不会出现任何问题。对于多个订阅,我无法做到这一点。

  1. 我尝试创建 3 个不同的服务连接

到目前为止我在VS代码中尝试的是:

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

  

但是我面临着状态文件 (.tfstate) 的问题,因为它们是 3 个不同的订阅。

Terraform 工作区 我还阅读了有关 terraform 工作区的信息,但在将 Terraform 与 Azure DevOps 管道一起使用时,我在配置时遇到了问题,请问我可以获取一些有关如何执行此操作的建议吗?谢谢

azure azure-devops terraform terraform-provider-azure
1个回答
0
投票

为了将 terraform 资源部署到两个或多个订阅,通过传递别名参数来使用多个提供程序是一种有效的方法。

alias
用于传递订阅名称,如下所示。使用它,您可以传递多个订阅进行部署,并在资源中将其引用为
azurerm.<aliasname>

请参阅@Jeff Brown Tech 的博客以获取相关信息。

以下是完整代码供大家参考

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 = {}
}

enter image description here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.