Replace function in terraform

I want to replace the string to blank. How can I do that?

tags = { Name = replace(local.rule_file_names[fn], "/test/", "-") }

I have imported the security group resource. the resource in AWS doesn’t have Name tag. it is blank. I want to do same via code.

When I put "-" it shows to change the resource. Also when I put space " ", it also shows to change. I want nothing to change. I need it to show the infrastructure upto date while doing plan.

I tried the folliowing codes

tags = { Name = replace(local.rule_file_names[fn], "/test/", "-") }
tags = { Name = replace(local.rule_file_names[fn], "/test/", " ") }

Hi @dipendra.chaudhary,

It sounds like your goal here is for there to be no Name tag declared at all in some situations, rather than to always set it but sometimes set it to a different value.

If so, you won’t be able to use replace to solve this problem, because that can only change which value you assign to that tag. Not declaring the tag requires producing a map that doesn’t have a Name element at all, which means that it’s the map itself that must be conditional, not the Name element value in particular.

I’m not sure I fully understand the underlying purpose of the replace rule you’ve shown here, but given the rest of the context of your question I think your goal is for Name to be set only if local.rule_file_names[fn] has a value other than "test". I’m not sure what fn is here (there’s nothing in your example which declares it) but I’ll just preserve that expression exactly for the sake of this example:

  tags = (
    local.rule_file_names[fn] != "test" ?
    { Name = local.rule_file_names[fn] } :