Search code examples
azure-web-app-serviceterraformterraform-provider-azureazure-container-registryterraform-template-file

Error: azurerm_app_service.ci_rg: resource repeated multiple times


I am trying to deploy 2 different Azure Apps in the same resource group.

These Azure Apps are defined as docker images stored in an Azure Container Registry (where I previously pushed those docker images).

I am not able to deploy both of them at the same time because I think there is something wrong in the way I am defining them as Terraform is expecting to find only one azurerm_app_service, but I am not sure how I can work around this?

When I run this command: terraform plan -var-file test.tfvars, then I see this message in the output:

Error: azurerm_app_service.ci_rg: resource repeated multiple times

How do I define "2 different resources of the same type"?

This is the content of the main.tf file (where I inject the variables defined in variables.tf with the values defined in test.tfvars):

// the resource group definition
resource "azurerm_resource_group" "ci_rg" {
  name = "${var.resource_group_name}"
  location = "${var.azure_location}"
}

// the app service plan definition
resource "azurerm_app_service_plan" "ci_rg" {
  name                = "${var.app_service_plan}"
  location            = "${azurerm_resource_group.ci_rg.location}"
  resource_group_name = "${azurerm_resource_group.ci_rg.name}"
  kind                = "Linux"

  sku {
    tier = "Standard"
    size = "S1"
    capacity = 2 // for both the docker containers
  }

  properties {
    reserved = true
  }
}

// the first azure app
resource "azurerm_app_service" "ci_rg" {
  name                = "${var.first_app_name}"
  location            = "${azurerm_resource_group.ci_rg.location}"
  resource_group_name = "${azurerm_resource_group.ci_rg.name}"
  app_service_plan_id = "${azurerm_app_service_plan.ci_rg.id}"

  site_config {
    linux_fx_version = "DOCKER|${var.first_app_docker_image_name}"
  }

  app_settings {
      "CONF_ENV" = "${var.conf_env}"

      "DOCKER_REGISTRY_SERVER_URL"      = "${var.docker_registry_url}",
      "DOCKER_REGISTRY_SERVER_USERNAME" = "${var.docker_registry_username}",
      "DOCKER_REGISTRY_SERVER_PASSWORD" = "${var.docker_registry_password}",
  }
}

// the second azure app
resource "azurerm_app_service" "ci_rg" {
  name                = "${var.second_app_name}"
  location            = "${azurerm_resource_group.ci_rg.location}"
  resource_group_name = "${azurerm_resource_group.ci_rg.name}"
  app_service_plan_id = "${azurerm_app_service_plan.ci_rg.id}"

  site_config {
    linux_fx_version = "DOCKER|${var.second_app_docker_image_name}"
  }

  app_settings {
      "CONF_ENV" = "${var.conf_env}"

      "DOCKER_REGISTRY_SERVER_URL"      = "${var.docker_registry_url}",
      "DOCKER_REGISTRY_SERVER_USERNAME" = "${var.docker_registry_username}",
      "DOCKER_REGISTRY_SERVER_PASSWORD" = "${var.docker_registry_password}",
  }
}

Edit:

I am not entirely sure about how this Terraform thing works, but I think the label azurerm_app_service is taken by the "syntax of Terraform". See the docs here: https://www.terraform.io/docs/providers/azurerm/r/app_service.html where the title is azurerm_app_service. So I don't think I can change that.


Solution

  • My guess would be you need to rename the second one to something else. Like this: resource "azurerm_app_service" "ci_rg_second". It obviously doesnt like the fact that it has the same name.