terraformazure-functionsterraform-provider-azure

Update just blob contents with terraform


I have a block like this in terraform:

resource "azurerm_storage_blob" "function_code" {
  name                   = "sample-func.zip"
  storage_account_name   = azurerm_storage_account.st-func.name
  storage_container_name = azurerm_storage_container.container-deploy.name
  type                   = "Block"
  source                 = "sample-func.zip"
}

Later on this blob is referenced as a source for Function app code.

If nothing changes in my terraform definition, but i have changed the contents of sample-func.zip, how can I update the blob contents and redeploy Functions App code with terraform cli?

I have tried just changing the file and checked if terraform picks that up as state change, but no luck. I ended up just destroying the whole infra and recreating, but that is a pain in the ass


Solution

  • The easiest way to do this is by adding a hash with md5:

    resource "azurerm_storage_blob" "function_code" {
      name                   = "sample-func.zip"
      storage_account_name   = azurerm_storage_account.st-func.name
      storage_container_name = azurerm_storage_container.container-deploy.name
      type                   = "Block"
      source                 = "sample-func.zip"
      content_md5            = filemd5("sample-func.zip")
    }
    

    This forces terraform to compare the hashes of the content, which means when the content changes, the hash will change and terraform will know to update the blob.

    from: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_blob