New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider security groups with source security groups when hashing #2376

Merged
merged 13 commits into from Jun 19, 2015

Conversation

Projects
None yet
@catsby
Member

catsby commented Jun 16, 2015

Supersedes #2318
Fixes #2294

Original message:

Previously they would conflict you had multiple security group rules
with the same ingress or egress ports but different source security
groups because only the CIDR blocks were considered (which are empty
when using source security groups).

Updates:

Remediation: If you encounter an error regarding duplicate rules, you will need to identify and manually remove the offending/missing security group rules from the Security Group, and re-plan and apply with Terraform.

Consider security groups with source security groups when hashing
Previously they would conflict you had multiple security group rules
with the same ingress or egress ports but different source security
groups because only the CIDR blocks were considered (which are empty
when using source security groups).

Updated to include migrations (from clint@ctshryock.com)

Signed-off-by: Clint Shryock <clint@ctshryock.com>
@catsby

This comment has been minimized.

Show comment
Hide comment
@catsby

catsby Jun 16, 2015

Member

This will clean up #2294. It will migrate people when possible, but there may still exist scenarios (like in 2294) where some manual cleanup will be necessary. The reason being that in the config given there, there are 2 rules created, but without considering the source group id in the hash, the state file will mistakenly have the same source id for both rules (clearly not the correct case).

Member

catsby commented Jun 16, 2015

This will clean up #2294. It will migrate people when possible, but there may still exist scenarios (like in 2294) where some manual cleanup will be necessary. The reason being that in the config given there, there are 2 rules created, but without considering the source group id in the hash, the state file will mistakenly have the same source id for both rules (clearly not the correct case).

@catsby

This comment has been minimized.

Show comment
Hide comment
@catsby

catsby Jun 16, 2015

Member

cc @phinze for some review

Member

catsby commented Jun 16, 2015

cc @phinze for some review

@catsby

This comment has been minimized.

Show comment
Hide comment
@catsby

catsby Jun 16, 2015

Member

The reason being that in the config given there, there are 2 rules created, but without considering the source group id in the hash, the state file will mistakenly have the same source id for both rules (clearly not the correct case).

I should add this; remotely (on AWS) the rules are created correctly. They're simply stored locally in the state file incorrectly. This PR will migrate most cases ( I believe ) and prevent that in the future.

Users with these conflicts can hit "Duplicate" errors. Manual clean up is needed, unless we swallow that error in the create method for the rule... but that seemed unsafe

Member

catsby commented Jun 16, 2015

The reason being that in the config given there, there are 2 rules created, but without considering the source group id in the hash, the state file will mistakenly have the same source id for both rules (clearly not the correct case).

I should add this; remotely (on AWS) the rules are created correctly. They're simply stored locally in the state file incorrectly. This PR will migrate most cases ( I believe ) and prevent that in the future.

Users with these conflicts can hit "Duplicate" errors. Manual clean up is needed, unless we swallow that error in the create method for the rule... but that seemed unsafe

return *b[i].GroupName < *b[j].GroupName
}
panic("mismatched security group rules, may be a terraform bug")

This comment has been minimized.

@phinze

phinze Jun 16, 2015

Member

Can a user get to this panic by specifying a config improperly? Or are we protected by earlier validations from that?

@phinze

phinze Jun 16, 2015

Member

Can a user get to this panic by specifying a config improperly? Or are we protected by earlier validations from that?

This comment has been minimized.

@catsby

catsby Jun 17, 2015

Member

I believe we're protected, but I'm honestly not 100% sure. @jszwedko wrote that part, and I meant to ask you @phinze about it. The panic makes me uneasy, but I'm not sure what's the consequences of omitting it are. Is there a more sane "default" fall through behavior?

@catsby

catsby Jun 17, 2015

Member

I believe we're protected, but I'm honestly not 100% sure. @jszwedko wrote that part, and I meant to ask you @phinze about it. The panic makes me uneasy, but I'm not sure what's the consequences of omitting it are. Is there a more sane "default" fall through behavior?

This comment has been minimized.

@phinze

phinze Jun 17, 2015

Member

Yeah let's just return false here, which will just sort last anything that's wonky looking, allowing later code to fail in a likely more meaningful way.

@phinze

phinze Jun 17, 2015

Member

Yeah let's just return false here, which will just sort last anything that's wonky looking, allowing later code to fail in a likely more meaningful way.

This comment has been minimized.

@jszwedko

jszwedko Jun 18, 2015

Contributor

I'd worry that just returning false might mask an error (though theoretically that line should never be hit). If we don't want to panic, I'd suggest at least sorting the bad data in a deterministic way (otherwise you could end up in a semi-permadiff situation).

@jszwedko

jszwedko Jun 18, 2015

Contributor

I'd worry that just returning false might mask an error (though theoretically that line should never be hit). If we don't want to panic, I'd suggest at least sorting the bad data in a deterministic way (otherwise you could end up in a semi-permadiff situation).

This comment has been minimized.

@jszwedko

jszwedko Jun 18, 2015

Contributor

As a user of terraform, I'd personally prefer that it blow up than silently swallow this sort of error.

@jszwedko

jszwedko Jun 18, 2015

Contributor

As a user of terraform, I'd personally prefer that it blow up than silently swallow this sort of error.

This comment has been minimized.

@mitchellh

mitchellh Jun 18, 2015

Member

One thing to keep in mind (a positive thing) is that Terraform will recover panics that pop out of resource calls (as long as they're not from a goroutine within that resource call) and still handle cleanup and partial state.

@mitchellh

mitchellh Jun 18, 2015

Member

One thing to keep in mind (a positive thing) is that Terraform will recover panics that pop out of resource calls (as long as they're not from a goroutine within that resource call) and still handle cleanup and partial state.

@zxjinn

This comment has been minimized.

Show comment
Hide comment
@zxjinn

zxjinn Jun 17, 2015

@catsby after running unmodified code I posted in #2294 against this branch, I still get the same error on the second apply 😦

$ ~/code/go/bin/terraform --version
Terraform v0.6.0-dev (640836ee58d9d3e92a75f7c6e27953eb16e2f911)

zxjinn commented Jun 17, 2015

@catsby after running unmodified code I posted in #2294 against this branch, I still get the same error on the second apply 😦

$ ~/code/go/bin/terraform --version
Terraform v0.6.0-dev (640836ee58d9d3e92a75f7c6e27953eb16e2f911)
@zxjinn

This comment has been minimized.

Show comment
Hide comment
@zxjinn

zxjinn Jun 17, 2015

@catsby Sorry false alarm! I forgot to update the symlink to point to ~/code/go/bin/terraform, I was still using the latest release when I ran that. 👏 thank you very much!

zxjinn commented Jun 17, 2015

@catsby Sorry false alarm! I forgot to update the symlink to point to ~/code/go/bin/terraform, I was still using the latest release when I ran that. 👏 thank you very much!

catsby added some commits Jun 18, 2015

Merge branch 'master' into jszwedko-fix-sg
* master: (23 commits)
  typo
  Update CHANGELOG.md
  provider/aws: Add docs for autoscaling_policy + cloudwatch_metric_alarm
  provider/aws: Add autoscaling_policy
  provider/aws: Add cloudwatch_metric_alarm
  Update CHANGELOG.md
  Update CHANGELOG.md
  provider/template: don't error when rendering fails in Exists
  Update CHANGELOG.md
  Added Azure SQL server and service support.
  Update CHANGELOG.md
  docs: clarify wording around destroy/apply args
  Getting Started: Added a Next Step upon finishing install.
  docs: add description of archive format to download page
  docs: snapshot plugin dependencies when releasing
  add v0.5.3 transitory deps
  Fixes support for changing just the read / write capacity of a GSI
  Change sleep time for DynamoDB table waits from 3 seconds to 5 seconds
  Remove request for attribute changes
  Fix AWS SDK imports
  ...
Merge branch 'master' into jszwedko-fix-sg
* master:
  provider/azure: Fix SQL client name to match upstream
@catsby

This comment has been minimized.

Show comment
Hide comment
@catsby

catsby Jun 19, 2015

Member

Thanks for checking it out @zxjinn , much appreciated. Sorry for the mess 😄

Member

catsby commented Jun 19, 2015

Thanks for checking it out @zxjinn , much appreciated. Sorry for the mess 😄

@phinze

This comment has been minimized.

Show comment
Hide comment
@phinze

phinze Jun 19, 2015

Member

LGTM

Member

phinze commented Jun 19, 2015

LGTM

catsby added a commit that referenced this pull request Jun 19, 2015

Merge pull request #2376 from hashicorp/fix-security-group-rule-hashi…
…ng-with-source-sg

Consider security groups with source security groups when hashing

@catsby catsby merged commit f045e9e into master Jun 19, 2015

1 check was pending

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details

@catsby catsby deleted the fix-security-group-rule-hashing-with-source-sg branch Jun 19, 2015

@jszwedko

This comment has been minimized.

Show comment
Hide comment
@jszwedko

jszwedko Jun 19, 2015

Contributor

👏 thanks @catsby !

Contributor

jszwedko commented Jun 19, 2015

👏 thanks @catsby !

@marshallpierce

This comment has been minimized.

Show comment
Hide comment
@marshallpierce

marshallpierce Jul 8, 2015

I'm on terraform 0.6.0 and I'm seeing the error warning me about this issue. This happens the first time I apply my infrastructure. Looks like this was merged before 0.6.0, so am I perhaps seeing a different issue?

Edit: can't reproduce anymore. I was probably doing something dumb.

marshallpierce commented Jul 8, 2015

I'm on terraform 0.6.0 and I'm seeing the error warning me about this issue. This happens the first time I apply my infrastructure. Looks like this was merged before 0.6.0, so am I perhaps seeing a different issue?

Edit: can't reproduce anymore. I was probably doing something dumb.

@jtchoi

This comment has been minimized.

Show comment
Hide comment
@jtchoi

jtchoi Aug 4, 2015

We have two rules referencing the same port and security group, but differing protocols. I believe in this case there should not be a duplicate but terraform 0.6.1 treats them as separate

jtchoi commented Aug 4, 2015

We have two rules referencing the same port and security group, but differing protocols. I believe in this case there should not be a duplicate but terraform 0.6.1 treats them as separate

@JeanMertz

This comment has been minimized.

Show comment
Hide comment
@JeanMertz

JeanMertz Aug 14, 2015

Contributor

Just had this happen to me as well, using the latest Terraform stable version at this moment.

I already deleted all things manually, so don't have a log right now, but definitely saw this with ~5 security group resources – which don't have a cidr block – during my run.

Contributor

JeanMertz commented Aug 14, 2015

Just had this happen to me as well, using the latest Terraform stable version at this moment.

I already deleted all things manually, so don't have a log right now, but definitely saw this with ~5 security group resources – which don't have a cidr block – during my run.

@samprakos

This comment has been minimized.

Show comment
Hide comment
@samprakos

samprakos Aug 18, 2015

This happens to me still with the latest build (v0.6.4-dev).

samprakos commented Aug 18, 2015

This happens to me still with the latest build (v0.6.4-dev).

@jszwedko

This comment has been minimized.

Show comment
Hide comment
@jszwedko

jszwedko Aug 18, 2015

Contributor

@SamClinckspoor can you post some example configuration reproducing?

Contributor

jszwedko commented Aug 18, 2015

@SamClinckspoor can you post some example configuration reproducing?

@SamClinckspoor

This comment has been minimized.

Show comment
Hide comment
@SamClinckspoor

SamClinckspoor Aug 18, 2015

Contributor

@jszwedko wrong mention 😉
@samprakos

Contributor

SamClinckspoor commented Aug 18, 2015

@jszwedko wrong mention 😉
@samprakos

@jszwedko

This comment has been minimized.

Show comment
Hide comment
@jszwedko

jszwedko Aug 18, 2015

Contributor

Oops! @samprakos ^^

Contributor

jszwedko commented Aug 18, 2015

Oops! @samprakos ^^

@samprakos

This comment has been minimized.

Show comment
Hide comment
@samprakos

samprakos Aug 18, 2015

I don't believe I'd be able to reproduce it on demand...I fixed the 18 errors by manually deleting the rules from aws and re-running Terraform plan/apply. It's almost as though the security group ids in aws all of a sudden didn't align with the ones in the tfstate file.

samprakos commented Aug 18, 2015

I don't believe I'd be able to reproduce it on demand...I fixed the 18 errors by manually deleting the rules from aws and re-running Terraform plan/apply. It's almost as though the security group ids in aws all of a sudden didn't align with the ones in the tfstate file.

@jszwedko

This comment has been minimized.

Show comment
Hide comment
@jszwedko

jszwedko Aug 19, 2015

Contributor

@samprakos that is probably different from this issue then.

Contributor

jszwedko commented Aug 19, 2015

@samprakos that is probably different from this issue then.

@catsby

This comment has been minimized.

Show comment
Hide comment
@catsby

catsby Aug 20, 2015

Member

I recently opened #3019 to do further refactoring of Security Group rules. If you're interested in trying it out, I would recommend a test setup, as there's a schema migration and I don't know how easy that would be to roll back.

Member

catsby commented Aug 20, 2015

I recently opened #3019 to do further refactoring of Security Group rules. If you're interested in trying it out, I would recommend a test setup, as there's a schema migration and I don't know how easy that would be to roll back.

@dgarlitt

This comment has been minimized.

Show comment
Hide comment
@dgarlitt

dgarlitt Oct 6, 2016

Is there a work-around for this issue in the meantime? I get the "duplicate Security Group rule" error when trying to apply the following resources:

resource "aws_security_group_rule" "all-master-to-master" {
  type = "ingress"
  security_group_id = "${aws_security_group.masters-k8s.id}"
  self = true
  from_port = 0
  to_port = 0
  protocol = "-1"
}

resource "aws_security_group_rule" "all-master-to-node" {
  type = "ingress"
  security_group_id = "${aws_security_group.nodes-k8s.id}"
  source_security_group_id = "${aws_security_group.masters-k8s.id}"
  from_port = 0
  to_port = 0
  protocol = "-1"
}

dgarlitt commented Oct 6, 2016

Is there a work-around for this issue in the meantime? I get the "duplicate Security Group rule" error when trying to apply the following resources:

resource "aws_security_group_rule" "all-master-to-master" {
  type = "ingress"
  security_group_id = "${aws_security_group.masters-k8s.id}"
  self = true
  from_port = 0
  to_port = 0
  protocol = "-1"
}

resource "aws_security_group_rule" "all-master-to-node" {
  type = "ingress"
  security_group_id = "${aws_security_group.nodes-k8s.id}"
  source_security_group_id = "${aws_security_group.masters-k8s.id}"
  from_port = 0
  to_port = 0
  protocol = "-1"
}
@gtmtech

This comment has been minimized.

Show comment
Hide comment
@gtmtech

gtmtech Nov 1, 2016

Still getting this on terraform 0.7.5 >.<

Configuration was built and first deployed using terraform 0.7.5 too, so no legacy stuff - something is clearly still broken

gtmtech commented Nov 1, 2016

Still getting this on terraform 0.7.5 >.<

Configuration was built and first deployed using terraform 0.7.5 too, so no legacy stuff - something is clearly still broken

@gtmtech

This comment has been minimized.

Show comment
Hide comment
@gtmtech

gtmtech Nov 1, 2016

Ah I tracked it down - in my case I was rewiring two aws_subnets, subnet A, and subnet B, and swapping their CIDR blocks. On some terraform apply's it would say it couldnt create the subnet because the CIDR already existed (one bug - terraform isnt intelligent enough to handle this situation) - on another run it complained:

* aws_security_group_rule.egress8080loadbalancer: [WARN] A duplicate Security Group rule was found on (sg-9d5c8ffb). This may be
a side effect of a now-fixed Terraform issue causing two security groups with
identical attributes but different source_security_group_ids to overwrite each
other in the state. See https://github.com/hashicorp/terraform/pull/2376 for more
information and instructions for recovery. Error message: the specified rule "peer: 10.128.10.0/24, TCP, from port: 8080, to port: 8080, ALLOW" already exists
* aws_security_group_rule.ingress8080loadbalancer: [WARN] A duplicate Security Group rule was found on (sg-67439001). This may be
a side effect of a now-fixed Terraform issue causing two security groups with
identical attributes but different source_security_group_ids to overwrite each
other in the state. See https://github.com/hashicorp/terraform/pull/2376 for more
information and instructions for recovery. Error message: the specified rule "peer: 10.128.10.0/24, TCP, from port: 8080, to port: 8080, ALLOW" already exists

Which would be the case because the subnets are swapping around and order is important.

In such cases like these the only appropriate thing would be to delete all subnets first, and all SG first and then recreate. Its more advanced logic than terraform currently has, but this would cater for this suite of problems

gtmtech commented Nov 1, 2016

Ah I tracked it down - in my case I was rewiring two aws_subnets, subnet A, and subnet B, and swapping their CIDR blocks. On some terraform apply's it would say it couldnt create the subnet because the CIDR already existed (one bug - terraform isnt intelligent enough to handle this situation) - on another run it complained:

* aws_security_group_rule.egress8080loadbalancer: [WARN] A duplicate Security Group rule was found on (sg-9d5c8ffb). This may be
a side effect of a now-fixed Terraform issue causing two security groups with
identical attributes but different source_security_group_ids to overwrite each
other in the state. See https://github.com/hashicorp/terraform/pull/2376 for more
information and instructions for recovery. Error message: the specified rule "peer: 10.128.10.0/24, TCP, from port: 8080, to port: 8080, ALLOW" already exists
* aws_security_group_rule.ingress8080loadbalancer: [WARN] A duplicate Security Group rule was found on (sg-67439001). This may be
a side effect of a now-fixed Terraform issue causing two security groups with
identical attributes but different source_security_group_ids to overwrite each
other in the state. See https://github.com/hashicorp/terraform/pull/2376 for more
information and instructions for recovery. Error message: the specified rule "peer: 10.128.10.0/24, TCP, from port: 8080, to port: 8080, ALLOW" already exists

Which would be the case because the subnets are swapping around and order is important.

In such cases like these the only appropriate thing would be to delete all subnets first, and all SG first and then recreate. Its more advanced logic than terraform currently has, but this would cater for this suite of problems

@shailendersingh2

This comment has been minimized.

Show comment
Hide comment
@shailendersingh2

shailendersingh2 Jul 14, 2017

I am facing same error where i already have SG with specific CIDR block but when my terrafrom aplly command gives same duplicate error and quits during terraform apply. I can't delete existing rule because it was already present and PRD data communication is happening and i need functionality to just ignore if it is already present and i used lifecycle {ignore_changed = "subnetname"} that not helped me so why it can't just ignore if security group CIDR already present.

shailendersingh2 commented Jul 14, 2017

I am facing same error where i already have SG with specific CIDR block but when my terrafrom aplly command gives same duplicate error and quits during terraform apply. I can't delete existing rule because it was already present and PRD data communication is happening and i need functionality to just ignore if it is already present and i used lifecycle {ignore_changed = "subnetname"} that not helped me so why it can't just ignore if security group CIDR already present.

@genevieve

This comment has been minimized.

Show comment
Hide comment
@genevieve

genevieve Nov 17, 2017

Is there a way to migrate from inline security group rules to aws_security_group_rule resources? We want the same rules but external from the security group so that we can conditionally create new rules for a particular source security group.

genevieve commented Nov 17, 2017

Is there a way to migrate from inline security group rules to aws_security_group_rule resources? We want the same rules but external from the security group so that we can conditionally create new rules for a particular source security group.

@korfuri

This comment has been minimized.

Show comment
Hide comment
@korfuri

korfuri Jun 28, 2018

Contributor

@genevieve better late than never, but: you can migrate, kind of.

  • Moving each rule out of the inline definition into its own aws_security_group_rule resource will create a plan that wants to destroy and re-create all rules.
  • Instead of applying that plan, you can use terraform state mv to move each resource from its old path (aws_security_group.mygroup.aws_security_group_rule.mygroup.0) to its new path (aws_security_group_rule.myrule1). You may need to use terraform state show to find out what "old path" corresponds to what "new path".

It's quite tedious if you have a lot of rules. At least it's easy to tell if you're done: you can plan as many times as you want while moving stuff around, and when the plan is empty, you know you've got it right. Also terraform state mv won't let you overwrite a resource by moving another resource on top of it, so you're relatively safe when using it.

Contributor

korfuri commented Jun 28, 2018

@genevieve better late than never, but: you can migrate, kind of.

  • Moving each rule out of the inline definition into its own aws_security_group_rule resource will create a plan that wants to destroy and re-create all rules.
  • Instead of applying that plan, you can use terraform state mv to move each resource from its old path (aws_security_group.mygroup.aws_security_group_rule.mygroup.0) to its new path (aws_security_group_rule.myrule1). You may need to use terraform state show to find out what "old path" corresponds to what "new path".

It's quite tedious if you have a lot of rules. At least it's easy to tell if you're done: you can plan as many times as you want while moving stuff around, and when the plan is empty, you know you've got it right. Also terraform state mv won't let you overwrite a resource by moving another resource on top of it, so you're relatively safe when using it.

@maliaoreta

This comment has been minimized.

Show comment
Hide comment
@maliaoreta

maliaoreta Jul 18, 2018

Hi @korfuri!

I'm currently attempting to use terraform state mv when refactoring inline resource types into separate resources/modules.
However I've been unable to find the valid source address syntax for these inline resources such as aws_security_group_rule or aws_network_interface_attachment.

For example, when using terraform state show on the aws_network_interface resource to get the attachment address, it looks something like this:

attachment.#                       = 1
attachment.xxxxxxxxx.attachment_id = eni-attach-xxxxxxxx
attachment.xxxxxxxxx.device_index  = 0
attachment.xxxxxxxxx.instance      = i-xxxxxxxxxxxxxx

Sorry if I'm just missing something obvious, but where can I find the correct addresses for these inline configs?
This would be really helpful since I'm currently importing a bunch of existing AWS resources into Terraform management, and all the resources seem to be returned with inline configurations.

Thanks in advance!

maliaoreta commented Jul 18, 2018

Hi @korfuri!

I'm currently attempting to use terraform state mv when refactoring inline resource types into separate resources/modules.
However I've been unable to find the valid source address syntax for these inline resources such as aws_security_group_rule or aws_network_interface_attachment.

For example, when using terraform state show on the aws_network_interface resource to get the attachment address, it looks something like this:

attachment.#                       = 1
attachment.xxxxxxxxx.attachment_id = eni-attach-xxxxxxxx
attachment.xxxxxxxxx.device_index  = 0
attachment.xxxxxxxxx.instance      = i-xxxxxxxxxxxxxx

Sorry if I'm just missing something obvious, but where can I find the correct addresses for these inline configs?
This would be really helpful since I'm currently importing a bunch of existing AWS resources into Terraform management, and all the resources seem to be returned with inline configurations.

Thanks in advance!

@korfuri

This comment has been minimized.

Show comment
Hide comment
@korfuri

korfuri Jul 18, 2018

Contributor

I think I was confused in my response above, and that's not possible.
You may be able to do something like: (1) back up your state (2) Write down the security group's ID and terraform state rm it (3) change your config to use non-inline rules (4) terraform import your security group again. The rules will be imported as their own resources, with arbitrary paths. (5) terraform state mv each rule into its proper location.

Contributor

korfuri commented Jul 18, 2018

I think I was confused in my response above, and that's not possible.
You may be able to do something like: (1) back up your state (2) Write down the security group's ID and terraform state rm it (3) change your config to use non-inline rules (4) terraform import your security group again. The rules will be imported as their own resources, with arbitrary paths. (5) terraform state mv each rule into its proper location.

@maliaoreta

This comment has been minimized.

Show comment
Hide comment
@maliaoreta

maliaoreta Jul 18, 2018

YES that worked!!!
Thank you so much!!! Especially for such a quick response!

Have a good one!

maliaoreta commented Jul 18, 2018

YES that worked!!!
Thank you so much!!! Especially for such a quick response!

Have a good one!

@alkalinecoffee

This comment has been minimized.

Show comment
Hide comment
@alkalinecoffee

alkalinecoffee Aug 22, 2018

We're seeing this issue (Terraform v0.11.7, provider.aws v1.29.0) when modifying a security group rule with new IP ranges. The security group config has no in-line rules--all rules are defined in aws_security_group_rule resources.

We've commented out all of our aws_security_group_rule resources, applied (so the security group has no rules attached), removed the security group from state, re-imported, uncommented and re-applied all of the rules. This works fine, until we need to add a new rule, which results in the duplicate error.

Steps to reproduce (let's call the security group infra)...

  1. Clear out existing rules from security group and state:
# Comment out rules in config
tf apply (security group now has no rules)
tf state list
tf state show sg-123456
  1. Remove the SG from state and re-add:
tf state rm module.ci.aws_security_group.infra
tf import tf module.ci.aws_security_group.infra sg-123456
  1. Inspect state of resource to ensure rules are empty
tf state show module.ci.aws_security_group.infra
  1. Re-add security groups removed from Step 1
# Uncomment security group rules in config
tf apply
  1. Add a new IP range to one of the rules and tf apply ... which results in failure

This issue should be revisited as 1) it's still broken and 2) the URL provided in the error message points the user to this thread for a resolution, for which one does not clearly exist.

alkalinecoffee commented Aug 22, 2018

We're seeing this issue (Terraform v0.11.7, provider.aws v1.29.0) when modifying a security group rule with new IP ranges. The security group config has no in-line rules--all rules are defined in aws_security_group_rule resources.

We've commented out all of our aws_security_group_rule resources, applied (so the security group has no rules attached), removed the security group from state, re-imported, uncommented and re-applied all of the rules. This works fine, until we need to add a new rule, which results in the duplicate error.

Steps to reproduce (let's call the security group infra)...

  1. Clear out existing rules from security group and state:
# Comment out rules in config
tf apply (security group now has no rules)
tf state list
tf state show sg-123456
  1. Remove the SG from state and re-add:
tf state rm module.ci.aws_security_group.infra
tf import tf module.ci.aws_security_group.infra sg-123456
  1. Inspect state of resource to ensure rules are empty
tf state show module.ci.aws_security_group.infra
  1. Re-add security groups removed from Step 1
# Uncomment security group rules in config
tf apply
  1. Add a new IP range to one of the rules and tf apply ... which results in failure

This issue should be revisited as 1) it's still broken and 2) the URL provided in the error message points the user to this thread for a resolution, for which one does not clearly exist.

@alkalinecoffee

This comment has been minimized.

Show comment
Hide comment
@alkalinecoffee

alkalinecoffee Aug 23, 2018

Following up on the message above...

We had the following lifecycle in our security group rule configs:

lifecycle {
  create_before_destroy = true
}

It seems obvious now, but removing this lifecycle config avoids the duplicate warning.

We initially had this lifecycle policy in place as we ran into an issue where a bad terraform apply left our security groups in a bad state which resulted in blocked traffic. But it seems that overall, having this policy in place is inherently incompatible with the strict duplication rules applied to security group rules.

I'm not sure what the resolution is here, or if this issue belongs in this thread or a new one should be opened. We want to continue using the lifecycle for the reason above, but it also prevents us from easily managing our SG rules.

alkalinecoffee commented Aug 23, 2018

Following up on the message above...

We had the following lifecycle in our security group rule configs:

lifecycle {
  create_before_destroy = true
}

It seems obvious now, but removing this lifecycle config avoids the duplicate warning.

We initially had this lifecycle policy in place as we ran into an issue where a bad terraform apply left our security groups in a bad state which resulted in blocked traffic. But it seems that overall, having this policy in place is inherently incompatible with the strict duplication rules applied to security group rules.

I'm not sure what the resolution is here, or if this issue belongs in this thread or a new one should be opened. We want to continue using the lifecycle for the reason above, but it also prevents us from easily managing our SG rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment