title: Can i pass variables to AWS EC2 instances created with terraform and user_data? Or update an already created resource
body: <p>I’m new on learning terraform with AWS and by i understand i can create an EC2 instances and pass it some Terminal commands (via “user_data”) in order to install software, etc…</p>
  <p>I wonder if i can i install Docker, download a repository, build an image and deploy.</p>
  <p>But if i have a fullstack app, and i have:</p>
  <p>Instance_1: Backend</p>
  <p>Instance_2: Frontend</p>
  <p>How can i catch the IP of Instance_1 (via ‘output’) and pass it to the source code of the app of the frontend stored on Instance_2 in order to make the Frontend capable of connect to the Backend on Instance_1?.</p>
  <p>I know that i can download source code via github and build it via Docker</p>
  <p>if i create an EC2 instance</p>
  <pre><code>resource &quot;aws_instance&quot; &quot;Mi_Primer_Servidor&quot;{
  
    #Verificar el 'id' de la ami y las configuraciones con AWS
    ami=&quot;ami-056cb9ae6e2df09e8&quot;
  
    #Seleccionamos el tipo de maquina virtual
    instance_type = &quot;t2.micro&quot;
  
    #Asignamos una zona de disponibilidad (OPCIONAL)
    availability_zone = &quot;us-west-2a&quot;
  
    #Se recomienda crear previamente la llave en AWS y descargar el archivo
    #O usar una llave ya existente
    key_name = &quot;terraform&quot;
  
    #Definimos la interfaz de red (OPCIONAL)
    network_interface {
      device_index = 0
      network_interface_id = aws_network_interface.mi_interfaz_de_red.id
    }
  
    #A continuacion haremos que la maquina virtual ejecute algunos 
    #comandosvia consola como instalar algunos programas 
    #En este caso vamos a instalar y ejecutar un servidor apache
    user_data = &lt;&lt;-EOF
                #!/bin/bash
                sudo apt-get update -y
                sudo apt-get upgrade -y
                sudo apt install apache2 -y
                sudo systemctl start apache2
                sudo bash -c 'echo Mi primer servidor con terraform &gt; /var/www/html/index.html'
              EOF   
              
    #Etiquetas
    tags = {
      Name = &quot;Mi servidor con terraform&quot;
    }
   }
  </code></pre>
  <p>I don't know if i can pass 'user_data' commands after the instance is created and pass it a captured output value in order to update the resource.</p>
  <p>Or i don't know if there are other method</p>
  <p>Thank you and sorry for the english</p>