[英]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.