2

我编写了一些 Terraform 代码来创建 Azure 存储帐户。这是代码:

resource "azurerm_storage_account" "i_ten_prov_storage" {
  name                     = "${var.storage_account_name}"
  resource_group_name      = "${azurerm_resource_group.i_resource_group.name}"
  location                 = "${var.location}"
  account_tier             = "Standard"
  account_replication_type = "LRS"
  account_kind             = "StorageV2"

/*  cors_rule {
    allowed_headers = "${var.allowed_headers}"
    allowed_methods = "${var.allowed_methods}"
    allowed_origins = "${var.allowed_origins}"
    exposed_headers = "${var.exposed_headers}"
    max_age_in_seconds = "${var.max_age_in_seconds}"
  }*/
  cors_rule {
    allowed_headers = ["*"]
    allowed_methods = ["*"]
    allowed_origins = ["*"]
    exposed_headers = ["*"]
    max_age_in_seconds = ["*"]
  }
  tags = "${local.tags}"
}

我正在关注这个文档,它说在 Terraform 中允许 CORS 用于 azure 存储帐户: https ://www.terraform.io/docs/providers/azurerm/r/storage_account.html#allowed_headers

但是我在运行时收到以下错误terraform apply

Error: azurerm_storage_account.idl_tenant_provisioning_storage: : invalid or unknown key: cors_rule
ERROR: Job failed: exit code 1

我正在使用 Terraform 0.11.11。

从 azure 门户中,我可以看到 cors 可以单独应用于各种存储

来自 Azure 门户的屏幕截图

我想申请各种对象

4

4 回答 4

1

此处的文档不清楚,但该cors_rule块应嵌套在一个queue_properties块下,如资源文档中所述

queue_properties块支持以下内容:

  • cors_rule- (可选)cors_rule如下定义的块。

  • logging- (可选)logging如下定义的块。

  • minute_metrics- (可选)minute_metrics如下定义的块。

  • hour_metrics- (可选)hour_metrics如下定义的块。

您还可以在源代码中资源的架构中看到这一点:

// ...
            "queue_properties": {
                Type:     schema.TypeList,
                Optional: true,
                Computed: true,
                MaxItems: 1,
                Elem: &schema.Resource{
                    Schema: map[string]*schema.Schema{
                        "cors_rule": {
                            // ...
于 2020-02-03T14:32:49.997 回答
1

我认为可能的解决方案是在 Terraform 中执行 Azure CLI 命令。

如果您将参数设置为value ,我发现 CLI 命令az storage cors add可以将 cors 规则添加到所有服务。然后就可以使用 Terraform来执行命令了。示例代码如下所示:--servicesbfqtnull_resource

resource "null_resource" "test" {
    provisioner "local-exec" {
        command = "az storage cors add --methods GET POST PUT --origins '*' --services bqft --account-name xxx"       
    }
}

您可以根据需要在 CLI 命令中使用更多参数。PowerShell 命令Set-AzStorageCORSRule,但 CLI 命令更方便、更合适。

于 2020-02-04T13:47:21.130 回答
0
resource "azurerm_storage_account" "storage" {
  name                      = var.storage_account_name
  resource_group_name       = data.terraform_remote_state.rg.outputs.rg_name
  location                  = data.terraform_remote_state.rg.outputs.rg_location
  account_tier              = "Standard"
  account_replication_type  = "LRS"

  allow_blob_public_access  = true

  blob_properties {
    cors_rule {
      allowed_headers = ["*"]
      allowed_methods = ["GET","HEAD","OPTIONS","PUT"]
      allowed_origins = ["https://google.ga", "http://localhost:4200"]
      exposed_headers = ["*"]
      max_age_in_seconds = 200
    }
  }

  tags = {
    Environment = "QA"
    Team        = "Yes"
  }
}
于 2021-07-29T19:27:26.763 回答
0

cors_rule 必须在blob_properties块内。

resource "azurerm_storage_account" "strgacc" {
  name                     = "strgacc"
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "GRS"
  blob_properties{
    cors_rule{
        allowed_headers = ["*"]
        allowed_methods = ["GET","HEAD","POST","PUT"]
        allowed_origins = ["https://example.com"]
        exposed_headers = ["*"]
        max_age_in_seconds = 3600
        }
    }
}
于 2021-01-11T15:08:04.467 回答