Tuesday, June 07, 2022

terraform import: module + for_each

very useful info!

Terraform Imports: Resources, Modules, for_each, and Count | by Tyler DeKnecht | The Startup | Medium

"child" module

medium_demo.tf

variable "user_name" {}

resource "aws_iam_user" "user" {
  for_each = toset(var.user_name) # receiving var values list
  name = each.value # "iterator", creating iam users from list 
  # ...
}


"parent" block, "calling" child module medium_demo.tf

main.tf

module "iam" {
  source = "../modules/iam/medium_demo" 
  user_name = ["bill"] # defines list of values passed to module
  # ...
}

Here is the actual import command; notice escaped double quote \" that is required.
Final "bill" is AWS IAM ID of resource being imported, in this case username

> terraform import module.iam.aws_iam_user.user[\"bill\"] bill



No comments: