-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
Validation bug when using local variables combined with data sources #19284
Comments
Have since built a minimal example configuration that exhibits the issue - it's reproducible without using remote state, just any data source, and appears to be more of an issue of interpolation of local variables - the validation is deciding that an as-yet-uncalculated local variable is empty, which terraform treats the same as not being set. This does not occur for string-type local variables, but does occur for map keys within local variables, regardless of nesting. See below minimal configuration - if either the bucket key within the map is changed to be a hardcoded string (or indeed a non-local string variable), or the logging_config is coded to use the data source directly, it works.
|
Hi @edmundcraske! Sorry for this strange behavior. Terraform is acting weird here because In Terraform v0.11 and prior there isn't any official solution for dynamically setting a block like this. The good news is that such a feature is already in master in preparation for the forthcoming v0.12.0 release. The new way to write what you wanted to express here would be: locals {
cloudfront_logging_configs = {
enabled = [{
bucket = data.terraform_remote_state.cloudfront_logs_s3_bucket.bucket_name
include_cookies = false
prefix = "${var.environment}/"
}]
disabled = []
}
cloudfront_logging_config_key = var.cloudfront_logging_enabled ? "enabled" : "disabled"
}
resource "aws_cloudfront_distribution" "cloudfront" {
dynamic "logging_config" {
for_each = local.cloudfront_logging_configs[local.cloudfront_logging_config_key]
content {
bucket = logging_config.value.bucket
include_cookies = logging_config.value.include_cookies
prefix = logging_config.value.prefix
}
}
# other config here...
} This new Since this work is already landed in master ready to be included in the release, I'm going to close this out. Thanks for reporting this, and sorry for the unfortunate timing of you running into this during this period where this fix is ready but other fixes are blocking the final release. |
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. |
Hi, I'm seeing a weird issue that smells to me like a general configuration/input validation issue, although I've caused it when trying to conditionally enable logging for an AWS CloudFront distribution, so it could be a quirk of the way that the aws provider is implemented.
I'm trying to write a resource definition for aws_cloudfront_distribution where 'logging_config' is defined conditionally based upon a variable being set to "true".
When running the below configuration (edited for brevity - can share my full config or try and come up with a minimal version showing the bug if needed), regardless of setting 'var.cloudfront_logging_enabled', I get the following error:
Error: aws_cloudfront_distribution.cloudfront: "logging_config.0.bucket": required field is not set
However, if I remove the
bucket = "${data.terraform_remote_state.cloudfront_logs_s3_bucket.bucket_name}"
and replace withbucket = "arbitrarystring"
then the code works as expected - when 'var.cloudfront_logging_enabled' is false, it does not want to enable logging, and when true, it wants to enable it using the bucket string specified.If I hardcode the logging_config to use the remote state data source, that works - it appears that the combination of conditionally selecting between the output of a remote state data source and an empty configuration triggers a bug that causes terraform to decide that there is a missing field in the configuration, possibly because it has not retrieved the required data to calculate what that field is yet?
Terraform Version
Terraform Configuration Files
Debug Output
Can provide debug output if needed but does not seem to contain anything useful regarding this validation.
Crash Output
Does not crash
Expected Behavior
Depending on the setting of 'var.cloudfront_logging_enabled', logging should either be enabled with the bucket name from the remote state data source ("true"), or not configured ("false").
Actual Behavior
Regardless of setting of 'var.cloudfront_logging_enabled', terraform gives the error:
Error: aws_cloudfront_distribution.cloudfront: "logging_config.0.bucket": required field is not set
This does not happen if the bucket name is hard coded, or if the conditional element is removed (i.e. always setting logging_config to take the bucket argument from the remote state data source) - it is the combination of the two that appears to trigger the issue.
Steps to Reproduce
terraform init
terraform plan
Additional Context
Using s3 backend and remote state.
The text was updated successfully, but these errors were encountered: