i want to upload a generated file as new blobobject to azure. My problem is, that if the azurerm_storage_container doesn't exists i have to create it as a resource. But if this container is already present in the storage account i get the error
'The specified container already exists'.
So i thought i could define my azurerm_storage_container as Data object. But then i get a error that
module.components.data.azurerm_storage_container.blobstorage: Provider doesn't support data source: azurerm_storage_container
To clarify: i want to create a new storage-container if there is not present in azure. after that i want to upload my generated local file into a new blobobject with is included into the storage-container.
I tried to set all objects as resource and data. i don't find the right combination of this objects to achieve my goal
As I know, you just can use the Terraform external data source to execute a script to get the info if the container has existed, then create the container or not according to the state.
Here is the example code with a bash script using the Azure CLI command:
#!/bin/bash
eval "$(jq -r '@sh "export container_name=\(.container_name) account_name=\(.account_name)"')"
flag=$(az storage container exists --name $container_name --account-name $account_name --query exists)
if [ $flag ]
then
echo "{\"exists\":\"True\"}"
else
echo "{\"exists\":\"False\"}"
fi
Terraform:
variable "container_name" {}
data "azurerm_storage_account" "test" {
name = "charlescloudshell"
resource_group_name = "v-chaxu-ChinaCXPTeam"
}
data "external" "exists" {
program = ["/bin/bash", "./container.sh"]
query = {
container_name = "${var.container_name}"
account_name = "${data.azurerm_storage_account.test.name}"
}
}
resource "azurerm_storage_container" "test" {
count = "${data.external.exists.result["exists"] == "False" ? 1 : 0}"
name = "${var.container_name}"
storage_account_name = "${data.azurerm_storage_account.test.name}"
container_access_type = "private"
}