What is a good way to mount an Azure File Storage automatically during provisioning on VMs?
There is a Microsoft doc describing how a file share can be mounted in general. This requires that Azure CLI is installed and a user is logged in.
As an alternative, I have created a working setup using terraform working on local host:
First, the existing storage account needs to be defined:
data "azurerm_storage_account" "storage-account" {
name = "storageaccount"
resource_group_name = "RG-storage-account"
}
When a VM is provisioned, the share is mounted by a remote execution provisionier:
resource "azurerm_virtual_machine" "vm" {
# ...
# stuff
provisioner "remote-exec" {
inline = [
"sudo mkdir -p /mnt/${data.azurerm_storage_account.storage-accoun.name}/share",
"sudo mount -t cifs //${data.azurerm_storage_account.storage-account.name}.file.core.windows.net/share /mnt/${data.azurerm_storage_account.storage-account.name}/share -o vers=3.0,dir_mode=0755,file_mode=0755,serverino,username=${data.azurerm_storage_account.storage-account.name},password=${data.azurerm_storage_account.storage-account.primary_access_key}",
]
}
}
Similar it is also possible to inject the mount to /etc/fstab
, also by using cloud init.
However, after applying, the key (accessed as primary_access_key
) remains in the terraform state. Is this considered safe? Is there a way to delete the key from the state? Also, when using cloud-init, the key will remain in the custom data of the VM.
Is there another safe way to mount data on the VM without storing the keys?