[英]Setting CORS in Azure storage account from Terraform
我編寫了一些 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 可以單獨應用於各種存儲
我想申請所有類型的對象
我認為可能的解決方案是在 Terraform 中執行 Azure CLI 命令。
我發現 CLI 命令az storage cors add
可以將 cors 規則添加到所有服務,如果您將參數--services
設置為bfqt
。 然后就可以使用 Terraform null_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 命令更方便,更合適。
這里的文檔不清楚,但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": {
// ...
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
}
}
}
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"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.