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

S3-Bucket - make templated logging bucket name more flexible #620

Open
westonplatter opened this issue Apr 3, 2023 · 2 comments
Open

S3-Bucket - make templated logging bucket name more flexible #620

westonplatter opened this issue Apr 3, 2023 · 2 comments

Comments

@westonplatter
Copy link

Have a question? Please checkout our Slack Community or visit our Slack Archive.

Slack Community

Describe the Feature

While using the s3-bucket component and trying to add logging to it, I want to use a logging_bucket_name_rendering_template format to work when null-label's tenant and environment are null. When I tried using the suggested format for when only tenant is null, I ran into an error with terraform not being able to format a null value with %s -- this is consistent with tf format syntax section docs (see last paragraph in that section).

See the example below.

I believe the required feature change is to remove any null values in the bucket_name line,

│ Error: Error in function call
│ 
│   on ../../modules/example/main.tf line 30, in locals:
│   30:     final_bucket_name = format("%s-%s-%s-%%s", var.test_namespace, var.test_tenant, var.test_environment, var.test_stage, var.test_logging_bucket_name)
│     ├────────────────
│     │ while calling format(format, args...)
│     │ var.test_environment is "myEnviornment"
│     │ var.test_logging_bucket_name is "myLoggingBucketName"
│     │ var.test_namespace is "myNameSpace"
│     │ var.test_stage is "myStage"
│     │ var.test_tenant is null
│ 
│ Call to function "format" failed: unsupported value for "%s" at 3: null value cannot be formatted.
╵
Releasing state lock. This may take a few moments...

Expected Behavior

When null values are present in the null-label settings (eg, when environment is null), the format function should still generate a logging bucket name.

Use Case

NA

Describe Ideal Solution

Adjust this line to remove nulls before calling the format function.

Something like this,

locals {
    non_null_values = compact([var.test_namespace, var.test_tenant, var.test_environment, var.test_stage, var.test_logging_bucket_name])
    final_bucket_name = format("%s-%s-%s-%%s", local.non_null_values)
}

If I can get feedback on a viable solution, I'm happy to submit a PR.

Alternatives Considered

Manually set the logging bucket as needed.

Additional Context

variable "test_namespace" {
  type        = string
  description = "The namespace to use for the test"
  default = "myNameSpace"
}
  
variable "test_tenant" {
  type        = string
  description = "The tenant to use for the test"
  default     = null
}

variable "test_environment" {
  type        = string
  description = "The environment to use for the test"
  default     = "myEnvironment"
}

variable "test_stage" {
  type        = string
  description = "The stage to use for the test"
  default     = "myStage"
}

variable "test_logging_bucket_name" {
    type        = string
    description = "The logging bucket name to use for the test"
}

locals {
    non_null_values = compact([var.test_namespace, var.test_tenant, var.test_environment, var.test_stage, var.test_logging_bucket_name])
    final_bucket_name = format("%s-%s-%s-%%s", local.non_null_values)
}

output "final_bucket_name" {
  value = local.final_bucket_name
}
@westonplatter westonplatter changed the title Question S3-Bucket - make templated logging bucket name more flexible Apr 3, 2023
westonplatter added a commit to westonplatter/terraform-aws-components that referenced this issue Apr 7, 2023
@Nuru
Copy link
Contributor

Nuru commented Oct 3, 2024

The format recommended in the variable description for when not using tenant is wrong. What is needed is a correction to the documentation, not the code. The correct format is

"%s-%[3]s-%[4]s-%[5]s"

@westonplatter
Copy link
Author

@Nuru thanks for the feedback. Will try the suggestion and check back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants