Without making any changes to main.tf, Terraform add already existing security group rules. Terrafrom Identifies each SG rules in main.tf as a new rule(but these rules are already in aws as a result of previous execution)and try to recreate them when I execute the command(terrafrom plan/apply)
This is output of terraform apply command
~ module.application_sg.aws_security_group.security_group
ingress.#: "3" => "1"
ingress.2358522502.cidr_blocks.#: "1" => "0"
ingress.2358522502.cidr_blocks.0: "20.0.1.0/24" => ""
ingress.2358522502.from_port: "443" => "0"
ingress.2358522502.protocol: "tcp" => ""
ingress.2358522502.security_groups.#: "0" => "0"
ingress.2358522502.self: "false" => "false"
ingress.2358522502.to_port: "443" => "0"
ingress.3250959853.cidr_blocks.#: "1" => "0"
ingress.3250959853.cidr_blocks.0: "20.0.1.0/24" => ""
ingress.3250959853.from_port: "8080" => "0"
ingress.3250959853.protocol: "tcp" => ""
ingress.3250959853.security_groups.#: "0" => "0"
ingress.3250959853.self: "false" => "false"
ingress.3250959853.to_port: "8080" => "0"
ingress.753360330.cidr_blocks.#: "0" => "0"
ingress.753360330.from_port: "0" => "0"
ingress.753360330.protocol: "-1" => "-1"
ingress.753360330.security_groups.#: "0" => "0"
ingress.753360330.self: "true" => "true"
ingress.753360330.to_port: "0" => "0"
+ module.rule1.aws_security_group_rule.rule
cidr_blocks.#: "1"
cidr_blocks.0: "20.0.1.0/24"
from_port: "80"
protocol: "tcp"
security_group_id: "sg-17c13770"
self: "false"
source_security_group_id: "<computed>"
to_port: "80"
type: "ingress"
This rules is already existing one.Please help to avoid this as this is very difficult to understand when we are going to add new rules to environment.
Check the state file. This normally happens when Terraform has applied the changes but hasn't updated the state file.
You can list what's in the state file for the location by using:
If it's missing in the state file you should be able to use Terraform's import command to import the pre-existing resource into the state file with something like: