title: Terraform remote-exec Failed to upload script: scp: /tmp permission denied
body: <p>I am heavily struggling with the local-exec by Terraform.
  I am deploying a an EC2 instance with an own built AMI from amazon-linux-2.</p>
  <p>I have this code:</p>
  <pre><code>locals {
    ssh_user_home = &quot;/home/ec2-user&quot;
  }
  
  resource &quot;null_resource&quot; &quot;rerun&quot; {
    triggers = {
      rerun = uuid()
    }
  
  
    provisioner &quot;file&quot; {
      source      = &quot;${path.module}/sources&quot;
      destination = &quot;${local.ssh_user_home}/tmp&quot;
  
      connection {
        type        = &quot;ssh&quot;
        user        = &quot;${var.aws_ssh_user}&quot;
        private_key = &quot;${data.aws_secretsmanager_secret_version.kibana_proxy_ssh_value.secret_string}&quot;
        host        = &quot;${aws_instance.logstash.private_ip}&quot;
      }
    }
  
    provisioner &quot;file&quot; {
      source      = &quot;./creds&quot;
      destination = &quot;${local.ssh_user_home}/tmp&quot;
  
      connection {
        type        = &quot;ssh&quot;
        user        = &quot;${var.aws_ssh_user}&quot;
        private_key = &quot;${data.aws_secretsmanager_secret_version.kibana_proxy_ssh_value.secret_string}&quot;
        host        = &quot;${aws_instance.logstash.private_ip}&quot;
      }
    }
  
    provisioner &quot;remote-exec&quot; {
      inline = [
  
        &quot;cd ${local.ssh_user_home}/tmp&quot;,
        &quot;cp creds/.htpasswd.${var.aws_env} creds/.htpasswd&quot;,
        &quot;bash -x sources/ansible.sh ${var.es_fqdn} ${var.kibana_domain}&quot;,
        # &quot;rm -r /tmp/creds/&quot;,
        # &quot;rm -r /tmp/sources/&quot;,
      ]
  
      connection {
        type        = &quot;ssh&quot;
        user        = &quot;${var.aws_ssh_user}&quot;
        private_key = &quot;${data.aws_secretsmanager_secret_version.kibana_proxy_ssh_value.secret_string}&quot;
        host        = &quot;${aws_instance.logstash.private_ip}&quot;
        #script_path = &quot;${local.ssh_user_home}&quot;
      }
    }
  }
  
  </code></pre>
  <p>There is always this error:</p>
  <pre><code>module.logstash-instance.null_resource.rerun (remote-exec): Connecting to remote host via SSH...
  module.logstash-instance.null_resource.rerun (remote-exec):   Host: 10.135.202.29
  module.logstash-instance.null_resource.rerun (remote-exec):   User: ec2-user
  module.logstash-instance.null_resource.rerun (remote-exec):   Password: false
  module.logstash-instance.null_resource.rerun (remote-exec):   Private key: true
  module.logstash-instance.null_resource.rerun (remote-exec):   Certificate: false
  module.logstash-instance.null_resource.rerun (remote-exec):   SSH Agent: false
  module.logstash-instance.null_resource.rerun (remote-exec):   Checking Host Key: false
  mmodule.logstash-instance.null_resource.rerun (remote-exec): Connected!
  
  Failed to upload script: scp: /tmp: Permission denied
  </code></pre>
  <p>No idea what I am doing wrong since terraform uses <code>ec2-user</code> as user and everything is copied into <code>/home/ec2-user/tmp</code></p>