hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.58k stars 4.62k forks source link

Support for using latest correct build deployment for Azure Spring Apps #20980

Open felipmiguel opened 1 year ago

felipmiguel commented 1 year ago

Is there an existing issue for this?

Community Note

Description

When using azurerm_spring_cloud_build_deployment it is necessary to specify build_result_id. It is possible to specify a build id, but there is no way to retrieve that value using terraform. Another option is specifying <default>. The problem with the default value is that it is not the latest or the current deployment, causing a kind of uninstallation of the application. In my environment it causes the application to stop working.

Some proposals:

New or Affected Resource(s)/Data Source(s)

azurerm_spring_cloud_build_deployment, azurerm_spring_cloud_build

Potential Terraform Configuration

# Using a new data source named azure_spring_cloud_build
data "azurerm_spring_cloud_build_deployment" "latest_build" {
     spring_cloud_app_id = azurerm_spring_cloud_app.application.id
     build_id = "<latest>"
}
resource "azurerm_spring_cloud_build_deployment" "application_deployment" {
  name                = "default"
  spring_cloud_app_id = azurerm_spring_cloud_app.application.id
  build_result_id     = data.azurerm_spring_cloud_build_deployment.latest_build.id
  # other params
}

# Asumming a new value for build_result_id
resource "azurerm_spring_cloud_build_deployment" "application_deployment" {
  name                = "default"
  spring_cloud_app_id = azurerm_spring_cloud_app.application.id
  build_result_id     = "<latest>"
  # other params
}

References

No response

yuwzho commented 1 year ago

Generatlly, I agree with the "Potential Terraform Configuration" provided by @manicminer . According to currently API spec, my suggestion is having a data resource for Microsoft.AppPlatform/Spring/BuildServices/Builds, that can retrive the latest build result id. Then the configuration can be written as below. Everytime the configuration executed, the Deployment will consume the latest build result id.

cc @ms-henglu @smile37773 @showpune

data "azurerm_spring_cloud_buildservice_build" "example" {
  name                            = azurerm_spring_cloud_buildservice_build.example.name
  resource_group_name = azurerm_spring_cloud_buildservice_build.example.resource_group_name
  service_name               = azurerm_spring_cloud_buildservice_build.example.service_name
  buildservice_name       = azurerm_spring_cloud_buildservice_build.example.buildservice_name
}

resource "azurerm_spring_cloud_build_deployment" "application_deployment" {
  name                         = "default"
  spring_cloud_app_id  = azurerm_spring_cloud_app.application.id
  build_result_id           = data.azurerm_spring_cloud_buildservice_build.example.triggeredBuildResult.id
  # other params
}
d-desai commented 1 year ago

Hi is this feature being implemented or is there a work around right now which we can use to get the latest correct build?

showpune commented 1 year ago

@smile37773, can you help to update here?

smile37773 commented 1 year ago

Hi is this feature being implemented or is there a work around right now which we can use to get the latest correct build?

Yes, you can get the latest result id by calling the GET API of the build. You can get the triggeredBuildResult in the response, which is the latest result id. And then you can use this id as the BuildResultId in the deployment.

yuwzho commented 1 year ago

Looks like the data is not added yet. You can try the azapi for a quick work. @d-desai cc @ms-henglu

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.7.0"
    }
    azapi = {
      source = "Azure/azapi"
    }
  }
}

resource "azurerm_spring_cloud_service" "spring" {
  name                = var.service_name
  resource_group_name = var.resource_group_name
  location            = var.location
  sku_name            = "E0"

  build_agent_pool_size = "S1"
}

data "azapi_resource" "buildservice" {
  type      = "Microsoft.AppPlatform/Spring/buildServices@2023-03-01-preview"
  name      = "default"
  parent_id = azurerm_spring_cloud_service.spring.id
}

data "azapi_resource" "build" {
  type      = "Microsoft.AppPlatform/Spring/buildServices/builds@2023-03-01-preview"
  name      = var.build_name
  parent_id = data.azapi_resource.buildservice.id

  response_export_values = ["*"]
}

resource "azurerm_spring_cloud_app" "demo-time-app" {
  name                = "demo-time"
  resource_group_name = var.resource_group_name
  service_name        = var.service_name

  is_public = true
}

resource "azurerm_spring_cloud_build_deployment" "blue" {
  name                = "blue"
  spring_cloud_app_id = azurerm_spring_cloud_app.demo-time-app.id
  build_result_id     = jsondecode(data.azapi_resource.build.output).properties.triggeredBuildResult.id
  instance_count      = 2
  quota {
    cpu    = "2"
    memory = "2Gi"
  }
}
felipmiguel commented 1 year ago

@yuwzho , is it possible to change the behavior of <default> build_result_id? The solution that you provided can work when there is already an existing build. However if you deploy Terraform configuration for the first time there is no build, hence the deployment will fail. I changing the behavior of <default> to the latest available or at least that do not overwrite if there a build assigned can work better.

Ideally the steps to follow would be:

  1. Deploy Terraform. build_result_id=<default>. Doesn't work yet as there is no build.
  2. Deploy the application, for instance using az cli. It works now as it is assigned a build to the deployment.
  3. If changes in the Terraform configuration are required, for instance for other components, not the application, then apply Terraform. Right now, using <default> overwrites the working build id. If <default> doesn't overwrite it would continue working.