gpt4 book ai didi

azure - 在 Terraform 中创建和链接 Azure 资源

转载 作者:行者123 更新时间:2023-12-03 02:05:41 27 4
gpt4 key购买 nike

需要通过 Terraform 创建 Azure 资源(例如 Application Insights、Key Vault 和 Log Analytics)并将其链接到 APIM。我浏览了 Terraform 文档和其他网站,但找不到任何示例。这是我的 Terraform 脚本,用于初始化资源组下的资源,但登录 Azure 门户后需要链接 APIM 和 Application Insights、Key Vault 和 Log Analytics。我期待创建和链接资源,并避免在 Azure 门户中手动链接。

    terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0.2"
}
}
required_version = ">= 1.1.0"
}

provider "azurerm" {
features {}
}

data "azurerm_client_config" "current" {}


#APIM Resource
resource "azurerm_resource_group" "TerraformPOC-DevResourceGroup" {
name = "TerraformPOC-DevResourceGroup"
location = "WestEurope"
}


resource "azurerm_application_insights" "TerraformPOC-Application-Insights" {
name = "TerraformPOC-Application-Insights"
location = azurerm_resource_group.TerraformPOC-DevResourceGroup.location
resource_group_name = azurerm_resource_group.TerraformPOC-DevResourceGroup.name
application_type = "other"
}


resource "azurerm_api_management" "TerraformPOC-APIManagement" {
name = "TerraformPOC-APIManagement"
location = azurerm_resource_group.TerraformPOC-DevResourceGroup.location
resource_group_name = azurerm_resource_group.TerraformPOC-DevResourceGroup.name
publisher_name = "TestDemo"
publisher_email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="60101201040505104e0d01140801040120010d01040515134e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>"
sku_name = "Developer_1"
}


resource "azurerm_log_analytics_workspace" "TerraformPOC-Log-Analytics" {
name = "TerraformPOC-Log-Analytics"
location = azurerm_resource_group.TerraformPOC-DevResourceGroup.location
resource_group_name = azurerm_resource_group.TerraformPOC-DevResourceGroup.name
retention_in_days = 30
}

最佳答案

我尝试在我的环境中重现该场景:

我使用以下代码将日志分析工作区链接到 azure keyvalue:

代码:

resource "azurerm_key_vault" "test" {
name = "kavymykeyvault"
resource_group_name = data.azurerm_resource_group.example.name
location = data.azurerm_resource_group.example.location
enabled_for_disk_encryption = true
tenant_id = data.azurerm_client_config.current.tenant_id
soft_delete_retention_days = 7
purge_protection_enabled = false

sku_name = "standard"

access_policy {
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id

key_permissions = [
"Get"
]

secret_permissions = [
"Get"
]

storage_permissions = [
"Get"
]
}

}

resource "azurerm_log_analytics_workspace" "test" {
name = "myloganalyticskav"
resource_group_name = data.azurerm_resource_group.example.name
location = data.azurerm_resource_group.example.location
}

resource "azurerm_storage_account" "test" {
name = "kamystorageaccountname"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
account_tier = "Standard"
account_replication_type = "LRS"
}

resource "azurerm_monitor_diagnostic_setting" "test" {
name = "kavyaexamplediag"
target_resource_id = azurerm_key_vault.test.id
storage_account_id = azurerm_storage_account.test.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id

log {
category = "AuditEvent"
enabled = false

retention_policy {
enabled = false
}
}

metric {
category = "AllMetrics"

retention_policy {
enabled = false
}
}
}

并且可以创建成功

enter image description here

门户:

enter image description here

您可以使用以下代码以相同的方式将azure应用程序见解链接到APIM

代码:

resource "azurerm_application_insights" "example" {
name = "kaaexample-appinsights"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
application_type = "web"
}

resource "azurerm_api_management" "example" {
name = "kavyaaaexample-apim"
location = data.azurerm_resource_group.example.location
resource_group_name = data.azurerm_resource_group.example.name
publisher_name = "My Company"
publisher_email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20434f4d50414e59605445525241464f524d0e494f" rel="noreferrer noopener nofollow">[email protected]</a>"
sku_name = "Developer_1"
}
resource "azurerm_api_management_logger" "example" {
name = "kaavexample-apimlogger"
api_management_name = azurerm_api_management.example.name
resource_group_name = data.azurerm_resource_group.example.name

application_insights {
instrumentation_key = azurerm_application_insights.example.instrumentation_key
}
}

resource "azurerm_api_management_diagnostic" "example" {
identifier = "applicationinsights"
resource_group_name = data.azurerm_resource_group.example.name
api_management_name = azurerm_api_management.example.name
api_management_logger_id = azurerm_api_management_logger.example.id

sampling_percentage = 5.0
always_log_errors = true
log_client_ip = true
verbosity = "verbose"
http_correlation_protocol = "W3C"

frontend_request {
body_bytes = 32
headers_to_log = [
"content-type",
"accept",
"origin",
]
}

frontend_response {
body_bytes = 32
headers_to_log = [
"content-type",
"content-length",
"origin",
]
}

backend_request {
body_bytes = 32
headers_to_log = [
"content-type",
"accept",
"origin",
]
}

backend_response {
body_bytes = 32
headers_to_log = [
"content-type",
"content-length",
"origin",
]
}
}

enter image description here

关于azure - 在 Terraform 中创建和链接 Azure 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74949849/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com