Reputation: 3602
I have the following Terraform configuration that runs a simple gcloud command using a null_resource with a local-exec provisioner.
resource "null_resource" "test_gcloud" {
provisioner "local-exec" {
command = <<EOT
gcloud version
EOT
}
}
This works perfectly fine when I run terraform apply locally. However, when I run it on Terraform Cloud, I get the following error:
/bin/sh: 1: gcloud: not found
What I have tried:
terraform-google-modules/gcloud/google
, but still not working.resource "null_resource" "install_gcloud" {
provisioner "local-exec" {
command = <<EOT
if ! command -v gcloud &> /dev/null; then
echo "gcloud not found. Installing gcloud SDK..."
curl -o google-cloud-sdk.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-409.0.0-linux-x86_64.tar.gz
tar -xvzf google-cloud-sdk.tar.gz
./google-cloud-sdk/install.sh -q
export PATH=$PATH:$(pwd)/google-cloud-sdk/bin
echo 'export PATH=$PATH:$(pwd)/google-cloud-sdk/bin' >> ~/.bashrc
else
echo "gcloud is already installed."
fi
EOT
}
}
resource "null_resource" "test_gcloud" {
provisioner "local-exec" {
command = <<EOT
gcloud version
EOT
}
}
Question:
1. Why does gcloud work locally but not in Terraform Cloud?
2. How can I make gcloud available in Terraform Cloud so I can run commands using local-exec?
Any advice or solutions would be appreciated. Thanks!
Upvotes: 0
Views: 58
Reputation: 74209
When you use "remote operations" with HCP Terraform (formerly Terraform Cloud), you are running Terraform CLI in HCP Terraform's run environment, and so your configuration can only use software that is available in that environment.
That documentation makes a pretty broad statement at the time of writing:
The operating system and other software installed on the worker VMs is an internal implementation detail of HCP Terraform. It is not part of a stable public interface, and is subject to change at any time.
Since the local-exec
provisioner's purpose is to execute other software available in the execution environment, the above statement effectively means that you should not use the local-exec
provisioner in HCP Terraform at all, because no specific external software is guaranteed to be available for you to run with it.
Instead, you will need to find a way to achieve what you want to achieve using features of Terraform providers such as hashicorp/google
, rather than using provisioners. Otherwise you will need to disable remote operations for your workspace and then run Terraform on a computer that has the gcloud
program installed.
Upvotes: 0