hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

Home Page:https://www.terraform.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Import statement must not give and error when having workspace

alecscodehub opened this issue · comments

Terraform Version

Terraform v1.5.5
on darwin_amd64
+ provider registry.terraform.io/hashicorp/archive v2.1.0
+ provider registry.terraform.io/hashicorp/aws v4.9.0

Your version of Terraform is out of date! The latest version
is 1.8.5. You can update by downloading from https://www.terraform.io/downloads.html

Use Cases

When using

import {
  to = aws_identitystore_group_membership.example
  id = "d-0000000000/00000000-0000-0000-0000-000000000000"
}

and you import for default workspace all ok, but when you import and you have several workspace you getting a error and you dont have a way to just say apply this only for this workspace.

Attempted Solutions

The solution is to comment the code which is not "clean"
/*
import {
to = aws_identitystore_group_membership.example
id = "d-0000000000/00000000-0000-0000-0000-000000000000"
}
*/

Proposal

Allow Count inside a import.

import {
count = terraform.workspace == "dev" ? 1 : 0
to = aws_identitystore_group_membership.example
id = "d-0000000000/00000000-0000-0000-0000-000000000000"
}

References

No response

Hi @alecscodehub,

import blocks deliberately do not have a count meta-argument, because instances to be imported always have unique identities unrelated to the order the instances in the state. You can still use the same conditional expressions in the for_each argument, or provide the id's more dynamically via input variables.

locals {
  group_membership = terraform.workspace == "dev" : ["d-0000000000/00000000-0000-0000-0000-000000000000"] : []
}
import {
  for_each = local.group_membership
  to = aws_identitystore_group_membership.example
  id = each.value
}

That solution sounds great @jbardin but i get this error "Unexpected attribute: An attribute named "for_each" is not expected here". Correct me if im wrong but you cant put foreach of count in an import statement.

@alecscodehub, you are using an out of date version of Terraform. Support for for_each was added in v1.7.