How to create an App Service in Azure using an existent resource group

Hey everyone,

I’m trying to create an App Service in Azure using a pipeline with terraform, my question is :

It is possible to create an App Service using an existing resource group previously created ?

  1. This is the error the im getting:
    Error: A resource with the ID “/subscriptions/xxxxxx-xxxxxxxxx-xxxxxxxxxx-xxxxxxxxx/resourceGroups/DevOps_group” already exists

  2. This is my actual code:
    terraform {
    required_providers {
    azurerm = {
    source = “hashicorp/azurerm”
    version = “2.85.0”
    }
    }
    }

provider “azurerm” {
features {}
}

#Definindo Resource Group
resource “azurerm_resource_group” “rg” {
name = “DevOps_group”

}

#Definindo Service Plan
resource “azurerm_app_service_plan” “serviceplan” {
name = “appserviceplan-devops”
location = “westus2”
resource_group_name = azurerm_resource_group.rg.name
kind = “Linux”
reserved = true

sku {
tier = “Basic”
size = “B1”
}
}

#Criando App Service
resource “azurerm_app_service” “app-service” {
name = “app-service-devops”
location = “westus2”
resource_group_name = azurerm_resource_group.rg.name
app_service_plan_id = azurerm_app_service_plan.serviceplan.id

site_config {
linux_fx_version = “DOCKER|containerregistry.azurecr.io/devops-container
}

}

=======

Hi @rafaelcardosodevops

This is possible for sure! Anything you’d define as a resource will be created, thus you need something else, a data source.

An example config with that would look like:

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "2.85.0"
    }
  }
}

provider "azurerm" {
  features {}
}

#Definindo Resource Group
data "azurerm_resource_group" "rg" {
  name = "DevOps_group"
}

#Definindo Service Plan
resource "azurerm_app_service_plan" "serviceplan" {
  name                = "appserviceplan-devops"
  location            = "westus2"
  resource_group_name = data.azurerm_resource_group.rg.name
  kind                = "Linux"
  reserved            = true

  sku {
    tier = "Basic"
    size = "B1"
  }
}

#Criando App Service
resource "azurerm_app_service" "app-service" {
  name                = "app-service-devops"
  location            = "westus2"
  resource_group_name = data.azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.serviceplan.id

  site_config {
    linux_fx_version = "DOCKER|containerregistry.azurecr.io/devops-container"
  }
}