0

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

7
  • How did you define the Address in the provider code? Commented Dec 31, 2024 at 10:06
  • @MarkoE I just changed that into local/flowtrends but 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 ensure terraform init just 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. Commented Dec 31, 2024 at 10:29
  • 1
    Please do not post screenshots of error messages or text (in general). Commented Dec 31, 2024 at 10:42
  • @Daan What does go env GOBIN return? Commented Dec 31, 2024 at 10:46
  • @MarkoE go env GOBIN returns /home/daan-myfamilyname/go/bin Commented Dec 31, 2024 at 11:05

1 Answer 1

3

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:

  • Binary permissions are correct
  • No leftover .terraform or .terraform.lock.hcl files
  • The provider binary is actually compiled for linux_amd64

Let me know how it goes

Sign up to request clarification or add additional context in comments.

1 Comment

Unless you actually own the GitHub organization "local" -- which corresponds to the registry.terraform.io namespace "local" -- I would suggest using an address that actually belongs to you rather than pretending to own an address you don't. If you're installing a provider from a local source anyway then you can use literally any valid hostname you like, and so the most typical answer is to use a domain name you own as the registry hostname, which therefore ensures that you can't possibly be using a source address that belongs to someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.