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.46k stars 4.54k forks source link

timeframe attribute from azurerm_subscription_cost_management_view does not work properly #22933

Open JoelCanteroGCO opened 10 months ago

JoelCanteroGCO commented 10 months ago

Is there an existing issue for this?

Community Note

I have the following code to create a subscription cost management view: `resource "azurerm_subscription_cost_management_view" "subscription_cost_management_view" { name = "costview-${var.subscription_name}" display_name = "costview-${var.subscription_name}" chart_type = "StackedColumn" accumulated = false

subscription_id = "/subscriptions/${var.subscription_id}"

report_type = "Usage" timeframe = "YearToDate"

dataset { granularity = "Monthly"

aggregation {
  name        = "totalCost"
  column_name = "Cost"
}

} pivot { name = "ResourceId" type = "Dimension" }

pivot { name = "ResourceGroupName" type = "Dimension" }

pivot { name = "ServiceName" type = "Dimension" } }`

But when I go to this view, the timeframe is not YearToDate:

image

Terraform Version

1.5.5

AzureRM Provider Version

3.69.0

Affected Resource(s)/Data Source(s)

azurerm_subscription_cost_management_view

Terraform Configuration Files

variable "subscription_id" {
  type = string
}

variable "subscription_name" {
  type = string
}
# Configure the Azure Active Directory Provider
provider "azurerm" {
  features {}
  client_id     = ""
  client_secret = ""
  tenant_id     = ""
  subscription_id = var.subscription_id
}

resource "azurerm_subscription_cost_management_view" "subscription_cost_management_view" {
  name         = "costview-${var.subscription_name}"
  display_name = "costview-${var.subscription_name}"
  chart_type   = "StackedColumn"
  accumulated  = false

  subscription_id = "/subscriptions/${var.subscription_id}"

  report_type = "Usage"
  timeframe   = "YearToDate"

  dataset {
    granularity = "Monthly"

    aggregation {
      name        = "totalCost"
      column_name = "Cost"
    }
  }
  pivot {
       name = "ResourceId"
        type = "Dimension"
    }

  pivot {
       name = "ResourceGroupName"
       type = "Dimension"
    }

  pivot {
       name = "ServiceName"
       type = "Dimension"
    }
}

resource "azurerm_cost_management_scheduled_action" "example" {
  name         = "scheduledaction-${var.subscription_name}"
  display_name = "scheduledaction-${var.subscription_name}"

  view_id = azurerm_subscription_cost_management_view.subscription_cost_management_view.id

  email_address_sender = ""
  email_subject        = "Reporte de costes de la suscripción"
  email_addresses      = [""]
  message              = "Hi all, take a look at last 6 months spending!"

  frequency  = "Daily"
  start_date = "2023-01-02T00:00:00Z"
  end_date   = "2024-01-01T00:00:00Z"
}

Debug Output/Panic Output

Plan: 2 to add, 0 to change, 2 to destroy.
azurerm_cost_management_scheduled_action.example: Destroying... [id=/subscriptions/78cc28dc-d0c5-43b7-8352-965399361c04/providers/Microsoft.CostManagement/scheduledActions/scheduledaction-ds-sin013-dev]
azurerm_cost_management_scheduled_action.example: Destruction complete after 1s
azurerm_subscription_cost_management_view.subscription_cost_management_view: Destroying... [id=/subscriptions/78cc28dc-d0c5-43b7-8352-965399361c04/providers/Microsoft.CostManagement/views/costview-ds-sin013-dev]
azurerm_subscription_cost_management_view.subscription_cost_management_view: Destruction complete after 3s
azurerm_subscription_cost_management_view.subscription_cost_management_view: Creating...
azurerm_subscription_cost_management_view.subscription_cost_management_view: Creation complete after 6s [id=/subscriptions/78cc28dc-d0c5-43b7-8352-965399361c04/providers/Microsoft.CostManagement/views/costview-ds-sin013-dev]
azurerm_cost_management_scheduled_action.example: Creating...
azurerm_cost_management_scheduled_action.example: Creation complete after 3s [id=/subscriptions/78cc28dc-d0c5-43b7-8352-965399361c04/providers/Microsoft.CostManagement/scheduledActions/scheduledaction-ds-sin013-dev]

Apply complete! Resources: 2 added, 0 changed, 2 destroyed.

Expected Behaviour

To see the costview with YearToDate timeframe

Actual Behaviour

The costview is just for the actual month

Steps to Reproduce

terraform apply

Important Factoids

No response

References

No response

neil-yechenwei commented 10 months ago

Thanks for raising this issue. Seems "granularity" needs to be set to "Daily".

cerocool1203 commented 10 months ago

I do encounter the same issue while trying to replicate the view below. Additionally, it seems like we cannot set the report type as ActualCost only Usage is valid and for the timeframe the value Last12months is not valid neither but it is available via API.


  "value": [
    {
      "id": "/subscriptions/dhfksdfhksdhkhkshf/providers/Microsoft.CostManagement/views/yearlyview",
      "name": "yearlyview",
      "type": "Microsoft.CostManagement/Views",
      "eTag": "\"dsadsadd\"",
      "properties": {
        "displayName": "Yearly View",
        "scope": "subscriptions/kjsdfksdfhdghdoghdo",
        "createdOn": "2023-08-15T12:12:02.5352058Z",
        "modifiedOn": "2023-08-15T12:12:02.5352058Z",
        "query": {
          "type": "ActualCost",
          "timeframe": "None",
          "dataSet": {
            "granularity": "Monthly",
            "aggregation": {
              "totalCost": {
                "name": "Cost",
                "function": "Sum"
              },
              "totalCostUSD": {
                "name": "CostUSD",
                "function": "Sum"
              }
            },
            "grouping": [
              {
                "type": "Dimension",
                "name": "ServiceName"
              }
            ],
            "sorting": [
              {
                "direction": "Ascending",
                "name": "BillingMonth"
              }
            ]
          },
          "includeMonetaryCommitment": false
        },
        "dateRange": "Last12Months",
        "currency": "USD",
        "chart": "StackedColumn",
        "accumulated": "False",
        "metric": "ActualCost",
        "kpis": [
          {
            "type": "Budget",
            "id": "COST_NAVIGATOR.BUDGET_OPTIONS.NONE",
            "enabled": true
          },
          {
            "type": "Forecast",
            "id": null,
            "enabled": true
          }
        ],
        "pivots": [
          {
            "type": "Dimension",
            "name": "ServiceName"
          },
          {
            "type": "Dimension",
            "name": "ResourceLocation"
          },
          {
            "type": "Dimension",
            "name": "ResourceGroupName"
          }
        ]
      }
    },```
JoelCanteroGCO commented 10 months ago

Contributor

I have tried to set the granularity to Daily but the problem persists.

cerocool1203 commented 10 months ago

I manage to get the view I wanted by using the AzAPI resource as workaround

  type        = "Microsoft.CostManagement/views@2023-04-01-preview"
  resource_id = azurerm_subscription_cost_management_view.yearly.id
  body = jsonencode({
    properties = {
      dateRange = "Last12Months"
    }
  })
}
JoelCantero commented 7 months ago

I manage to get the view I wanted by using the AzAPI resource as workaround

  type        = "Microsoft.CostManagement/views@2023-04-01-preview"
  resource_id = azurerm_subscription_cost_management_view.yearly.id
  body = jsonencode({
    properties = {
      dateRange = "Last12Months"
    }
  })
}

Hello @cerocool1203, I have tried:

resource "azapi_resource" "example" {
    name = "costview"
    type        = "Microsoft.CostManagement/views@2023-04-01-preview"
    parent_id = azurerm_subscription_cost_management_view.example.id
    body = jsonencode({
        properties = {
        dateRange = "Last12Months"
        }
    })

but it throws this error:

│ RESPONSE 404: 404 Not Found │ ERROR CODE UNAVAILABLE │ -------------------------------------------------------------------------------- │ The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. │ --------------------------------------------------------------------------------

zincob commented 1 month ago

Is there any solution on the issue? We are facing exactly the same issue