scottwinkler / terraform-provider-shell

Terraform provider for executing shell commands and saving output to state file
Mozilla Public License 2.0
279 stars 60 forks source link

Permission denied. Error occured during shell execution #117

Closed pforyszewski closed 1 year ago

pforyszewski commented 1 year ago

I have created a script to create Azure IoT Device based on this topic.

RESOURCE

resource "shell_script" "register_iot_edge_device" {
  lifecycle_commands {
    create = "$script create"
    read   = "$script read"
    delete = "$script delete"
  }

  environment = {
    iot_hub_name         = "bc-cloud-iot-hub-${random_string.salt.result}"
    iot_edge_device_name = local.iot_device_name
    script               = "scripts/register_iot_edge_device.sh"
  }
}

SCRIPT

#!/bin/bash

set -e

create() {
    az iot hub device-identity create --device-id "$IOT_EDGE_DEVICE_NAME" --edge-enabled --hub-name "$IOT_HUB_NAME" --output none
    # shellcheck disable=SC2162
    read
}

read() {
    az iot hub device-identity connection-string show --device-id "$IOT_EDGE_DEVICE_NAME" --hub-name "$IOT_HUB_NAME"
}

delete() {
    az iot hub device-identity delete --device-id "$IOT_EDGE_DEVICE_NAME" --hub-name "$IOT_HUB_NAME"
}

# Check if the function exists (bash specific)
if declare -f "$1" >/dev/null; then
    # call arguments verbatim
    "$@"
else
    # Show a helpful error
    echo "'$1' is not a known function name" >&2
    exit 1
fi

Every try ends up with an ERROR

│ Error: Error occured during shell execution.
│ Error: 
│ exit status 126
│ 
│ Command: 
│ $script create
│ 
│ StdOut: 
│
│ StdErr: 
│ /bin/sh: 1: scripts/register_iot_edge_device.sh: Permission denied

I don't even know how to start solving it and I haven't found such an issue with this provider on the internet. There were some similar topics abour remote-exec and users were just adding chmod -x to the command but I can't see a way to do this here.

pforyszewski commented 1 year ago

Damm... It was all about this: Changed

script = "scripts/register_iot_edge_device.sh"

to

script = "./scripts/register_iot_edge_device.sh"