Hi I need to assign the value from an input variable based on 3 values that are on 3 AWS SSM parameters, and then update the value of 1 of those 3 AWS SSM parameters.
Here I explain one case in order to be more clear: I have an input variable filename1 and 3 local variables
variable "filename1" {
type = string
}
locals {
path1 = "filevar1"
path2 = "filevar2"
path3 = "filevar3"
}
Then I get the values of 3 AWS SSM parameters
data "aws_ssm_parameter" "variable1" {
name = local.path1
}
data "aws_ssm_parameter" "variable2" {
name = local.path2
}
data "aws_ssm_parameter" "variable3" {
name = local.path3
}
Based on the values that I get if variable 1 is different from the other 2 variables I shoud assign the input variable to variable2.
Based on some research I assume that I should do it with this
locals {
file2 = "${data.aws_ssm_parameter.variable2.value}" == "${data.aws_ssm_parameter.variable3.value}" != "${data.aws_ssm_parameter.variable1.value}" ? "${data.aws_ssm_parameter.variable2.value}" : "${var.filename1}"
then update SSM value with this
resource "aws_ssm_parameter" "variable2" {
name = local.path2
type = "String"
value = "${local.file2}"
overwrite = true
}
It doesn't work, do you have some example on how to assign a value for a any type of variable based on 3 values?
I have other different conditions, I just need to understand how to declare in order to replicate on the other conditions.
Thanks for your help!!
CodePudding user response:
In your file2
definition you've written the following sub-expression:
"${data.aws_ssm_parameter.variable2.value}" == "${data.aws_ssm_parameter.variable3.value}" != "${data.aws_ssm_parameter.variable1.value}"
The ==
and !=
operators are both binary operators (that is, they take only two arguments) and both produce a boolean result. That means that Terraform will evaluate the first one first, and then compare its result to the other one.
"${data.aws_ssm_parameter.variable2.value}" == "${data.aws_ssm_parameter.variable3.value}"
In the case where both of these are equal, the result will be true
. If they are non-equal, the result will be false
.
If we substitute those results for the original expression, we get:
true != "${data.aws_ssm_parameter.variable1.value}"
false != "${data.aws_ssm_parameter.variable1.value}"
Both of these expressions will always return false
, because a boolean value can never be equal to a string value in the Terraform language.
If I'm understanding your requirement correctly, you want to test whether the first one is equal to both of the other ones. That requires two separate tests, which you can then combine together using the &&
operator which means "and":
(
data.aws_ssm_parameter.variable1.value != data.aws_ssm_parameter.variable2.value &&
data.aws_ssm_parameter.variable1.value != data.aws_ssm_parameter.variable3.value
)
The above means: variable1 is different from variable2 and variable1 is different from variable 3. In other words, it will produce true
only if both of those conditions are true.
Now we can incorporate that with the conditional test you showed in your example to get the full expression:
(
(
data.aws_ssm_parameter.variable1.value != data.aws_ssm_parameter.variable2.value &&
data.aws_ssm_parameter.variable1.value != data.aws_ssm_parameter.variable3.value
) ?
data.aws_ssm_parameter.variable2.value :
var.filename1
)
I wrapped this over multiple lines to make it easier to read the different parts of the expression, but those newlines are not significant and so you can lay this out differently if you wish.
Given the length of the condition here I might prefer to split it out into a separate local value and then refer to it as part of the conditional:
locals {
override_path = (
data.aws_ssm_parameter.variable1.value != data.aws_ssm_parameter.variable2.value &&
data.aws_ssm_parameter.variable1.value != data.aws_ssm_parameter.variable3.value
)
file2 = (
local.override_path ?
data.aws_ssm_parameter.variable2.value :
var.filename1
)
}