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.6k stars 4.64k forks source link

Scheduled Query Rule Alert creation fails if done directly after creating the Log Analytics Workspace it intends to use #17145

Open Cosmin-Apopei opened 2 years ago

Cosmin-Apopei commented 2 years ago

Is there an existing issue for this?

Community Note

Terraform Version

1.2.1

AzureRM Provider Version

3.8.0

Affected Resource(s)/Data Source(s)

azurerm_monitor_scheduled_query_rules_alert, azurerm_log_analytics_workspace

Terraform Configuration Files

resource "azurerm_log_analytics_workspace" "example" {
  name                = "example"
  location            = "uksouth"
  resource_group_name = "RGname"
  sku                 = "PerGB2018"
  retention_in_days   = 30
  tags = var.tags
}

resource "azurerm_monitor_scheduled_query_rules_alert" "alert" {
  name = "someRandomName"
  description = "does not work"
  query = "CDBDataPlaneRequests | where DurationMs > 3000 and TimeGenerated >= ago(5m)"

  resource_group_name = "RGname"
  data_source_id = azurerm_log_analytics_workspace.example.id

  action {
    action_group = "actionGroupID"
  }

  location = "uksouth"

  severity = 1
  frequency = 5
  time_window = 5

  trigger {
    operator = "GreaterThanOrEqual"
    threshold = 1
  }

   depends_on = [
    azurerm_log_analytics_workspace.example
  ]
}

Debug Output/Panic Output

╷
│ Error: creating or updating Monitor Scheduled Query Rules: (Scheduled Query Rule Name "someRandomName" / Resource Group "RGname"): insights.ScheduledQueryRulesClient#CreateOrUpdate: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="NotFound" Message="{\r\n  \"error\": {\r\n    \"message\": \"The workspace could not be found\",\r\n    \"code\": \"WorkspaceNotFoundError\",\r\n    \"correlationId\": \"d55d0c7a-81a0-4746-bf10-2ddf43b83a99\"\r\n  }\r\n}"
│ 
│   with module.default_cosmos_sqa.module.excessive_http_error_codes_sqa.azurerm_monitor_scheduled_query_rules_alert.alert,
│   on ../../../../terraform-modules/modules/standard-modules/monitor-scheduled-query-alert/main.tf line 1, in resource "azurerm_monitor_scheduled_query_rules_alert" "alert":
│    1: resource "azurerm_monitor_scheduled_query_rules_alert" "alert" {
│

Expected Behaviour

The Scheduled Query Rule Alert should create.

Actual Behaviour

The Scheduled Query Rule Alert does not create because of a 404 error. Note that if you are to wait about a minute and rerun the Terraform file, the Scheduled Query Rule Alert will create (probably because the Log Analytics Workspace has existed for long enough)

Steps to Reproduce

  1. Create a TF file that creates a azurerm_log_analytics_workspace and a azurerm_monitor_scheduled_query_rules_alert
  2. terraform apply <-- error appears here
  3. wait a minute
  4. terraform apply <-- this time it will work

Important Factoids

No response

References

No response

teowa commented 2 years ago

Hi @Cosmin-Apopei , thank you for the issue. I have tried several times with given configuration with same location and version but it can work well.

Maybe you can get more logs or details for debug. Please refer to Debugging Terraform, you can set environment variableexport TF_LOG=debug and export TF_LOG_PATH=./terraform.log to get the terraform apply log. If in the log azurerm_log_analytics_workspace is successfully created but later not found in Get method, it may be a Azure API issue.

Here is an example log of a successful creation, the PUT request will return 201 and a GET request after the PUT will return 200:

...
 2022-06-09T11:53:09.141+0800 [DEBUG] provider.terraform-provider-azurerm_v3.8.0_x5: AzureRM Request:
 PUT /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/test-rg/providers/Microsoft.OperationalInsights/workspaces/test-law?api-version=2020-08-01 HTTP/1.1^M
...
2022-06-09T11:53:18.261+0800 [DEBUG] provider.terraform-provider-azurerm_v3.8.0_x5: AzureRM Response for https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/test-rg/providers/Microsoft.OperationalInsights/workspaces/test-law?api-version=2020-08-01:
 HTTP/2.0 201 Created^M
...
 2022-06-09T11:53:19.957+0800 [DEBUG] provider.terraform-provider-azurerm_v3.8.0_x5: AzureRM Request:
 GET /subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/test-rg/providers/Microsoft.OperationalInsights/workspaces/test-law?api-version=2020-08-01 HTTP/1.1^M
...
2022-06-09T11:53:21.663+0800 [DEBUG] provider.terraform-provider-azurerm_v3.8.0_x5: AzureRM Response for https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/test-rg/providers/Microsoft.OperationalInsights/workspaces/test-law?api-version=2020-08-01:
 HTTP/2.0 200 OK^M

If you have further findings, feel free to reply here.

OmarHawk commented 1 year ago

I do have the same issue. I'll check the logs as you mentioned and see if I'd rather create an issue with the Azure API for that...

ziyeqf commented 1 year ago

I assume it's almost same with https://github.com/hashicorp/terraform-provider-azurerm/issues/21627#issuecomment-1536148346