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

[Enhancement]: default_tags from environment variables #33255

Closed
jtdoepke opened this issue Aug 31, 2023 · 7 comments · Fixed by #33339
Closed

[Enhancement]: default_tags from environment variables #33255

jtdoepke opened this issue Aug 31, 2023 · 7 comments · Fixed by #33339
Labels
enhancement Requests to existing resources that expand the functionality or scope. provider Pertains to the provider itself, rather than any interaction with AWS. tags Pertains to resource tagging.
Milestone

Comments

@jtdoepke
Copy link
Contributor

jtdoepke commented Aug 31, 2023

Description

I would like to be able to configure the default_tags of the AWS provider using environment variables.

This would be useful, for example, in Terraform Cloud/Enterprise. If I have a bunch of workspaces organized under a project, I could attach a variable set to the project with an environment variable that would configure a tag like Owner=my-team across the entire set of AWS resources under that project.

Affected Resource(s) and/or Data Source(s)

  • aws provider

Potential Terraform Configuration

# Read default_tags from environment variables like:
# TF_AWS_DEFAULT_TAGS_<tag_name>=<tag_value>

provider "aws" {
  # Suppose the following environment variables are set:
  # TF_AWS_DEFAULT_TAGS_Owner=my-team
  # TF_AWS_DEFAULT_TAGS_Application=my-app

  default_tags {
    tags = {
      Application = "foobar" # Explicit configuration should override the env vars.
    }
  }

  # The resulting tags map should be `{ Owner = "my-team", Application = "foobar" }`.
}

References

Pull request to implement this change: #33339

Would you like to implement a fix?

Yes

@jtdoepke jtdoepke added the enhancement Requests to existing resources that expand the functionality or scope. label Aug 31, 2023
@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@terraform-aws-provider terraform-aws-provider bot added the needs-triage Waiting for first response or review from a maintainer. label Aug 31, 2023
@johnsonaj johnsonaj added the tags Pertains to resource tagging. label Sep 7, 2023
@justinretzolk
Copy link
Member

Hey @jtdoepke 👋 Thank you for taking the time to raise this, and for opening a pull request to implement it! I'll let the team go through their normal review process, but wanted to mention a workaround in the meantime. Terraform has the capability of loading values for variables by reading environment variables prefixed with TF_VAR_name. Leveraging that and merge(), you can achieve a nearly identical result:

provider "aws" {
  default_tags {
    # with merge(), if the same key exists in both maps, the later one takes precedence
    tags = merge(var.default_tags, var.additional_default_tags)
  }
}

variable "default_tags" {
  description = "A map where the key is Environment and the value is the environment. Should be read from the environment and not passed directly."
  type = map
  default = null
}

variable "additional_default_tags" {
  description = "A map of tag names to tag values"
  type = map
  default = null
}

resource "aws_s3_bucket" "test" {
  bucket = "test"
}

output "tags" {
  value = aws_s3_bucket.test.tags_all
}

With this terraform.tfvars:

additional_default_tags = { Terraform = true, Environment = "prod" }

The following output occurs (note the override of Environment:

$ TF_VAR_default_tags='{ Environment = "dev" }' terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_s3_bucket.test will be created
  + resource "aws_s3_bucket" "test" {
      + acceleration_status         = (known after apply)
      + acl                         = (known after apply)
      + arn                         = (known after apply)
      + bucket                      = "test"
      + bucket_domain_name          = (known after apply)
      + bucket_prefix               = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + object_lock_enabled         = (known after apply)
      + policy                      = (known after apply)
      + region                      = (known after apply)
      + request_payer               = (known after apply)
      + tags_all                    = {
          + "Environment" = "prod"
          + "Terraform"   = "true"
        }
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + tags = {
      + Environment = "prod"
      + Terraform   = "true"
    }

@justinretzolk justinretzolk added waiting-response Maintainers are waiting on response from community or contributor. and removed needs-triage Waiting for first response or review from a maintainer. labels Sep 7, 2023
@jtdoepke
Copy link
Contributor Author

jtdoepke commented Sep 7, 2023

Hey @jtdoepke 👋 Thank you for taking the time to raise this, and for opening a pull request to implement it! I'll let the team go through their normal review process, but wanted to mention a workaround in the meantime.

Yep, that's a totally valid way to do this. Our usecase here is that we want to add a common set of default_tags for cost control (Team, Project, etc) into a lot of existing Terraform workspaces, and to be able to change them with relative ease going forward. We could refactor every TF module we deploy to use that workaround pattern, and then implement it in every new module by convention. But it would be nice if we didn't have to.

My company has a lot of teams distributed globally that are using Terraform relatively independently. I think that this feature will also save us time. If any of those teams forget to implement the above workaround pattern in their own TF, we won't have go bother them to change it.

@github-actions github-actions bot removed the waiting-response Maintainers are waiting on response from community or contributor. label Sep 7, 2023
@bcbrockway
Copy link

Just to add that this would give an additional benefit whereby you don't have to define:

variable "additional_default_tags" {
  description = "A map of tag names to tag values"
  type = map
  default = null
}

...in every calling module to ensure it gets passed through to wherever the provider block is defined.

@jar-b jar-b added the provider Pertains to the provider itself, rather than any interaction with AWS. label Aug 2, 2024
Copy link

github-actions bot commented Aug 2, 2024

Warning

This issue has been closed, meaning that any additional comments are hard for our team to see. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

@github-actions github-actions bot added this to the v5.62.0 milestone Aug 2, 2024
Copy link

github-actions bot commented Aug 9, 2024

This functionality has been released in v5.62.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Copy link

github-actions bot commented Sep 9, 2024

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 Sep 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. provider Pertains to the provider itself, rather than any interaction with AWS. tags Pertains to resource tagging.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants