Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - Unable to run multiple commands in local exec

I'm new to Terraform world. I'm trying to run a shell script using Terraform.

Below is the main.tf file

#Executing shell script via Null Resource

resource "null_resource" "install_istio" {
 provisioner "local-exec" {
    command = <<EOT
      "chmod +x install-istio.sh"
      "./install-istio.sh"
    EOT
    interpreter = ["/bin/bash", "-c"]
    working_dir = "${path.module}"
  }
}

Below is the install-istio.sh file which it needs to run

#!/bin/sh

# Download and install the Istio istioctl client binary

# Specify the Istio version that will be leveraged throughout these instructions
ISTIO_VERSION=1.7.3

curl -sL "https://github.com/istio/istio/releases/download/$ISTIO_VERSION/istioctl-$ISTIO_VERSION-linux-amd64.tar.gz" | tar xz

sudo mv ./istioctl /usr/local/bin/istioctl
sudo chmod +x /usr/local/bin/istioctl

# Install the Istio Operator on EKS
istioctl operator init

# The Istio Operator is installed into the istio-operator namespace. Query the namespace.
kubectl get all -n istio-operator

# Install Istio components
istioctl profile dump default

# Create the istio-system namespace and deploy the Istio Operator Spec to that namespace.
kubectl create ns istio-system
kubectl apply -f istio-eks.yaml

# Validate the Istio installation
kubectl get all -n istio-system

I'm getting below warning:

Warning: Interpolation-only expressions are deprecated
  on .terraform/modules/istio_module/Istio-Operator/main.tf line 10, in resource "null_resource" "install_istio":
  10:     working_dir = "${path.module}"
Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.
Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

The above script in main.tf does run command in the background.

Can someone help me with the missing part? How can I run multiple commands using local exec Also, How can I get rid of the warning message?

Appreciate all your help, Thanks!

like image 636
Sweta Sharma Avatar asked Dec 07 '25 09:12

Sweta Sharma


2 Answers

I think there are two separate things going on here which are actually not related.

The main problem here is in how you've written your local-exec script:

    command = <<EOT
      "chmod +x install-istio.sh"
      "./install-istio.sh"
    EOT

This will become the following shell script to run:

"chmod +x install-istio.sh"
"./install-istio.sh"

By putting the first command line in quotes, you're telling the shell to try to run a program that is called chmod +x install-istio.sh without any arguments. That is, the shell will try to find an executable in your PATH called chmod +x install-istio.sh, rather than trying to run a command called just chmod with some arguments as I think you intended.

Remove the quotes around the command lines to make this work. Quotes aren't needed here because neither of these commands contain any special characters that would require quoting:

    command = <<-EOT
      chmod +x install-istio.sh
      ./install-istio.sh
    EOT

The warning message about interpolation-only expressions is unrelated to the problem of running these commands. This is telling you that you've used a legacy syntax that is still supported for backward compatibility but no longer recommended.

If you are using the latest version of Terraform at the time of writing (one of the v0.15 releases, or later) then you may be able to resolve this and other warnings like it by switching into this module directory and running terraform fmt, which is a command that updates your configuration to match the expected style conventions.

Alternatively, you could manually change what that command would update automatically, which is to remove the redundant interpolation markers around path.module:

    working_dir = path.module
like image 169
Martin Atkins Avatar answered Dec 08 '25 21:12

Martin Atkins


Please note that for running several command Terraform suggest using multiple provisioners (ref). Multiple provisioners are executed in the order they're defined in the configuration file. For instance:

resource "null_resource" "install_istio" {
  # ...

  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    working_dir = "${path.module}"
    command = "chmod +x install-istio.sh"
  }

  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    working_dir = "${path.module}"
    command = "./install-istio.sh"
  }
}

I understand that for some cases it might be an overhead because same things (interpreter and working_dir) should be populated several times, but in other cases, it provides flexibility to run exactly how you want.

like image 31
Kyrylo Kravets Avatar answered Dec 08 '25 22:12

Kyrylo Kravets



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!