New in terrafom - Problem with aws_security_group_rule importation

Hello all,

i have a little problem with my terraform execution, i use the 0.11 version, and i want to import an existing aws infrastrure.

Many of my resources have already been imported, but when i import aws_security_group, terraform absolutely want to remove aws_security_group… I think he cannot import the rules, so i change my method for create resource :

Before :

resource “aws_security_group” “WinRM” {
name = “toto-terraform.workspace-WinRM”
vpc id = “${aws_vpc.toto.id}”

tags {
Environment = “terraform.workspace”
Name =“toto-terraform.workspace-winrm”
}
lifecycle {
ignore_changes = [“name”,“description”]
}
}

ingress {
 from_port   = 5986
 to_port     = 5986
 protocol    = "tcp"
 cidr_blocks = ["0.0.0.0/0"]

}
lifecycle {
ignore_changes = [“name”,“description”]
}
}

And after :

resource “aws_security_group” “WinRM” {
name = “toto-terraform.workspace-WinRM”
vpc_id = “aws_vpc.toto.id”

tags {
Environment = “terraform.workspace”
Name =“toto-terraform.workspace-winrm”
}
lifecycle {
ignore_changes = [“name”,“description”]
}
}

resource “aws_security_group_rule” “WinRM” {
type = “ingress”
from_port = 5986
to_port = 5986
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
security_group_id = “aws_security_group.WinRM.id”

}

unfortunately, when i import SG and SG rules, and i run terrafom plan, terraform want destroy “aws_security_group_rule.WinRM-1”

Any advice ? :slight_smile:

Thank you !

There are a couple of commands that can help you figure out what’s going on:

terraform plan, both before and after you import the resource, will show you not only what terraform is going to do, but why. The output of that command can help, and if you share it here maybe someone can see something.

terraform state list - after you run terraform import - might also help you see if there is a mismatch in the resource.

You say that terraform wants to destroy “aws_security_group_rule.WinRM-1”, but that resource name doesn’t match anything in your configuration. It would also help if you can share the actual import command you are running.