'terraform - failing to set environment variable using local-exec

As part of terraform run I'm trying to set environment variables on my Linux server using "local-exec" and command (I need to use it with a different application)

resource "null_resource" "set_env3" {
  provisioner "local-exec" {
    command = "export BASTION_SERVER_PUBLIC_IP=2.2.2.2"
  }
}

But when running "echo $BASTION_SERVER_PUBLIC_IP" on my Linux server i'm getting an empty output and I also can't locate BASTION_SERVER_PUBLIC_IP parameter when running "printenv"

BTW - I have tried to run the following - but again i can not find the parameter

resource "null_resource" "update34" {
  provisioner "local-exec" {
   command = "env"
   environment = {
       BASTION = "5.5.5.5"
     }
   }
}


Solution 1:[1]

An export like that just exports the environment variable so it is available to other child processes spawned by the terraform process. It doesn't export it to the parent, Unix shell process. In short: this is never going to work. You can't use Terraform null_resource to set your local computer's environment variables.

What you need to do is define the value as a Terraform output. Then after you run terraform apply you do something like the following:

export BASTION_SERVER_PUBLIC_IP=$(terraform output bastion_public_ip --raw)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark B