hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.63k stars 9.01k forks source link

Cost Category rules out of order #26774

Open apeduru-patreon opened 1 year ago

apeduru-patreon commented 1 year ago

Community Note

Terraform CLI and Terraform AWS Provider Version

Terraform provider version 4.29

Affected Resource(s)

Terraform Configuration Files

Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.

I've redacted some of the values as those are company sensitive. Apart from that, this is the terraform configuration that is being used.

resource "aws_ce_cost_category" "cost_center" {
  name         = "Cost Center"
  rule_version = "CostCategoryExpression.v1"

  default_value = "uncategorized"

  rule {
    type  = "REGULAR"
    value = "prod-roles"
    rule {
      and {
        dimension {
          key           = "LINKED_ACCOUNT"
          values        = [local.production]
          match_options = ["EQUALS"]
        }
      }
      and {
        tags {
          key = "role"
          values = ["..."]
          match_options = ["EQUALS"]
        }
      }
    }
  }

  rule {
    type  = "REGULAR"
    value = "rnd-roles"
    rule {
      and {
        dimension {
          key           = "LINKED_ACCOUNT"
          values        = [local.development]
          match_options = ["EQUALS"]
        }
      }
      and {
        tags {
          key = "role"
          values = ["..." ]
          match_options = ["EQUALS"]
        }
      }
    }
  }

  rule {
    type  = "REGULAR"
    value = "rnd-accounts"
    rule {
      dimension {
        key = "LINKED_ACCOUNT"
        values = ["..." ]
        match_options = ["EQUALS"]
      }
    }
  }

  rule {
    type  = "REGULAR"
    value = "prod-env"
    rule {
      tags {
        key = "environment"
        values = [
          "production",
          "prod",
        ]
        match_options = ["EQUALS"]
      }
    }
  }

  rule {
    type  = "REGULAR"
    value = "rnd-env"
    rule {
      tags {
        key = "environment"
        values = [
          "development",
          "dev",
          "staging",
        ]
        match_options = ["EQUALS"]
      }
    }
  }

}

Debug Output

Panic Output

Expected Behavior

When I define these rules in Terraform, I'm expecting the rules that are defined in order in Terraform to also show up in the same order in the cost category rules editor. i.e.

1. prod-roles
2. rnd-roles
3. rnd-accounts
4. prod-env
5. rnd-env

Actual Behavior

When the Terraform is applied and the rules are processed in AWS, I'm seeing these rules are out of order. It looks like this in the rules editor

1. prod-roles
2. prod-env
3. rnd-roles
4. rnd-accounts
5. rnd-env

Steps to Reproduce

  1. terraform apply

Important Factoids

References

throwaway8787 commented 1 year ago

@justinretzolk this is not an enhancement, but is indeed a bug, this ticket should be labeled as such. The functionality of billing categories breaks if rules are out of defined order. This comes into play heavily when using matches of contains, starts with, and ends with.

jnancel commented 10 months ago

Hello guys,

This issue is actually critical for us because we need that feature in order to be able to properly allocate cloud costs to users.

EDIT : actually, I found a very interesting blog post on how to workaround that by using a method to actually order the elements you sends into the rules : https://netmemo.github.io/post/tf-map-ordering/.

To be tested.

Thanks

bradwood commented 2 months ago

I'm also having exactly the same issue right now. While the workaround that @jnancel mentioned looks cool, I'd rather have a clean solution to this.

Any updates on when we can get a fix for this?

EDIT:

I'm on v5.33.0 btw.