I am a Terraform (provider) beginner and I try to create an out of the box working script that runs terraform init and does all the preparation that is needed to make that working. For example, registry lookups should be avoided at all times as the Terraform provider to use is just a local binary.
My script is failing (see screenshot) and I really do not understand why. The right files (such as terraformrc) are generated. Binary is copied to the plugin folder etc. It should just work. But I got an error. Look at the screenshots and the script.
How can I rewrite my script to make terraform init work without errors?
#!/usr/bin/env bash
set -e # Exit on any error
###############################################################################
# VARIABLES
###############################################################################
HOME_DIR=$(eval echo "~$USER") # Get the full home directory path
PLUGIN_DIR="$HOME_DIR/.terraform.d/plugins/local/flowtrends/1.0.0/linux_amd64"
TERRAFORMRC_PATH="$HOME_DIR/.terraformrc"
BINARY_NAME="terraform-provider-flowtrends_v1.0.0"
LOCAL_BINARY="./$BINARY_NAME"
MAIN_TF="main.tf"
###############################################################################
# STEP 1: FORCEFULLY OVERWRITE ~/.terraformrc WITH ABSOLUTE PATH
###############################################################################
cat << EOF > "$TERRAFORMRC_PATH"
provider_installation {
dev_overrides {
"local/flowtrends" = "$HOME_DIR/.terraform.d/plugins"
}
direct {
exclude = []
}
}
EOF
echo "Created $TERRAFORMRC_PATH with dev overrides for local/flowtrends."
###############################################################################
# STEP 2: CREATE A GUARANTEED VALID main.tf
###############################################################################
cat << EOF > "$MAIN_TF"
terraform {
required_providers {
flowtrends = {
source = "local/flowtrends"
version = "1.0.0"
}
}
}
provider "flowtrends" {}
EOF
echo "Created $MAIN_TF with source = \"local/flowtrends\"."
###############################################################################
# STEP 3: CREATE THE NECESSARY PLUGIN DIRECTORY STRUCTURE
###############################################################################
mkdir -p "$PLUGIN_DIR"
echo "Created directory structure: $PLUGIN_DIR"
###############################################################################
# STEP 4: COPY THE PROVIDER BINARY TO THE CORRECT LOCATION
###############################################################################
if [[ ! -f "$LOCAL_BINARY" ]]; then
echo "ERROR: Local provider binary $LOCAL_BINARY not found in the current directory."
echo "Please place the binary here and rerun the script."
exit 1
fi
cp "$LOCAL_BINARY" "$PLUGIN_DIR/"
echo "Copied provider binary to $PLUGIN_DIR/"
###############################################################################
# STEP 5: SET EXECUTABLE PERMISSION FOR THE PROVIDER BINARY
###############################################################################
chmod +x "$PLUGIN_DIR/$BINARY_NAME"
echo "Set execute permissions on $PLUGIN_DIR/$BINARY_NAME."
###############################################################################
# STEP 6: CLEAR TERRAFORM CACHE (FORCE CLEAN START)
###############################################################################
echo "Cleaning up Terraform cache..."
rm -rf .terraform/ .terraform.lock.hcl
###############################################################################
# STEP 7: FORCE DISABLE REGISTRY IN main.tf
###############################################################################
cat << EOF >> "$MAIN_TF"
// DISABLING REGISTRY LOOKUP
terraform {
backend "local" {}
}
EOF
###############################################################################
# STEP 8: RUN `terraform init` AND FORCE REGISTRY BYPASS
###############################################################################
echo "Running terraform init..."
terraform init -input=false
UPDATE
Using filesystem_mirror should work (see this new script) but unfortunately gives the same error.
#!/usr/bin/env bash
set -e # Exit on any error
###############################################################################
# VARIABLES
###############################################################################
HOME_DIR=$(eval echo "~$USER") # Get the full home directory path
PLUGIN_DIR="$HOME_DIR/.terraform.d/plugins/local/flowtrends/1.0.0/linux_amd64"
TERRAFORMRC_PATH="$HOME_DIR/.terraformrc"
BINARY_NAME="terraform-provider-flowtrends_v1.0.0"
LOCAL_BINARY="./$BINARY_NAME"
MAIN_TF="main.tf"
###############################################################################
# STEP 1: CONFIGURE ~/.terraformrc WITH FILESYSTEM MIRROR
###############################################################################
cat << EOF > "$TERRAFORMRC_PATH"
provider_installation {
filesystem_mirror {
path = "$HOME_DIR/.terraform.d/plugins"
}
direct {
exclude = []
}
}
EOF
echo "Configured $TERRAFORMRC_PATH with filesystem_mirror."
###############################################################################
# STEP 2: CREATE A GUARANTEED VALID main.tf
###############################################################################
cat << EOF > "$MAIN_TF"
terraform {
required_providers {
flowtrends = {
source = "local/flowtrends"
version = "1.0.0"
}
}
}
provider "flowtrends" {}
EOF
echo "Created $MAIN_TF with source = \"local/flowtrends\"."
###############################################################################
# STEP 3: CREATE THE NECESSARY PLUGIN DIRECTORY STRUCTURE
###############################################################################
mkdir -p "$PLUGIN_DIR"
echo "Created directory structure: $PLUGIN_DIR"
###############################################################################
# STEP 4: COPY THE PROVIDER BINARY TO THE FILESYSTEM MIRROR LOCATION
###############################################################################
if [[ ! -f "$LOCAL_BINARY" ]]; then
echo "ERROR: Local provider binary $LOCAL_BINARY not found in the current directory."
echo "Please place the binary here and rerun the script."
exit 1
fi
cp "$LOCAL_BINARY" "$PLUGIN_DIR/"
echo "Copied provider binary to $PLUGIN_DIR/"
###############################################################################
# STEP 5: SET EXECUTABLE PERMISSION FOR THE PROVIDER BINARY
###############################################################################
chmod +x "$PLUGIN_DIR/$BINARY_NAME"
echo "Set execute permissions on $PLUGIN_DIR/$BINARY_NAME."
###############################################################################
# STEP 6: CLEAR TERRAFORM CACHE (FORCE CLEAN START)
###############################################################################
echo "Cleaning up Terraform cache..."
rm -rf .terraform/ .terraform.lock.hcl
###############################################################################
# STEP 7: RUN `terraform init` AND FORCE FILESYSTEM MIRROR USAGE
###############################################################################
echo "Running terraform init..."
terraform init -input=false


Addressin the provider code?local/flowtrendsbut I keep on getting the same issue. There must be some way to just force terraform to only look locally right? The idea behind my script is that everything is prepared to ensureterraform initjust works out of the box. It would be scary if I can not test some providers locally at all because of their code. But as said, even when fixing this to the mentioned value, same problem.go env GOBINreturn?go env GOBINreturns/home/daan-myfamilyname/go/bin