I have following loop, script, bash 3.2 is installed:
#!/bin/bash -e
set -e
set -u
set -o pipefail
somefunc() {
jq -c '.[]' <"${imports_json}" | while read -r i; do
address=$(echo "$i" | jq -r '.address')
item_id=$(echo "$i" | jq -r '.id')
echo "Importing [${address}], Id: [${item_id}]"
terraform -chdir="${terraform_dir}" import -state="${state}" "${address}" "${item_id}"
done
echo "Import loop is done ..."
}
...
# First invoke, all iterated
somefunc()
some other script code
# Second invoke, single import while expected to go over long list
somefunc()
It is simple loop over json array:
[
{
"address": "some_terraform_resource.resource[\"some_name\"]",
"id": "some_resource_id"
},
{
"address": "another_terraform_resource.resorce",
"id": "another_id"
}
...
]
I run it in several places in long script
In one place it is just doing one iteration and quits the loop, with no error code and echo "Import loop is done ..."
following the loop executed. So, as if this terraform import
command acts as a break
.
If I comment terraform import
command out all array is iterated. If I change json content, is just one single import and break, no matter what resource is imported.
Just for experiment, I've tried:
- to suppress output (
> /dev/null
,2>&1
). || true
, even thoughterraform import
is returning 0 error code.- comment out
set
commands at the beginning of the script - run
terraform import ...
in subshell,(terraform import ....)
No effect in all cases
Trace with bash -x
:
Importing [some_terraform_resource.resource["some_name"]], Id: [some_resource_id], state: "/Users/<some-path>/baseline.tfstate"
+ terraform -chdir=/Users/<some-path>/project-for-import import -state=/Users/<some-path>/baseline.tfstate 'some_terraform_resource.resource["some_name"]' some_resource_id
+ read -r i
+ echo 'Terraform import is done'
I am pulling my hair out, what can cause loop to break ? What are the ways to troubleshoot it?