Closed franz-josef-kaiser closed 3 years ago
Did you ever run apply
on the version
module? If you didn't, then no state file will exist, and calling terraform output
will fail with the error shown.
Also, you can simplify things by using run_cmd
to run your script, instead of a separate module.
@brikis98 I used run_cmd
in the end, as written above.
Out of curiosity: Can you imagine a way to make this easier than running the dependency as if it would be a completely separate project with separate state, etc.? For e.g. with a local-exec
provisioner? I could not get this to work. Thanks in advance!
If run_cmd
works, why do you need dependency?
I don't. I am just curious and want to learn.
Closing this as the original question has been answered. If there are any follow ups, please open a new ticket with updated context. Thanks!
Task:
Version remote state by changing location.
Change remote state location when Git tag is incremented:
https://gitlab.com/api/v4/projects/<PROJECT_ID>/terraform/state/<ENV>-<GIT_TAG>
Attempt:
Use
external
data source.external
data source to rungit
command.output
.dependencies
to set execution order.dependency
to extract the data and load it intoinputs
for usage.The directory structure is the following:
The root
terragrunt.hcl
has the following contents (locals
omitted) to build thebackend
sconfig
data like the remote state backendaddress
:The version
module
is simply a call to a shell script and anoutput
to make data available to the rootbackend:config
.(open shell script) The shell script itself is simply a call to `git`, wrapped in a call to `jq` to transform the output to valid JSON.
```python #!/usr/bin/env bash set -eo pipefail VERSION=$(git describe --tags --abbrev=0 | sed 's/\.//g') jq -n \ --arg version "${VERSION}" \ '{"version":$version}' ```
Problem
Fetching the Git tag and providing it to Terraform or Terragrunt seems to fail with
output
not being created up front.(open full log) Running `terragrunt plan` runs `terraform init` in the `module/version` (dependency) folder and creates the `.terraform-cache/` dir. It then fails because of a missing `output`. The `terraform.tfstate` file has **not been created**. ```python [terragrunt] 2020/11/04 12:32:50 /path/to/root/modules/version/terragrunt.hcl is a dependency of /path/to/root/prod/main/terragrunt.hcl but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block. ```
### Log ```python $ terragrunt plan -out plan.cache [INFO] Getting version from tgenv-version-name [INFO] TGENV_VERSION is 0.25.5 [terragrunt] [/path/to/root/prod/main] 2020/11/04 12:32:03 Running command: terraform --version [terragrunt] 2020/11/04 12:32:07 Terraform version: 0.13.5 [terragrunt] 2020/11/04 12:32:07 Reading Terragrunt config file at /path/to/root/prod/main/terragrunt.hcl [terragrunt] 2020/11/04 12:32:07 WARNING: Could not parse remote_state block from target config /path/to/root/modules/version/terragrunt.hcl [terragrunt] 2020/11/04 12:32:07 WARNING: Falling back to terragrunt output. [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:07 Running command: terraform --version [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:11 Terraform version: 0.13.5 [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:11 Reading Terragrunt config file at /path/to/root/modules/version/terragrunt.hcl [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:11 Downloading Terraform configurations from file:///path/to/root/modules/version into /path/to/root/modules/version/.terragrunt-cache/ly5VMHkzUWbonYSR1gKb-vAc8h0/cmWUxX-13YOmtGNQ8iWOkHKAA6Y [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:11 Copying files from /path/to/root/modules/version into /path/to/root/modules/version/.terragrunt-cache/ly5VMHkzUWbonYSR1gKb-vAc8h0/cmWUxX-13YOmtGNQ8iWOkHKAA6Y [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:11 Setting working directory to /path/to/root/modules/version/.terragrunt-cache/ly5VMHkzUWbonYSR1gKb-vAc8h0/cmWUxX-13YOmtGNQ8iWOkHKAA6Y [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:11 Running command: terraform init Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/external... - Installing hashicorp/external v2.0.0... - Installed hashicorp/external v2.0.0 (signed by HashiCorp) The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, we recommend adding version constraints in a required_providers block in your configuration, with the constraint strings suggested below. * hashicorp/external: version = "~> 2.0.0" Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. ####### FAILS HERE ######## [terragrunt] [/path/to/root/modules/version] 2020/11/04 12:32:49 Running command: terraform output -json [terragrunt] 2020/11/04 12:32:50 /path/to/root/modules/version/terragrunt.hcl is a dependency of /path/to/root/prod/main/terragrunt.hcl but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block. [terragrunt] 2020/11/04 12:32:50 Unable to determine underlying exit code, so Terragrunt will exit with error code 1 ``` …
Switching to the
modules/version/
dir manually and callingterragrunt init
does not change this. Still there is noterraform.tfstate
file that holds theoutput
. And neither is there a.terraform
dir.The
.terraform
dir will only get created when callingterraform init
inside themodules/version/
dir manually.So after running
terraform init; terraform refresh
in themodules/version/
dir manually, creates the output. It can then get used by the rootterragrunt.hcl
to construct theremote_state
address
, etc.I tried using a
before_hook
to trigger the generation, but nothing runs beforeinit
.There's also the thing, that this would need a manual refresh after each single Git tag generation.
In the end, I used the Bash script and let it simply run with
I just have to make sure to
echo -n
the$VERSION
instead of outputting JSON. (Note:-n
to avoid new lines at the end`).Questions:
local exec
here?init
?inputs
/ Env vars inside theterragrunt.hcl
file?