简体   繁体   中英

Terraform - create azure cost management view using Rest API call via terraform code

I want to call Rest API in terraform. Below is the Rest API sample request that needs to be used to create a cost analysis view in Azure. We need to deploy this resource as code using terraform. In order to create the View/s we can use REST API: https://learn.microsoft.com/en-us/rest/api/cost-management/views/create-or-update?tabs=HTTP

we can use this API to Email subscribe by creating scheduled action: https://learn.microsoft.com/en-us/rest/api/cost-management/scheduled-actions/create-or-update?tabs=HTTP

For the second API for scheduled action for email subscription, we should use the payload body below as example:

{
    "kind": "Email",
    "properties": {
        "displayName": "Test ",
        "status": "Enabled",
        "viewId": "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}/providers/Microsoft.CostManagement/views/test",
        "schedule": {
            "frequency": "Weekly",
            "startDate": "2023-01-11T02:30:00.000Z",
            "endDate": "2024-01-10T18:30:00.000Z",
            "daysOfWeek": [
                "Wednesday"
            ]
        },
        "notification": {
            "to": [
                test@microsoft.com
            ],
            "subject": "Test",
            "message": "Test"
        },
        "fileDestination": {
            "fileFormats": [
                "Csv"
            ]
        },
        "scope": "/providers/Microsoft.Billing/billingAccounts/{BillingAccountID}"
    }
}

I tried to reproduce the same in my environment.

Tried with the properties mentioned in your json

Code:

main.tf:

   resource "azapi_resource" "symbolicname" {
      name      = "kavyaexample"
      parent_id = data.azurerm_resource_group.example.id
      type      = "Microsoft.CostManagement/views@2019-11-01"
      // location = "eastus"
      body = jsonencode({
        properties = {
          displayName = "myfilefmt"
          fileDestination = {
            fileFormats = "Csv"
          }
          notification = {
            language       = "en"
            message        = "this is test notif"
            regionalFormat = "string"
            subject        = "Test"
            to = [
              "xx@xxx.com"
            ]
          }
          notificationEmail = "string"
          schedule = {
            dayOfMonth = 19
            daysOfWeek = [
              "Thursday"
            ]
            endDate   = "2024-01-10T18:30:00.000Z"
            frequency = "weekly"
            //hourOfDay = int
            startDate = "2023-01-19T11:30:00.000Z"
            weeksOfMonth = [
              "string"
            ]
          }
          "scope" : "/providers/Microsoft.Billing/billingAccounts/xxxxx"
          "status" = "Enabled"
          "viewId" : "/providers/Microsoft.Billing/billingAccounts/xxxxe/providers/Microsoft.CostManagement/views/test",
        }
        kind = "Email"
      })
    }

在此处输入图像描述

Here install AzApi VSCode Extension to use the AzApi provider

Microsoft.CostManagement/views support following format as the document.

 resource "azapi_resource" "symbolicname" { type = "Microsoft.CostManagement/views@2019-11-01" name = "string" parent_id = "string" body = jsonencode({ properties = { accumulated = "string" chart = "string" displayName = "string" kpis = [ { enabled = bool id = "string" type = "string" } ] metric = "string" pivots = [ { ….. } ] query = { dataSet = { aggregation = {} configuration = { columns = [ "string" ] } filter = { and = [ { dimensions = { name = "string" operator = "string" values = [ "string" ] } or = [ { tagKey = { name = "string" operator = "string" values = [ "string" ] } tags = { name = "string" operator = "string" values = [ "string" ] } tagValue = { name = "string" operator = "string" values = [ "string" ] } } granularity = "string" grouping = [ { name = "string" type = "string" } ] sorting = [ { direction = "string" name = "string" } ] } timeframe = "string" timePeriod = { from = "string" to = "string" } type = "Usage" } scope = "string" } eTag = "string" }) }

It does not support following parameters.

So received the error:

Error: the `body` is invalid: 
│ `properties.fileDestination` is not expected here. Do you mean `properties.metric`? 
│ `properties.notification` is not expected here. Do you mean `properties.modifiedOn`?

在此处输入图像描述

Microsoft.CostManagement/scheduledActions has these parameters. azapi resource of Microsoft.CostManagement/scheduledActionsParentId can be tried giving as parentId in view.

Reference: Microsoft.CostManagement/scheduledActions - Bicep, ARM template & Terraform AzAPI reference | Microsoft Learn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM