<p>I am using <code>terraform</code> to deploy EKS and some other stuff to AWS cloud. and I also has some k8s configuration yml file to deploy k8s pods/deployment/load balancer etc.</p>
<p>I need to grab some values from terraform output such as vpc id, subnet id etc. and put them to k8s spec yml file. What is the best way to do that automatically?</p>
<p>For example,</p>
<p>I have configure file as below:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ServiceAccount
metadata:
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::xxxx:role/xxxx
  name: aws-load-balancer-controller
  namespace: kube-syste
</code></pre>
<p>The role arn value <code>arn:aws:iam::xxxx:role/xxxx</code> should be from the role terraform deployed which is in the output. How can I pass the terraform output to k8s yml file?</p>
<p>Can I use a command like <code>kubectl apply -f xxx.yml -v variables.json</code>?</p>

Joey Yi Zhao
<p>You can access the remote state value from the</p>
<pre><code>https://www.terraform.io/docs/language/state/remote-state-data.html

data &quot;terraform_remote_state&quot; &quot;vpc&quot; {
  backend = &quot;remote&quot;

  config = {
    organization = &quot;hashicorp&quot;
    workspaces = {
      name = &quot;vpc-prod&quot;
    }
  }
}

# Terraform &gt;= 0.12
resource &quot;aws_instance&quot; &quot;foo&quot; {
  # ...
  subnet_id = data.terraform_remote_state.vpc.outputs.subnet_id
}

# Terraform &lt;= 0.11
resource &quot;aws_instance&quot; &quot;foo&quot; {
  # ...
  subnet_id = &quot;${data.terraform_remote_state.vpc.subnet_id}&quot;
}
</code></pre>
<p>from here what you can is to run the command <code>terraform output -json</code></p>
<pre><code>terraform output -json | jq -r '@sh &quot;export EXAMPLE1=\(.example1.value)&quot;'
</code></pre>
<p>terraform output file something like</p>
<pre><code>output &quot;example1&quot; {
  value = &quot;hello&quot;
}
</code></pre>
<p>instead of <code>hello</code> set anything or varible.</p>
<p>terraform command will set value</p>
<pre><code>export EXAMPLE1='hello'
</code></pre>
<p>now again using the ubuntu simple command you can replace the OS environment variables values will K8s spec file.</p>
<p>check example at : <a href="https://stackoverflow.com/questions/65315202/can-i-set-terraform-output-to-env-variables#:%7E:text=One%20way%20you%20could%20do,statements%20to%20set%20environment%20variables">https://stackoverflow.com/questions/65315202/can-i-set-terraform-output-to-env-variables#:~:text=One%20way%20you%20could%20do,statements%20to%20set%20environment%20variables</a>.</p>
