terraformterraform-provider-gcp

Why is my terraform init automation script failing?


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

enter image description here enter image description here


Solution

  • The issue is with the provider path structure and configuration. Terraform expects local providers to include registry.terraform.io in the path, even when used locally

    #!/bin/bash
    set -e
    
    # Set up correct plugin directory structure
    PLUGIN_DIR="$HOME/.terraform.d/plugins/registry.terraform.io/local/flowtrends/1.0.0/linux_amd64"
    mkdir -p "$PLUGIN_DIR"
    
    # Copy provider binary
    cp terraform-provider-flowtrends_v1.0.0 "$PLUGIN_DIR/"
    chmod +x "$PLUGIN_DIR/terraform-provider-flowtrends_v1.0.0"
    
    # Configure .terraformrc
    cat > "$HOME/.terraformrc" << EOF
    provider_installation {
      filesystem_mirror {
        path    = "$HOME/.terraform.d/plugins"
        include = ["registry.terraform.io/local/*"]
      }
      direct {
        exclude = ["registry.terraform.io/local/*"]
      }
    }
    EOF
    
    # Create main.tf
    cat > main.tf << EOF
    terraform {
      required_providers {
        flowtrends = {
          source  = "registry.terraform.io/local/flowtrends"
          version = "1.0.0"
        }
      }
    }
    
    provider "flowtrends" {}
    EOF
    
    terraform init -input=false
    

    If you're still getting errors, check:

    Let me know how it goes