-
Hello, Does generate_hcl produce the files in all leaf directories? Example: I expect no files to be produced in the "policy" directory. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @az-z The The The When you execute So in your example, if Below are two ways you can solve your problem:
For the 2 option, see the example below:
then inside a child stack:
The example above also works for If you're using
let me know it this answer your question. |
Beta Was this translation helpful? Give feedback.
-
I think it does. But it a lot of useful and detailed information that I
need to process and apply.
Thank you _very_ much for taking your time to explain these concepts to me.
…On Wed, Jan 18, 2023, 16:38 伊亜希 ***@***.***> wrote:
Hi @az-z <https://github.com/az-z>
The generate_hcl generates HCL files but in your example, it shows an
S3.json file. Is this the one that got generated? Wasn't it a
generate_file block?
The generate_hcl supports only stack code generation (we call this the
generate context), then it generates files only inside stack directories
but you can control which stacks should have it generated. It's *never*
generated inside non-stack directories, if it does, then it's a bug.
The generate_file block, by default, also only generates inside stacks
but the generation context can be set to root and then in this case you
can say explicitly where the file must be generated (not only stacks, any
directory).
When you execute terramate generate, when in stack context, it generates
code for each stack. For each stack, it loads the generate_ definitions
of its directory and parent directories until the root of the repo/project
is reached and then generates the files relative to the stack dir. In other
words, each generate_ declaration generates inside all child stacks.
So in your example, if proj/general/policy is a stack and you have
generate_hcl declarations inside proj/general/ or proj/ then it will
generate inside policy.
This mechanism exists so you can group similar stacks inside a common
directory and then have a single generate_hcl for generating code inside
all of them.
Below are two ways you can solve your problem:
1. Place the generate_hcl or generate_file declaration inside the
stack directory which must have the file generated, then the file is only
generated there.
2. Add a condition attribute in the generate_ block and make the
condition evaluate to false just in the stack which should not have
the file generated.
For the *2* option, see the example below:
# /config.tm.hcl
globals {
generate_file_x = true
}
generate_hcl "x.tf" {
condition = global.generate_file_x
content {
test = 1
}
}
then inside a child stack:
# /general/stack-xpto
globals {
generate_file_x = false
}
The example above also works for generate_file if the context attribute
is not set.
If you're using generate_file block and the file must be generated only
at a single directory, then that's why the context attribute exists, so
you can control the target directory explicitly, making it an absolute path
(relative to the project root):
Example:
# /config.tm
globals {
object = {
test = 1
}
}
generate_file "/some/dir/file.json" {
context = root
content = tm_jsonencode(global.object)
}
let me know it this answer your question.
—
Reply to this email directly, view it on GitHub
<#810 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGHHXM5AXWWZLFOS5HNGBTWTBPGPANCNFSM6AAAAAAT7O5LKE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Hi @az-z
The
generate_hcl
generates HCL files but in your example, it shows anS3.json
file. Is this the one that got generated? Wasn't it agenerate_file
block?The
generate_hcl
supports onlystack
code generation (we call this the generatecontext
), then it generates files only inside stack directories but you can control which stacks should have it generated. It's never generated inside non-stack directories, if it does, then it's a bug.The
generate_file
block, by default, also only generates inside stacks but the generationcontext
can be set toroot
and then in this case you can say explicitly where the file must be generated (not only stacks, any directory).When you execute
terram…