簡體   English   中英

Terraform Azurerm:如果不存在則創建 blob

[英]Terraform Azurerm: Create blob if not exists

我從創建存儲帳戶、容器和塊 blob 的代碼中獲得了 Terra。 是否可以配置僅當塊 blob 不存在時才創建它?

在重新運行 terraform 的情況下,如果 blob 已經存在,我不想替換它,因為內容可能已被手動修改,我想保留它。

有小費嗎? 我唯一能想到的替代方案是在進一步的部署步驟中運行 powershell/bash 腳本,如果需要,它會創建文件,但我很好奇這是否可以僅使用 Terraform 來完成。

locals {
  storage_account_name_teast = format("%s%s", local.main_pw_prefix_short, "teast")
}

resource "azurerm_storage_account" "teaststorage" {
  name                            = local.storage_account_name_teast
  resource_group_name             = azurerm_resource_group.main.name
  location                        = var.location
  account_tier                    = var.account_tier
  account_replication_type        = var.account_replication_type
  allow_nested_items_to_be_public = false
  min_tls_version                 = "TLS1_2"

  network_rules {
    default_action = "Deny"
    bypass = [
      "AzureServices"
    ]
    virtual_network_subnet_ids = []
    ip_rules                   = local.ip_rules
  }
  tags = var.tags
}

resource "azurerm_storage_container" "teastconfig" {
  name                  = "config"
  storage_account_name  = azurerm_storage_account.teaststorage.name
  container_access_type = "private"
}


resource "azurerm_storage_blob" "teastfeaturetoggle" {
  name                   = "featureToggles.json"
  storage_account_name   = azurerm_storage_account.teaststorage.name
  storage_container_name = azurerm_storage_container.teastconfig.name
  type                   = "Block"
  source                 = "vars-pr-default-toggles.json"
}

掃描完 terraform 計划后,我發現它正在強制替換 blob,因為:

content_md5 = "9a95db04fb1ff3abcd7ff81fcfb96307" -> null # forces replacement

我將生命周期掛鈎添加到 blob 資源以防止它:

resource "azurerm_storage_blob" "teastfeaturetoggle" {
  name                   = "featureToggles.json"
  storage_account_name   = azurerm_storage_account.teaststorage.name
  storage_container_name = azurerm_storage_container.teastconfig.name
  type                   = "Block"
  source                 = "vars-pr-default-toggles.json"

  lifecycle {     
    ignore_changes = [       
      content_md5,     
    ]   
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM