2 Answers
- Newest
- Most votes
- Most comments
6
How about this:
- Leave existing rules untouched initially Do not remove or modify the inline rules in aws_security_group yet. Terraform won’t detect changes if you just remove them from config, it leaves them in place.
- Add new standalone resources Define your new aws_vpc_security_group_ingress_rule and aws_vpc_security_group_egress_rule resources in your config. These won’t apply yet, but they’ll be ready to import.
- Manually import existing rules into Terraform state Use terraform import to bring each individual rule into state. You’ll need the rule IDs from the AWS Console (Security Group > Inbound/Outbound Rules > Rule ID):
terraform import aws_vpc_security_group_ingress_rule.my_rule sgr-abc123
- Remove the inline rules from aws_security_group Once the standalone rules are imported and managed, you can safely remove the inline ingress and egress blocks from the original security group resource.
- Run terraform plan and apply Terraform will now manage the rules via the new resources, without deleting or recreating them thus avoiding downtime.
0
To ensure a zero-downtime migration from aws_security_group_rule to the new aws_vpc_security_group_ingress_rule and aws_vpc_security_group_egress_rule resources, you can use a parallel Security Group (blue/green) approach. This method lets you transition safely without conflicting or duplicated rules.
Steps:
- Create sg_new and add rules using aws_vpc_security_group_ingress_rule/aws_vpc_security_group_egress_rule.
- Attach both SGs (sg_old + sg_new) to all ENIs/targets (EC2, ALB, ECS, RDS). Instances can have multiple SGs simultaneously.
- Update any SG-to-SG references (sources/destinations) to point to sg_new.
- Verify traffic, then detach sg_old and destroy it.
This approach avoids duplicate-rule conflicts because rules exist in separate Security Groups, maintaining continuous network access during migration.
answered a year ago
Relevant content
asked 3 years ago
asked 2 years ago
