title: remote-exec provisioner in terraform gives timeout
body: <p>I am trying to spin up an EC2 instance using an ubuntu image. I want to provide ssh access to the instance, install and run the apache server, and open up port 80, all through terraform.</p>
  <p>I have the following code for <code>remote-exec</code> for my EC2</p>
  <pre><code>provisioner &quot;remote-exec&quot; {
    inline = [
      &quot;sudo apt update -y&quot;,
      &quot;sudo apt upgrade -y&quot;,
      &quot;sudo apt install apache2 -y&quot;,
      &quot;sudo systemctl status apache2&quot;
    ]
  }
  </code></pre>
  <p>however, <strong>after all commands are executed successfully</strong> i.e. systemctl shows the correct response, my prompt hangs at aws_instance.web_server_instance: Still creating...</p>
  <p><a href="https://i.stack.imgur.com/tPBln.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tPBln.png" alt="console output" /></a></p>
  <p>my full code</p>
  <pre><code>terraform {
    required_version = &quot;~&gt; 1.1.5&quot;
  
    required_providers {
      aws = {
        source  = &quot;hashicorp/aws&quot;
        version = &quot;3.74.1&quot;
      }
  
      local = {
        source  = &quot;hashicorp/local&quot;
        version = &quot;2.1.0&quot;
      }
  
      tls = {
        source  = &quot;hashicorp/tls&quot;
        version = &quot;3.1.0&quot;
      }
    }
  }
  
  provider &quot;aws&quot; {
    profile = &quot;terraform&quot;
    region  = &quot;us-east-1&quot;
  }
  
  locals {
    application_name = &quot;web_server&quot;
  }
  
  
  
  # ssh key
  
  resource &quot;tls_private_key&quot; &quot;web_server_key_pair_gen&quot; {
    algorithm = &quot;RSA&quot;
  }
  
  resource &quot;local_file&quot; &quot;web_server_private_key&quot; {
    content  = tls_private_key.web_server_key_pair_gen.private_key_pem
    filename = &quot;${local.application_name}_private_key.pem&quot;
  }
  
  resource &quot;aws_key_pair&quot; &quot;web_server_public_key&quot; {
    key_name   = &quot;${local.application_name}_public_key&quot;
    public_key = tls_private_key.web_server_key_pair_gen.public_key_openssh
  }
  
  
  
  # security group
  
  resource &quot;aws_security_group&quot; &quot;web_server_security_group&quot; {
    name = &quot;${local.application_name}_security_group&quot;
  
    dynamic &quot;ingress&quot; {
      for_each = [
        { port = 22, description = &quot;ssh&quot; },
        { port = 80, description = &quot;http&quot; },
      ]
      content {
        description      = ingress.value.description
        from_port        = ingress.value.port
        to_port          = ingress.value.port
        protocol         = &quot;tcp&quot;
        cidr_blocks      = [&quot;0.0.0.0/0&quot;]
        ipv6_cidr_blocks = []
        prefix_list_ids  = []
        security_groups  = []
        self             = false
      }
    }
  
    egress {
      from_port   = 0
      to_port     = 0
      protocol    = &quot;-1&quot;
      cidr_blocks = [&quot;0.0.0.0/0&quot;]
    }
  
    tags = {
      Name = &quot;${local.application_name}_security_group&quot;
    }
  }
  
  
  
  # ami lookup
  
  data &quot;aws_ami&quot; &quot;ubuntu&quot; {
    most_recent = true
  
    filter {
      name   = &quot;name&quot;
      values = [&quot;ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*&quot;]
    }
  
    filter {
      name   = &quot;virtualization-type&quot;
      values = [&quot;hvm&quot;]
    }
  
    owners = [&quot;099720109477&quot;]
  }
  
  
  
  # ec2 instance
  
  resource &quot;aws_instance&quot; &quot;web_server_instance&quot; {
    ami                         = data.aws_ami.ubuntu.id
    instance_type               = &quot;t2.micro&quot;
    vpc_security_group_ids      = [aws_security_group.web_server_security_group.id]
    key_name                    = aws_key_pair.web_server_public_key.key_name
    associate_public_ip_address = true
  
    connection {
      user        = &quot;ubuntu&quot;
      private_key = tls_private_key.web_server_key_pair_gen.private_key_pem
      host        = self.public_ip
    }
  
    provisioner &quot;local-exec&quot; {
      command = &quot;chmod 600 ${local_file.web_server_private_key.filename}&quot;
    }
  
    provisioner &quot;remote-exec&quot; {
      inline = [
        &quot;sudo apt update -y&quot;,
        &quot;sudo apt upgrade -y&quot;,
        &quot;sudo apt install apache2 -y&quot;,
        &quot;sudo systemctl status apache2&quot;
      ]
    }
  
    tags = {
      Name = &quot;${local.application_name}_instance&quot;
    }
  }
  
  output &quot;server_public_ip&quot; {
    value = aws_instance.web_server_instance.public_ip
  }
  </code></pre>