Skip to content
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

"Know ahead of time" that create_before_destroy won't work in certain situations (Feature request) #6812

Closed
cjcjameson opened this issue Dec 12, 2018 · 6 comments
Labels
service/ec2 Issues and PRs that pertain to the ec2 service.

Comments

@cjcjameson
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform Version

$terraform version
Terraform v0.11.10
+ provider.aws v1.51.0

Affected Resource(s)

  • aws_security_group

Terraform Configuration Files

resource "aws_security_group" "sg-elsrc" {
  name        = "nlp-dev-poc-sg-elsrc"
  vpc_id      = "${data.terraform_remote_state.vpc.private_vpc_id_us-west-2_0}"
  # the git diff is that I used to have `description = "foo bar"` here

  ingress {
      from_port   = 9210
    to_port     = 9210
    protocol    = "tcp"
    cidr_blocks = ["${data.aws_subnet.it_vpc_subnet.cidr_block}"]
  }

  ingress {
    from_port   = 9211
    to_port     = 9211
    protocol    = "tcp"
    cidr_blocks = ["${data.aws_subnet.it_vpc_subnet.cidr_block}"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  lifecycle {
    create_before_destroy = 1
    ignore_changes        = ["tags.Creator"]
  }
}

Debug Output

https://gist.github.com/cjcjameson/b430e1b7fc4a196538831a5d40eb7567

Expected Behavior

terraform plan plans for a destruction and recreation. Although this is annoying that changing a security group's Description means you have to recreate, it's consistent with the explanation given in the docs and in AWS CloudFormation

The security group should have been destroyed, and then new security group created.

Actual Behavior

Terraform tried to create the security group first, but because the name is the same as it was before (only the description had changed)

The code at https://github.com/terraform-providers/terraform-provider-aws/blob/dacf0b4c8a8bd4b6d4fd1b6aa5618f72939121a7/aws/resource_aws_security_group.go#L58-L64 is correct, it seems. So maybe it's a graph issue?

Steps to Reproduce

  1. Config up a security group with a description
  2. terraform apply
  3. Change the config, only changing the description
  4. terraform apply

Important Factoids

  • I also renamed the filename (on my macOS filesystem) of the Terraform file in the meantime
  • I'm on Mojave
  • I had also been doing Elasticsearch domain provisioning, but this bug reproduced even when I isolated the security group change only

References

@bflad
Copy link
Contributor

bflad commented Dec 12, 2018

Hi @cjcjameson 👋 Your Terraform configuration has the following:

  lifecycle {
    create_before_destroy = 1
    ignore_changes        = ["tags.Creator"]
  }

The documentation for create_before_destroy can be found here: https://www.terraform.io/docs/configuration/resources.html#create_before_destroy

Since the resource is explicitly configured to create before destroy, you will need to remove that lifecycle configuration to change the graph ordering or consider one of the following which are compatible with create_before_destroy:

  • Switching the aws_security_group resource to use the name_prefix argument (Terraform will assign a random suffix)
  • Removing the name argument altogether (Terraform will assign a random name)

Hope this helps.

@bflad bflad added waiting-response Maintainers are waiting on response from community or contributor. service/ec2 Issues and PRs that pertain to the ec2 service. labels Dec 12, 2018
@cjcjameson
Copy link
Author

Oh darn. What's that doing there? I thought it was on a different stanza but I put it in the wrong place.

🤦‍♂️ sorry about that!

@ghost ghost removed the waiting-response Maintainers are waiting on response from community or contributor. label Dec 12, 2018
@cjcjameson
Copy link
Author

@bflad wait! Can I convert this into a feature request?!?

Terraform "should" "know" that create_before_destroy won't work when the name stays the same for this type of resource. The error message could show this explanation!

@cjcjameson cjcjameson reopened this Dec 12, 2018
@cjcjameson cjcjameson changed the title Changing Description of Security Group tries to create before destroying "Know ahead of time" that create_before_destroy won't work in certain situations (Feature request) Dec 12, 2018
@bflad
Copy link
Contributor

bflad commented Dec 12, 2018

Anything within resource lifecycle configuration (and operation ordering in general) is handled upstream in Terraform core. This information is not accessible to Terraform providers (e.g. resource code) during an operation. If you would like to submit a feature request, it'll have to be upstream to start: https://github.com/hashicorp/terraform/issues/

@cjcjameson
Copy link
Author

@ghost
Copy link

ghost commented Apr 1, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
service/ec2 Issues and PRs that pertain to the ec2 service.
Projects
None yet
Development

No branches or pull requests

2 participants