Hi all,
I am trying to import multiple S3 buckets using the import block. Also I don’t want to use import command but want to execute using normal procedure of Init, Plan & Apply. I am trying to use for_each loop for this but getting error. Below is the code from terraform site:
Site:- Import - Configuration Language | Terraform | HashiCorp Developer
Code:-
locals {
buckets = {
“staging” = “bucket1”
“uat” = “bucket2”
“prod” = “bucket3”
}
}
import {
for_each = local.buckets
to = aws_s3_bucket.this[each.key]
id = each.value
}
resource “aws_s3_bucket” “this” {
for_each = local.buckets
}
#########################Error msg########################
│ Error: Invalid expression
│
│ on import.tf line 10, in import:
│ 10: to = aws_s3_bucket.this[each.key]
│
│ A single static variable reference is required: only attribute access and indexing with constant keys. No calculations, function
│ calls, template expressions, etc are allowed here.
╵
╷
│ Error: Variables not allowed
│
│ on import.tf line 11, in import:
│ 11: id = each.value
│
│ Variables may not be used here.
╵
╷
│ Error: Unsuitable value type
│
│ on import.tf line 11, in import:
│ 11: id = each.value
│
│ Unsuitable value: value must be known