0

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

1 Answers1

0

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"
}
Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Ok thanks. This helps a little but unfortunately i don't want to use AZ cli (because of vendor lockin) I think i have to create my storage account first and then only use the blobobject resource to create my files. –  Sep 10 '19 at 08:26
  • @immae1nova What language do you want to use? This is just an example, you can use another script as you wish. Powershell or the REST API. – Charles Xu Sep 10 '19 at 08:28
  • ah okay maybe REST API with python or so could be the thing! Thanks for clarification :) –  Sep 10 '19 at 09:56
  • @immae1nova Well, if you think it's OK, you can accept it as the answer. – Charles Xu Sep 10 '19 at 10:00