简体   繁体   English

Terraform Azurerm:如果不存在则创建 blob

[英]Terraform Azurerm: Create blob if not exists

I got Terrafrom code that creates storage account, container and block blob.我从创建存储帐户、容器和块 blob 的代码中获得了 Terra。 Is it possible to configure that block blob is created only if it doesn't already exist?是否可以配置仅当块 blob 不存在时才创建它?

In case of re-running terraform I wouldn't like to replace blob if it is already there as the content might have been manually modified and i would like to keep it.在重新运行 terraform 的情况下,如果 blob 已经存在,我不想替换它,因为内容可能已被手动修改,我想保留它。

Any tips?有小费吗? Only alternative I could think of is running powershell/bash script during further deployment steps that would create file if needed, but I am curious if this can be done just with Terraform.我唯一能想到的替代方案是在进一步的部署步骤中运行 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"
}

After scanning through terraform plan I figured out it was forcing a blob replacement because of:扫描完 terraform 计划后,我发现它正在强制替换 blob,因为:

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

I added lifecycle hook to blob resource to prevent it:我将生命周期挂钩添加到 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.

相关问题 terraform 如果不存在则有条件地创建 ECR 存储库 - terraform to conditionally create ECR repository if not exists Terraform 导入 azurerm_role_assignment - Terraform Import azurerm_role_assignment Terraform azurerm 读取当前登录用户? - Terraform azurerm read current signed in user? terraform azurerm - 不能破坏公共 ip - terraform azurerm - cannot destroy public ip Terraform azurerm_windows_function_app ip_restrictions 问题 - Terraform azurerm_windows_function_app ip_restrictions issues terraform 错误:周期:data.azurerm_key_vault_secret - terraform Error: Cycle: data.azurerm_key_vault_secret terraform azurerm_data_factory_pipeline 将类型分配给变量 - terraform azurerm_data_factory_pipeline assing type to the variables terraform azurerm:错误:退出状态 1 - 需要 az 登录 - terraform azurerm : ERROR : exit status 1 - az login required Terraform AzureRM 不断修改 API 使用默认端点的代理配置进行管理 - Terraform AzureRM Continually Modifying API Management with Proxy Configuration for Default Endpoint Terraform - Azure - 同时使用“azurerm_windows_virtual_machine”和“azurerm_mssql_virtual_machine” - 但未配置 SQL 存储 - Terraform - Azure - Using "azurerm_windows_virtual_machine" and "azurerm_mssql_virtual_machine" together - but SQL Storage isn't getting configured
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM