2

I need to use some gcloud commands in order to create a Redis instance on GCP as terraform does not support some options that I need.

I'm trying this:

terraform {
  # Before apply, run script.
  before_hook "create_redis_script" {
    commands     = ["apply"]
    execute  = ["REDIS_REGION=${local.module_vars.redis_region}", "REDIS_PROJECT=${local.module_vars.redis_project}", "REDIS_VPC=${local.module_vars.redis_vpc}", "REDIS_PREFIX_LENGHT=${local.module_vars.redis_prefix_lenght}", "REDIS_RESERVED_RANGE_NAME=${local.module_vars.redis_reserved_range_name}", "REDIS_RANGE_DESCRIPTION=${local.module_vars.redis_range_description}", "REDIS_NAME=${local.module_vars.redis_name}", "REDIS_SIZE=${local.module_vars.redis_size}", "REDIS_ZONE=${local.module_vars.redis_zone}", "REDIS_ALT_ZONE=${local.module_vars.redis_alt_zone}", "REDIS_VERSION=${local.module_vars.redis_version}", "bash", "../../../scripts/create-redis-instance.sh"]
  }

The script is like this:

echo "[+]Creating IP Allocation Automatically using <$REDIS_VPC-network\/$REDIS_PREFIX_LENGHT>"
gcloud compute addresses create $REDIS_RESERVED_RANGE_NAME \
    --global \
    --purpose=VPC_PEERING \
    --prefix-lenght=$REDIS_PREFIX_LENGHT \
    --description=$REDIS_RANGE_DESCRIPTION \
    --network=$REDIS_VPC

The error I get is:

terragrunt apply
5b35d0bf15d0a0d61b303ed32556b85417e2317f
5b35d0bf15d0a0d61b303ed32556b85417e2317f
5b35d0bf15d0a0d61b303ed32556b85417e2317f
ERRO[0002] Hit multiple errors:
Hit multiple errors:
exec: "REDIS_REGION=us-east1": executable file not found in $PATH 
ERRO[0002] Unable to determine underlying exit code, so Terragrunt will exit with error code 1
Jose Luis Delgadillo
  • 2,348
  • 1
  • 6
  • 16
Kaleby Cadorin
  • 179
  • 2
  • 13
  • I was running into the same error. You could trying something like `execute = ["env", "REDIS_REGION=${local.module_vars.redis_region}" ...]`. The `env` command will set the env variable... In my case I wasn't getting any errors but the env variable was still not getting set.. So, instead of doing this in the `before_hook` I did it in `.envrc` (see https://direnv.net/) – Svetozar Misljencevic Nov 16 '21 at 18:41

1 Answers1

1

I encountered the same issue and resigned myself to pass the values as parameters instead of environment variables.

It involves to modify the script and is a far less clearer declaration, but it works :|

jobwat
  • 8,527
  • 4
  • 31
  • 30