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

Meta-parameter for enabling or disabling a resource #32736

Closed
mattsafraid opened this issue Feb 22, 2023 · 4 comments
Closed

Meta-parameter for enabling or disabling a resource #32736

mattsafraid opened this issue Feb 22, 2023 · 4 comments
Labels
enhancement new new issue not yet triaged

Comments

@mattsafraid
Copy link

mattsafraid commented Feb 22, 2023

Terraform Version

Terraform v1.3.9
on darwin_amd64

Use Cases

The proposal here is to create a new meta-parameter that would allow to control the state of a single resource.

This is useful for cases when a resource must be destroyed and we want to keep the code, or specially when we want to enable/disable a resource without turning it into an array of resources - which means the references to the resource are kept unchanged.

A similar behaviour can be found in other configuration languages. For example in Puppet, we can use ensure => absent and in Ansible we can use state: absent.

Attempted Solutions

The current behaviour today can be achieved with count and a condition, for example:

From https://www.hashicorp.com/blog/terraform-feature-toggles-blue-green-deployments-canary-test

variable "enable_green_application" {
  default = false
  type    = bool
}

resource "aws_instance" "application_green" {
  count                  = var.enable_green_application ? 1 : 0
  instance_type          = "t2.micro"
  [...]

However in this case we don't have a single resource, but an array of resources. So, any further reference to this object must be adapted - Although we know there's only one resource because of ? 1 : 0.

resource "aws_elb" "application_green" {
  count   = var.enable_green_application ? 1 : 0
  name    = "${var.prefix}-green-elb"
  [...]
  instances = aws_instance.application_green.*.id # references an array 
  [...]

resource "aws_route53_record" "application_green" {
  count   = var.enable_green_application ? 1 : 0
  [...]
  alias {
    name                   = aws_elb.application_green.0.dns_name # references first item 
    zone_id                = aws_elb.application_green.0.zone_id
    evaluate_target_health = true
  }
  [...]

Proposal

With this proposal, not only the code would be cleaner because we don't need a condition block neither array references, but we know there is either zero or one resource.

Applying the concept to the previous snippets, and assuming here a meta-parameter called enabled which accepts a boolean value, we'd have as follows:

variable "enable_green_application" {
  default = false
  type    = bool
}

resource "aws_instance" "application_green" {
  enabled                = var.enable_green_application # new meta parameter in action 
  instance_type          = "t2.micro"
  [...]
resource "aws_elb" "application_green" {
  enabled = var.enable_green_application
  name    = "${var.prefix}-green-elb"
  [...]
  instances = aws_instance.application_green.id # no more array references
  [...]

resource "aws_route53_record" "application_green" {
  enabled = var.enable_green_application
  [...]
  alias {
    name                   = aws_elb.application_green.dns_name # no more array references
    zone_id                = aws_elb.application_green.zone_id
    evaluate_target_health = true
  }
  [...]

References

No response

@mattsafraid mattsafraid added enhancement new new issue not yet triaged labels Feb 22, 2023
@crw
Copy link
Collaborator

crw commented Feb 22, 2023

Thanks for this request!

@apparentlymart
Copy link
Member

Indeed thanks for sharing this proposal, @mattsafraid!

I think this proposal is essentially the same as #21953. Would you agree? If so, I'd like to close this just to consolidate the discussion in one place.

@mattsafraid
Copy link
Author

Hi @apparentlymart, thanks for looking into it!

The proposal is indeed similar to #21953, however the proposal still references an array as in

network_acl_id = aws_network_acl.public[0].id

But from the comments I see that the discussion has advanced to the same idea. This issue can be closed. Thanks!

@mattsafraid mattsafraid closed this as not planned Won't fix, can't repro, duplicate, stale Feb 23, 2023
@github-actions
Copy link

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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants