'How to create one variable from another variable in terraform variable.tf file?
For example, In variable.tf file we have a code like below
variable "variable1" {
type = string
default = "ABC"
}
variable "variable2" {
type = string
default = "DEF"
}
variable "variable3" {
type = string
default = "$var.variable1-$var.variable2"
}
Expected output :
variable3 = ABC-DEF
Solution 1:[1]
you can use local instead
locals {
variable3 = var.variable1+"-"+var.variable2
}
and then to call it instead of using var. use local.
like this!
resource "example" "example" {
example = local.variable3
}
ref : https://www.terraform.io/docs/configuration/locals.html
Solution 2:[2]
To my knowledge what you want is not doable with default.
However you can create variable3 and just not assign it a default value and then in your call set variable3 = var.variable1-var.variable2
Not sure this solves your problem but to my knowledge the way you want to do it, won't work.
Also I would recommend upgrading to v0.12.
Solution 3:[3]
You can't do this. Docs clearly states:
The default argument requires a literal value and cannot reference other objects in the configuration.
But you could probably use locals for variable3.
Solution 4:[4]
You're looking for fixed size buffers.
You can create a structure that contains a number of bytes inline:
struct Structure
{
unsafe fixed byte largeField[256];
}
They are not actual arrays, C# stores all those bytes inline in the struct. The accesses to its elements are simply pointer offsets. It's unsafe, since it is not a real array, there is no bounds checking, you can overrun the stack – see this question.
It gets compiled roughly to the equivalent of:
struct Structure
{
[StructLayout(LayoutKind.Sequential, Size = 256)]
[CompilerGenerated]
[UnsafeValueType]
public struct <largeField>e__FixedBuffer
{
public byte FixedElementField;
}
[FixedBuffer(typeof(char), 128)]
public <largeField>e__FixedBuffer largeField;
}
As you can see the result contains no reference types, so you can use that with MemoryMappedViewAccessor.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kristof Jozsa |
| Solution 2 | Berimbolinho |
| Solution 3 | Marcin |
| Solution 4 |
