无法通过Terraform在Azure中创建数据收集规则。

huangapple go评论99阅读模式
英文:

Unable to create Data Collection Rule via Terraform in Azure

问题

"我的代码如下-"
"错误"
"请帮助找出问题所在。"

英文:

My Code looks like-

#dcr
无法通过Terraform在Azure中创建数据收集规则。
Error

无法通过Terraform在Azure中创建数据收集规则。

Please help to find where the issue is.

答案1

得分: 0

I tried to provision a Data Collection Rule via Terraform and I was able to deploy successfully using my code.

To achieve this configuration, I referred the documentation from Terraform registry.

I also tried with the code mentioned in Image. The reason for error is incorrect destination names in data flows and version of providers.

By changing the names and update the provider to the latest version and rewriting the code as per the requirement we will be able to provision the resource mentioned.

My Main.tf:

  1. provider "azurerm" {
  2. features {}
  3. subscription_id = "00000000"
  4. client_id = "0000000"
  5. tenant_id = "0000000"
  6. client_secret = "0000000"
  7. }
  8. resource "azurerm_resource_group" "example" {
  9. name = "Demorg-vk"
  10. location = "East US"
  11. }
  12. resource "azurerm_user_assigned_identity" "example" {
  13. name = "demovk-uai"
  14. resource_group_name = azurerm_resource_group.example.name
  15. location = azurerm_resource_group.example.location
  16. }
  17. resource "azurerm_log_analytics_workspace" "example" {
  18. name = "demovk-workspace"
  19. resource_group_name = azurerm_resource_group.example.name
  20. location = azurerm_resource_group.example.location
  21. }
  22. resource "azurerm_log_analytics_solution" "example" {
  23. solution_name = "WindowsEventForwarding"
  24. location = azurerm_resource_group.example.location
  25. resource_group_name = azurerm_resource_group.example.name
  26. workspace_resource_id = azurerm_log_analytics_workspace.example.id
  27. workspace_name = azurerm_log_analytics_workspace.example.name
  28. plan {
  29. publisher = "Microsoft"
  30. product = "OMSGallery/WindowsEventForwarding"
  31. }
  32. }
  33. resource "azurerm_eventhub_namespace" "example" {
  34. name = "exeventns"
  35. location = azurerm_resource_group.example.location
  36. resource_group_name = azurerm_resource_group.example.name
  37. sku = "Standard"
  38. capacity = 1
  39. }
  40. resource "azurerm_eventhub" "example" {
  41. name = "exevent2"
  42. namespace_name = azurerm_eventhub_namespace.example.name
  43. resource_group_name = azurerm_resource_group.example.name
  44. partition_count = 2
  45. message_retention = 1
  46. }
  47. resource "azurerm_storage_account" "example" {
  48. name = "demovkstorage"
  49. resource_group_name = azurerm_resource_group.example.name
  50. location = azurerm_resource_group.example.location
  51. account_tier = "Standard"
  52. account_replication_type = "LRS"
  53. }
  54. resource "azurerm_storage_container" "example" {
  55. name = "demovkcontainer"
  56. storage_account_name = azurerm_storage_account.example.name
  57. container_access_type = "private"
  58. }
  59. resource "azurerm_monitor_data_collection_endpoint" "example" {
  60. name = "demovk-dcre"
  61. resource_group_name = azurerm_resource_group.example.name
  62. location = azurerm_resource_group.example.location
  63. lifecycle {
  64. create_before_destroy = true
  65. }
  66. }
  67. resource "azurerm_monitor_data_collection_rule" "example" {
  68. name = "demovkrule"
  69. resource_group_name = azurerm_resource_group.example.name
  70. location = azurerm_resource_group.example.location
  71. data_collection_endpoint_id = azurerm_monitor_data_collection_endpoint.example.id
  72. destinations {
  73. log_analytics {
  74. workspace_resource_id = azurerm_log_analytics_workspace.example.id
  75. name = "example-destination-log"
  76. }
  77. event_hub {
  78. event_hub_id = azurerm_eventhub.example.id
  79. name = "example-destination-eventhub"
  80. }
  81. storage_blob {
  82. storage_account_id = azurerm_storage_account.example.id
  83. container_name = azurerm_storage_container.example.name
  84. name = "example-destination-storage"
  85. }
  86. azure_monitor_metrics {
  87. name = "example-destination-metrics"
  88. }
  89. }
  90. data_flow {
  91. streams = ["Microsoft-InsightsMetrics"]
  92. destinations = ["example-destination-metrics"]
  93. }
  94. data_flow {
  95. streams = ["Microsoft-InsightsMetrics", "Microsoft-Syslog", "Microsoft-Perf"]
  96. destinations = ["example-destination-log"]
  97. }
  98. data_flow {
  99. streams = ["Custom-MyTableRawData"]
  100. destinations = ["example-destination-log"]
  101. output_stream = "Microsoft-Syslog"
  102. transform_kql = "source | project TimeGenerated = Time, Computer, Message = AdditionalContext"
  103. }
  104. data_sources {
  105. syslog {
  106. facility_names = ["*"]
  107. log_levels = ["*"]
  108. name = "example-datasource-syslog"
  109. }
  110. iis_log {
  111. streams = ["Microsoft-W3CIISLog"]
  112. name = "example-datasource-iis"
  113. log_directories = ["C:\\Logs\\W3SVC1"]
  114. }
  115. log_file {
  116. name = "example-datasource-logfile"
  117. format = "text"
  118. streams = ["Custom-MyTableRawData"]
  119. file_patterns = ["C:\\JavaLogs\\*.log"]
  120. settings {
  121. text {
  122. record_start_timestamp_format = "ISO 8601"
  123. }
  124. }
  125. }
  126. performance_counter {
  127. streams = ["Microsoft-Perf", "Microsoft-InsightsMetrics"]
  128. sampling_frequency_in_seconds = 60
  129. counter_specifiers = ["Processor(*)\\% Processor Time"]
  130. name = "example-datasource-perfcounter"
  131. }
  132. windows_event_log {
  133. streams = ["Microsoft-WindowsEvent"]
  134. x_path_queries = ["*![System/Level=1]"]
  135. name = "example-datasource-wineventlog"
  136. }
  137. extension {
  138. streams = ["Microsoft-WindowsEvent"]
  139. input_data_sources = ["example-datasource-wineventlog"]
  140. extension_name = "example-extension-name"
  141. extension_json = jsonencode({
  142. a = 1
  143. b = "hello"
  144. })
  145. name = "example-datasource-extension"
  146. }
  147. }
  148. stream_declaration {
  149. stream_name = "Custom-MyTableRawData"
  150. column {
  151. name = "Time"
  152. type = "datetime"
  153. }
  154. column {
  155. name = "Computer"
  156. type = "string"
  157. }
  158. column {
  159. name = "AdditionalContext"
  160. type = "string"
  161. }
  162. }
  163. identity {
  164. type = "UserAssigned"
  165. identity_ids = [azurerm_user_assigned_identity.example.id]
  166. }
  167. description = "data collection rule example"
  168. tags = {
  169. foo = "bar"
  170. }
  171. depends_on = [
  172. azurerm_log_analytics_solution.example
  173. ]
  174. }

This script is provided as a demo version. I took this code because the Query asked was Incomplete.

Output:

  1. terraform plan
  2. <details>
  3. <summary>英文:</summary>
  4. &gt; I tried to provision a Data Collection Rule via Terraform and I was able to deploy successfully using my code.
  5. To achieve this configuration, I referred the documentation from [Terraform](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_data_collection_rule) registry.
  6. I also tried with the code mentioned in Image. The reason for error is incorrect destination names in data flows and version of providers.
  7. By changing the names and update the provider to the latest version and rewriting the code as per the requirement we will be able to provision the resource mentioned.
  8. My **Main.tf**:
  9. provider &quot;azurerm&quot; {
  10. features {}
  11. subscription_id = &quot;00000000&quot;
  12. client_id = &quot;0000000&quot;
  13. tenant_id = &quot;0000000&quot;
  14. client_secret = &quot;0000000&quot;
  15. }
  16. resource &quot;azurerm_resource_group&quot; &quot;example&quot; {
  17. name = &quot;Demorg-vk&quot;
  18. location = &quot;East US&quot;
  19. }
  20. resource &quot;azurerm_user_assigned_identity&quot; &quot;example&quot; {
  21. name = &quot;demovk-uai&quot;
  22. resource_group_name = azurerm_resource_group.example.name
  23. location = azurerm_resource_group.example.location
  24. }
  25. resource &quot;azurerm_log_analytics_workspace&quot; &quot;example&quot; {
  26. name = &quot;demovk-workspace&quot;
  27. resource_group_name = azurerm_resource_group.example.name
  28. location = azurerm_resource_group.example.location
  29. }
  30. resource &quot;azurerm_log_analytics_solution&quot; &quot;example&quot; {
  31. solution_name = &quot;WindowsEventForwarding&quot;
  32. location = azurerm_resource_group.example.location
  33. resource_group_name = azurerm_resource_group.example.name
  34. workspace_resource_id = azurerm_log_analytics_workspace.example.id
  35. workspace_name = azurerm_log_analytics_workspace.example.name
  36. plan {
  37. publisher = &quot;Microsoft&quot;
  38. product = &quot;OMSGallery/WindowsEventForwarding&quot;
  39. }
  40. }
  41. resource &quot;azurerm_eventhub_namespace&quot; &quot;example&quot; {
  42. name = &quot;exeventns&quot;
  43. location = azurerm_resource_group.example.location
  44. resource_group_name = azurerm_resource_group.example.name
  45. sku = &quot;Standard&quot;
  46. capacity = 1
  47. }
  48. resource &quot;azurerm_eventhub&quot; &quot;example&quot; {
  49. name = &quot;exevent2&quot;
  50. namespace_name = azurerm_eventhub_namespace.example.name
  51. resource_group_name = azurerm_resource_group.example.name
  52. partition_count = 2
  53. message_retention = 1
  54. }
  55. resource &quot;azurerm_storage_account&quot; &quot;example&quot; {
  56. name = &quot;demovkstorage&quot;
  57. resource_group_name = azurerm_resource_group.example.name
  58. location = azurerm_resource_group.example.location
  59. account_tier = &quot;Standard&quot;
  60. account_replication_type = &quot;LRS&quot;
  61. }
  62. resource &quot;azurerm_storage_container&quot; &quot;example&quot; {
  63. name = &quot;demovkcontainer&quot;
  64. storage_account_name = azurerm_storage_account.example.name
  65. container_access_type = &quot;private&quot;
  66. }
  67. resource &quot;azurerm_monitor_data_collection_endpoint&quot; &quot;example&quot; {
  68. name = &quot;demovk-dcre&quot;
  69. resource_group_name = azurerm_resource_group.example.name
  70. location = azurerm_resource_group.example.location
  71. lifecycle {
  72. create_before_destroy = true
  73. }
  74. }
  75. resource &quot;azurerm_monitor_data_collection_rule&quot; &quot;example&quot; {
  76. name = &quot;demovkrule&quot;
  77. resource_group_name = azurerm_resource_group.example.name
  78. location = azurerm_resource_group.example.location
  79. data_collection_endpoint_id = azurerm_monitor_data_collection_endpoint.example.id
  80. destinations {
  81. log_analytics {
  82. workspace_resource_id = azurerm_log_analytics_workspace.example.id
  83. name = &quot;example-destination-log&quot;
  84. }
  85. event_hub {
  86. event_hub_id = azurerm_eventhub.example.id
  87. name = &quot;example-destination-eventhub&quot;
  88. }
  89. storage_blob {
  90. storage_account_id = azurerm_storage_account.example.id
  91. container_name = azurerm_storage_container.example.name
  92. name = &quot;example-destination-storage&quot;
  93. }
  94. azure_monitor_metrics {
  95. name = &quot;example-destination-metrics&quot;
  96. }
  97. }
  98. data_flow {
  99. streams = [&quot;Microsoft-InsightsMetrics&quot;]
  100. destinations = [&quot;example-destination-metrics&quot;]
  101. }
  102. data_flow {
  103. streams = [&quot;Microsoft-InsightsMetrics&quot;, &quot;Microsoft-Syslog&quot;, &quot;Microsoft-Perf&quot;]
  104. destinations = [&quot;example-destination-log&quot;]
  105. }
  106. data_flow {
  107. streams = [&quot;Custom-MyTableRawData&quot;]
  108. destinations = [&quot;example-destination-log&quot;]
  109. output_stream = &quot;Microsoft-Syslog&quot;
  110. transform_kql = &quot;source | project TimeGenerated = Time, Computer, Message = AdditionalContext&quot;
  111. }
  112. data_sources {
  113. syslog {
  114. facility_names = [&quot;*&quot;]
  115. log_levels = [&quot;*&quot;]
  116. name = &quot;example-datasource-syslog&quot;
  117. }
  118. iis_log {
  119. streams = [&quot;Microsoft-W3CIISLog&quot;]
  120. name = &quot;example-datasource-iis&quot;
  121. log_directories = [&quot;C:\\Logs\\W3SVC1&quot;]
  122. }
  123. log_file {
  124. name = &quot;example-datasource-logfile&quot;
  125. format = &quot;text&quot;
  126. streams = [&quot;Custom-MyTableRawData&quot;]
  127. file_patterns = [&quot;C:\\JavaLogs\\*.log&quot;]
  128. settings {
  129. text {
  130. record_start_timestamp_format = &quot;ISO 8601&quot;
  131. }
  132. }
  133. }
  134. performance_counter {
  135. streams = [&quot;Microsoft-Perf&quot;, &quot;Microsoft-InsightsMetrics&quot;]
  136. sampling_frequency_in_seconds = 60
  137. counter_specifiers = [&quot;Processor(*)\\% Processor Time&quot;]
  138. name = &quot;example-datasource-perfcounter&quot;
  139. }
  140. windows_event_log {
  141. streams = [&quot;Microsoft-WindowsEvent&quot;]
  142. x_path_queries = [&quot;*![System/Level=1]&quot;]
  143. name = &quot;example-datasource-wineventlog&quot;
  144. }
  145. extension {
  146. streams = [&quot;Microsoft-WindowsEvent&quot;]
  147. input_data_sources = [&quot;example-datasource-wineventlog&quot;]
  148. extension_name = &quot;example-extension-name&quot;
  149. extension_json = jsonencode({
  150. a = 1
  151. b = &quot;hello&quot;
  152. })
  153. name = &quot;example-datasource-extension&quot;
  154. }
  155. }
  156. stream_declaration {
  157. stream_name = &quot;Custom-MyTableRawData&quot;
  158. column {
  159. name = &quot;Time&quot;
  160. type = &quot;datetime&quot;
  161. }
  162. column {
  163. name = &quot;Computer&quot;
  164. type = &quot;string&quot;
  165. }
  166. column {
  167. name = &quot;AdditionalContext&quot;
  168. type = &quot;string&quot;
  169. }
  170. }
  171. identity {
  172. type = &quot;UserAssigned&quot;
  173. identity_ids = [azurerm_user_assigned_identity.example.id]
  174. }
  175. description = &quot;data collection rule example&quot;
  176. tags = {
  177. foo = &quot;bar&quot;
  178. }
  179. depends_on = [
  180. azurerm_log_analytics_solution.example
  181. ]
  182. }
  183. This script is provided as demo version. I took this code because Query asked was Incomplete.
  184. **Output :**
  185. terraform plan
  186. ![enter image description here](https://i.imgur.com/eWmuMmg.png)
  187. terraform apply
  188. ![enter image description here](https://i.imgur.com/i84Za6s.png)
  189. ![enter image description here](https://i.imgur.com/LLykLZR.png)
  190. By making the changes in the code snippet with the code as I mentioned. we will be able to provision the required resource in Portal.
  191. </details>

huangapple
  • 本文由 发表于 2023年7月6日 16:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定