Unable to read all container names and copied into new containers

Hi All,

As part of migration we are trying to replicate like read all existing azure storage container and trying to create into new storage account containers

below is the code:

data "azurerm_storage_account" "example" {
  name                = "xxxxxxxxxx"
  resource_group_name = data.azurerm_resource_group.rg.name

data "azurerm_storage_container" "example" {
  name                 = "*"
  storage_account_name = data.azurerm_storage_account.example.name

resource "azurerm_storage_account" "this" {
  name                     = "tstgsanthioc"
  resource_group_name      = data.azurerm_resource_group.rg.name
  location                 = data.azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"

resource "azurerm_storage_container" "example" {
  for_each              = data.azurerm_storage_container.example

  name                  = each.key
  storage_account_name  = azurerm_storage_account.this.name

when i do apply : getting this error

 Error: retrieving Container "*" (Account "xxxxxxxxxxxx" / Resource Group "xxxxxxxxxxx"): containers.Client#GetProperties: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="OutOfRangeInput" Message="The specified resource name length is not within the permissible limits.\nRequestId:9644b0d9-001e-001e-64cf-92e20e000000\nTime:2023-05-30T08:21:32.4257490Z"
│   with data.azurerm_storage_container.example,
│   on main.tf line 71, in data "azurerm_storage_container" "example":
│   71: data "azurerm_storage_container" "example" {

[ERROR] The original error message:
Command '['terraform', 'plan', '-out', '.tfplan']' returned non-zero exit status 1.

[ERROR] The 'linux_vm' was not applied. See above error message.

To narrowly answer your specific question: You have made an incorrect assumption that you can use "*" as a wildcard to get multiple results. If you review the documentation for the

data source that you used, you’ll find that it looks up a single storage container by name, only.

I also have a broader observation about what you are trying to do here:

Terraform is designed to enable people to write a description of their infrastructure, and then have Terraform perform the necessary API calls to make that description a reality.

It is not designed to clone existing infrastructure. By trying to press it into a role for which it was never intended to be used, you’re likely to encounter a lot more challenges than someone using Terraform in the way it is intended to be used.

In my opinion, you need to make a choice: Will you be using Terraform to manage your infrastructure long term, for ongoing changes? Or are you just trying to build a use-once-and-then-throw-away copy script?

If the first, then you should write a Terraform configuration that defines what to create, without making runtime references to the old infrastructure.

If the second, Terraform is not the right tool for you - use your scripting language of choice and whatever support for calling Azure APIs exists for it.

Thanks for your reply,

I agree what you have suggested but we are trying to move all existing storage account along with containers from one subscription to another subscription with terraform

so I need help how we can read this containers with terraform…

If claim you agree with what I’ve suggested…

but then immediately follow up saying that you’re going to attempt to continue doing the thing I strongly suggested against, then you are not agreeing with me - you are showing there is no productive way for us to continue this conversation.

I’m pretty sure what you want to do is impossible.

As you said correct i have tried but no use.

let me create only storage account and i will use some cli tools to copy contianers along with data.

Thanks for your support