External data source throwing permission denied error in git actions?

Hello,

I am using git actions for deployment with terraform but external data source section is throwing an error. This is my data source

data "external" "token" {
  depends_on = [azurerm_mssql_database.sql]
  program    = ["${path.module}/scripts/myscript.sh"]

}

This error is coming. I can’t use sudo. Please help. Thank you.

Error: External Program Lookup Failed

on …/…/…/main.tf line 222, in data “external” “token”:
222: program = [“${path.module}/scripts/myscript.sh”]

The data source received an unexpected error while attempting to find the program

The program must be accessible according to the platform where Terraform is
The program must be accessible according to the platform where Terraform is
running.

If the expected program should be automatically found on the platform where
Terraform is running, ensure that the program is in an expected directory.
On Unix-based platforms, these directories are typically searched based on
the ‘$PATH’ environment variable. On Windows-based platforms, these
directories are typically searched based on the ‘%!P(MISSING)ATH%!’(MISSING) environment
variable.

If the expected program is relative to the Terraform configuration, it is
recommended that the program name includes the interpolated value of
‘path.module’ before the program name to ensure that it is compatible with
varying module usage. For example: “${path.module}/my-program”

The program must also be executable according to the platform where
Terraform is running. On Unix-based platforms, the file on the filesystem
must have the executable bit set. On Windows-based platforms, no action is
typically necessary.

Platform: linux
Program: …/…/…/scripts/myscript.sh
Error: exec: “…/…/…/scripts/myscript.sh”:
permission denied

Hi @smartaquarius10,

Since the error message here was “permission denied” I think the following is the relevant part of that long error message:

The program must also be executable according to the platform where
Terraform is running. On Unix-based platforms, the file on the filesystem
must have the executable bit set.

Linux is a “Unix-based platform” for this purpose, and so you’ll need to mark your script as executable in order to run it in this way.

An alternative approach which would avoid the need for your script itself to be executable would be to explicitly run it through the shell, and thus it would be the shell that the provider would be executing, rather than directly the script:

  program = ["sh", "${path.module}/scripts/myscript.sh"]

If you are using bash-specific shell features then you may need to use "bash" instead of just "sh" but I showed "sh" here for maximum compatibility, since that generic name should be available on all reasonable Unix systems and refer to a shell which provides typical POSIX shell functionality, but perhaps not with any extensions offered by “modern” shells like Bash.

@apparentlymart Still not working. This is the complete error coming now :frowning:

Error: External Program Execution Failed
 
   with module.TestModule.data.external.token,
   on ../../../main.tf line 25, in data "external" "token":
   25:   program = ["bash","${path.module}/scripts/myscript.sh"]
 
 The data source received an unexpected error while attempting to execute
 the program.
 
 The program was executed, however it returned no additional error
 messaging.
 
 Program: /usr/bin/bash
 State: exit status 1

@apparentlymart , with the help of your suggestion I’m able to resolve script running issue but it is still not reading the output. So, from shell script I am returning a Json like this

echo "{\"token\":\"$ACCESS_TOKEN\"}"

But terraform is always throwing this error.

Program output must be a JSON encoded map of string keys and string values. Invalid character ‘{’ after top-level value

I’m wondering if there are additional output characters. Could you try passing the output through jq?

echo ...... |jq

Thanks it worked used jq command directly. :slight_smile:

I have the similar issue but it says

The data source received an unexpected error while attempting to find the
program.

The program must be accessible according to the platform where Terraform is
running.

If the expected program should be automatically found on the platform where
Terraform is running, ensure that the program is in an expected directory. On
Unix-based platforms, these directories are typically searched based on the
'$PATH' environment variable. On Windows-based platforms, these directories
are typically searched based on the '%PATH%' environment variable.

If the expected program is relative to the Terraform configuration, it is
recommended that the program name includes the interpolated value of
'path.module' before the program name to ensure that it is compatible with
varying module usage. For example: "${path.module}/my-program"

The program must also be executable according to the platform where Terraform
is running. On Unix-based platforms, the file on the filesystem must have the
executable bit set. On Windows-based platforms, no action is typically
necessary.

Platform: linux
Program: bash
Error: exec: "bash": executable file not found in $PATH

Could you please help me with this?

data "external" "credentials" {
  working_dir = "${path.module}/scripts"
  program = [
    "bash",
    "myfile.sh",
  ]
}

Which is your current $PATH setup?
echo $PATH

You can also try to use full qualified paths to bash using:

/usr/bin/bash
or
/bin/bash

In case it is installed - obviously.