Skip to content

Terraform security group rules migration

0

Hi, I have some security group rules deployed using Terraform and the official AWS provider. However, the resource aws_security_group_rule is depreciated in favor of aws_vpc_security_group_egress_rule /aws_vpc_security_group_ingress_rule. I want to "migrate" those rules, but struggle to do it with zero downtime. The import would not work as the aws_vpc_security_group_egress_rule are not yet implemented. I found a solution in a post that talked about creating over permissive rules on purpose and proceed in 3 steps:

  1. Create permissive rules
  2. Delete old rule and create new ones with new resource
  3. Delete old permissives rules But this wouldn't work in my case. Because some of my already in place rules are permissive on purpose and would conflict. Example : Create an ingress rules that accept all traffic from my env CIDR and outgoing for everything in WAN.

Did you encounter the same issue ? How did you manage without a downtime ?

asked a year ago148 views

2 Answers
6

How about this:

  1. 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.
  2. 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.
  3. 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
  1. 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.
  2. Run terraform plan and apply Terraform will now manage the rules via the new resources, without deleting or recreating them thus avoiding downtime.
EXPERT

answered a year ago

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:

  1. Create sg_new and add rules using aws_vpc_security_group_ingress_rule/aws_vpc_security_group_egress_rule.
  2. Attach both SGs (sg_old + sg_new) to all ENIs/targets (EC2, ALB, ECS, RDS). Instances can have multiple SGs simultaneously.
  3. Update any SG-to-SG references (sources/destinations) to point to sg_new.
  4. 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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.