terraform-provider-azureazure-storage-filesterraform0.12+

How to copy a file from local machine to a storage account using Terraform


I have created a storage account and created a file share in it too using Terraform. I want to copy some files from my local machine to that storage account using Terraform. Is that possible? If yes, then how?

Here is my code-

provider "azurerm" {
  # whilst the `version` attribute is optional, we recommend pinning to a given version of the Provider
  version = "=2.0.0"
  subscription_id = "xxx-xxxx-xxxx-xxxx"
  tenant_id = "xxxxx-xxxxx-xxxx-xxxx"
  features {}
}
resource "azurerm_resource_group" "example" {
  name     = "azuretest"
  location = "West Europe"
}
resource "azurerm_storage_account" "example" {
  name                     = "asdfghjklteststorage"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}


resource "azurerm_storage_share" "example" {
  name                 = "sharename"
  storage_account_name = azurerm_storage_account.example.name
  quota                = 50

  acl {
    id = "xxx-xxx-xxx-xxx"

    access_policy {
      permissions = "rwdl"
      start       = "2020-05-10T09:38:21.0000000Z"
      expiry      = "2020-07-02T10:38:21.0000000Z"
    }
  }
}
resource "null_resource" "uploadfile" {

  provisioner "local-exec" {


  command = <<-EOT
  $storageAcct = Get-AzStorageAccount -ResourceGroupName "${azurerm_resource_group.example.name}" -Name "${azurerm_storage_account.example.name}"
   Set-AzStorageFileContent `
   -Context $storageAcct.Context `
   -ShareName "${azurerm_storage_share.example.name}" `
   -Source "C:\Users\xxx\xxx\xxx\Untitled.png" `
   -Path "Untitled.png"

  EOT

  interpreter = ["PowerShell", "-Command"]
  }
}  


Solution

  • You could use local-exec provisioner invokes a local executable after a resource is created. See upload a file to Azure file share with PowerShell.

    For example,

        resource "azurerm_storage_share" "example" {
          name                 = "sharename"
          storage_account_name = azurerm_storage_account.example.name
          quota                = 50
    
          acl {
            id = "xxxxxxxxxxxxxxx"
    
            access_policy {
              permissions = "rwdl"
              start       = "2020-05-10T09:38:21.0000000Z"
              expiry      = "2020-07-02T10:38:21.0000000Z"
            }
          }
        }
    
        resource "null_resource" "uploadfile" {
    
          provisioner "local-exec" {
    
    
          command = <<-EOT
          $storageAcct = Get-AzStorageAccount -ResourceGroupName "${azurerm_resource_group.example.name}" -Name "${azurerm_storage_account.example.name}"
           Set-AzStorageFileContent `
           -Context $storageAcct.Context `
           -ShareName "${azurerm_storage_share.example.name}" `
           -Source "C:\Users\xxx\terraform\test.txt" `
           -Path "test.txt"
    
          EOT
    
          interpreter = ["PowerShell", "-Command"]
          }
    
    }
    

    Result

    enter image description here

    For more information, you could read this blog.