<p>I am working with Terraform to luanch an Ec2 instance, where I want to execute some commands.
I put a bash file in user_data on aws_instance resource, but the file is not executed.</p>
<pre><code>...
resource &quot;aws_instance&quot; &quot;master&quot; {
  ami           = lookup(var.amis, var.region)
  instance_type = var.master_instance_type
  key_name      = &quot;id_rsa_sdtd&quot;
  security_groups = [aws_security_group.SDTD_VPC_Security_Group.id]
  subnet_id = aws_subnet.SDTD_VPC_Subnet.id
  iam_instance_profile = &quot;k8s-cluster-iam-master-role&quot;
  user_data = file(&quot;./master_config.sh&quot;)

}
...
</code></pre>
<p>Terraform plan</p>
<pre><code>...
 + tenancy                      = (known after apply)
 + user_data                    = &quot;def16ed029e3ecb79f09f750c548beed53d7f60d&quot;
 + volume_tags                  = (known after apply)
...
</code></pre>
<p>my bash file</p>
<pre><code>#!/bin/bash
# Install kubeadm and Docker
sudo apt update &amp;&amp; sudo apt -y upgrade
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository &quot;deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable&quot;
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo bash -c 'echo &quot;deb https://apt.kubernetes.io/ kubernetes-xenial main&quot; &gt; /etc/apt/sources.list.d/kubernetes.list'
sudo apt update
sudo apt install -y docker-ce kubelet kubeadm kubectl
...
</code></pre>

<p>There is no word in your question confirming <code>master_config.sh</code> hasnt been executed.</p>
<p>Below line doesnt mean it hasnt been executed. If it doesnt work - you should  definitely see a log <code>/var/log/cloud-init-output.log</code> as suggested in the comments
<code> + user_data                    = &quot;def16ed029e3ecb79f09f750c548beed53d7f60d&quot;</code></p>
<p>From the solutions that comes to my mind:</p>
<ul>
<li>use full path in below format, not simple <code>file(&quot;./master_config.sh&quot;)</code>. Maybe you wrapped in incorrectly.</li>
</ul>
<pre><code>resource &quot;aws_instance&quot; &quot;...&quot; {
  user_data = &quot;${file(&quot;/../../master_config.sh&quot;)}&quot;
  ...
}
</code></pre>
<ul>
<li>I would recommend you to use <a href="https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file" rel="nofollow noreferrer">templates_file</a> for such activities and then refer it in your configuration.</li>
</ul>
<pre><code>data &quot;template_file&quot; &quot;user_data&quot; {
  template = &quot;${file(&quot;/../../master_config.sh&quot;)}&quot;
  ...
}


resource &quot;aws_instance&quot; &quot;master&quot; {
  user_data = &quot;${data.template_file.user_data.rendered}&quot;
  ...
}

</code></pre>
