Importing SSM Parameter

I would like to import existing SSM parameters without overwrite value_wo but after import there is detected change for value_wo_version so value_wo is updated

my current setup

locals {
  workspace     = "development"
  instance      = "a"
  instance_name = "test-${local.instance}"

  api = {
    static = {
      ABC_1 = "abc_1"
    }
    dynamic = {
      DEF_1 = {import = true}
    }
  }

  api_envs = merge(local.api.static, local.api.dynamic)
}

import {
  for_each = {
    for key, value in local.api.dynamic : key => value
    if lookup(value, "import", false)
  }

  to = aws_ssm_parameter.api_env[each.key]
  identity = {
    name         = format("/%s/api/%s", local.instance_name, lower(each.key))
    has_value_wo = true
    value_wo_version = "20260127"
  }
}

resource "aws_ssm_parameter" "api_env" {
  for_each = local.api_envs

  name             = format("/%s/api/%s", local.instance_name, lower(each.key))
  type             = "SecureString"
  value            = can(tostring(each.value)) ? tostring(each.value) : null
  value_wo         = can(tostring(each.value)) ? null : lookup(each.value, "default", "tbd")
  value_wo_version = can(tostring(each.value)) ? null : "20260127"

  tags = {
    managed_by  = "terraform"
    environment = local.workspace
    instance    = local.instance
    type        = can(keys(each.value)) ? "dynamic" : "static"
  }

  lifecycle {
    ignore_changes = [value_wo]
  }
}

Any ideas how should I do that?

I’m gonna sound like an idiot here because I’ve been using terraform in production since it’s first release in 2014 but can you write .tf files for imports now?

I always just wrote the terraform I needed and then manually ran terraform import module.vpc.aws_vpc.this vpc-0123456789abcdef0 and let it import what it imported and then usually needed an apply to essential no-op any attributes not set on import (even if they are set in reality) so it gets written to the state.

I always just manually did my imports if I needed to import existing stuff so it was pulled under terraform management…if it was a bunch of similar resources maybe a bash loop like:

for i in {0..4}
do 
  echo "Importing app-server-$i..." 
  terraform import "aws_instance.app_servers[$i]" "app-server-$i" 
done

or, these days just get one of those chatbots to print you a list of import commands you need then paste them in a file with #!/usr/bin/env bashat the top, then chmod +x it and run it.

I haven’t been as deep in the terraform world lately and mostly just write my own modules when I need them and orchestrate them with terragrunt, so I might be out of date in how existing state importing is supposed to be handled these days.