HI All,
I had created 2 instances few days back, so that time I created instance profile which was OK. But ow from tfvars file I deleted 1 instance conf and triggered pipeline, so now terraform plan shows that it will create resources once again. Could you pls help me understand why Terraform recreates multiple times as it’s very challenging to proceed.
ec2.tf
resource “aws_iam_instance_profile” “instance_profile” {
name = “EC2Profile”
role = data.aws_iam_role.role.name
}
resource “aws_instance” “instance” {
for_each = var.instance_details
instance_type = each.value.instance_type
ami = each.value.ami
associate_public_ip_address = each.value.associate_public_ip_address
availability_zone = each.value.availability_zone
key_name = data.aws_key_pair.key.key_name
iam_instance_profile = aws_iam_instance_profile.instance_profile.name
subnet_id = each.value.subnet_id
vpc_security_group_ids = [each.value.vpc_security_group_ids]
source_dest_check = each.value.source_dest_check
root_block_device {
volume_size = each.value.root_block_device.root_block.volume_size
volume_type = each.value.root_block_device.root_block.volume_type
}
#ebs_block_device {}
tags = each.value.tags
lifecycle {
prevent_destroy = true
}
}
terraform.tfvars
instance_details = {
vmA = {
availability_zone = "us-east-1a"
ami = "ami-0f22c6995b1834835"
instance_type = "t2.micro"
associate_public_ip_address = true
subnet_id = "subnet-05706b65dafce1414" # terraform-automated-subnet-test-pub
vpc_security_group_ids = "sg-05ac36cfb5cb74fe6" # AnsibleSG
source_dest_check = false
root_block_device = {
"root_block" = {
volume_size = 10
volume_type = "gp2"
}
}
ebs_block_device = {}
tags = {
Name = "AUSE1ATLANS001" #<vendor-region-zone-env-os-app_name-instance_no.>
App = "Ansible"
Env = "Test"
Mode = "Automated"
Owner = "anirban.das1@xyz.com"
BlueprintVersion = "0.1"
}
},
vmB = {
availability_zone = “us-east-1b”
ami = “ami-0a1fd0ad4f75850bc”
instance_type = “t2.micro”
associate_public_ip_address = false
subnet_id = “subnet-008e0b4a61d2839f0” # terraform-automated-subnet-test-priv
vpc_security_group_ids = “sg-0a5c9959ea25eb14f” # PrivateSG
source_dest_check = true
root_block_device = {
“root_block” = {
volume_size = 8
volume_type = “gp2”
}
}
ebs_block_device = {}
tags = {
Name = “AUSE1ATLDJG001” #<vendor-region-zone-env-os-app_name-instance_no.>
App = “DJango WebApp”
Env = “Test”
Mode = “Automated”
Owner = “anirban.das1@xyz.com”
BlueprintVersion = “0.1”
}
}
}