3

As part of an Azure ACI definition Terraform script, I'm creating an azurerm_storage_share which I want to then upload some files to, before mounting to my container.

As far as I can tell, the right way to access the share once created is via SMB. This rules out all the Terraform provisioners (except local-exec) which support only SSH or WinRM.

So I'm thinking the most efficient, maintainable way to do this is to local-exec to a script that runs azure-cli commands like az storage upload ... ? That way TF does what it does best (orchestration) and have az do config provisioning.

Seffyroff
  • 33
  • 3

2 Answers2

1

Yes, you are right. Terrafrom could not directly access to Azure File share and upload files to it.

Based on my knowledge, maybe you could store your files in a Linux VM and install Azure Cli 2.0 on it. Then you could write a script to upload files to Azure file share like below:

#!/bin/bash
# A simple Azure Storage example script

export AZURE_STORAGE_ACCOUNT=<storage_account_name>
export AZURE_STORAGE_ACCESS_KEY=<storage_account_key>

az storage file upload --share-name myshare --source ~/temp/samplefile.txt

More information about this please refer to this link.

Then you could SSH to this VM and execute this script on Terraform.

Shui shengbao
  • 3,503
  • 1
  • 10
  • 20
  • Thanks. It's a working solution, I'll give it a try. I would prefer something that doesn't require an intermediate VM to spin up, perform configuration, then tear down again, but it looks to be the only way right now. I can't see a serverless solution either. Actually, rolling another container to perform configuration would probably make the most sense. – Seffyroff Nov 22 '17 at 18:54
  • @TheOperator Based on my knowledge, it is not possible with a server to execute a script. – Shui shengbao Nov 23 '17 at 00:31
  • Given that the container I'm deploying is actually a custom web server running a webapp, might I actually be better off using the web app for containers service? Is that service implemented in Terraform? – Seffyroff Nov 23 '17 at 19:17
  • Hi, web app do you mean Azure web app(Paas) or web app on a VM? – Shui shengbao Nov 24 '17 at 05:26
0

If you want to upload a file to a container in a storage account in Azure, you can use this resource:

resource "azurerm_storage_blob" "copyfile" {
  name                   = "remotefilename.txt"
  storage_account_name   = azurerm_storage_account.mystorageaccount.name
  storage_container_name = azurerm_storage_container.mycontainer.name
  type                   = "Block"
  source                 = "localfilename.txt"
}

More info from TF docs:

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

james.garriss
  • 360
  • 6
  • 17