# TCP SSH timeout while running remote\_exec on AWS instance

**URL:** <https://discuss.hashicorp.com/t/tcp-ssh-timeout-while-running-remote-exec-on-aws-instance/21234>\
**Category:** Terraform\
**Created:** [February 22, 2021, 12:11am UTC](https://discuss.hashicorp.com/t/tcp-ssh-timeout-while-running-remote-exec-on-aws-instance/21234 "2021-02-22T00:11:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sd999111](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/sd999111/32/17447_2.png) [@sd999111](https://discuss.hashicorp.com/u/sd999111)\
**Post date:** [February 22, 2021, 12:11am UTC](https://discuss.hashicorp.com/t/tcp-ssh-timeout-while-running-remote-exec-on-aws-instance/21234/1 "2021-02-22T00:11:18Z")

</div>

### Terraform Version

```auto
hashicorp/terraform:0.12.23

```

### Terraform Configuration Files

```auto
resource "aws_instance" "jellyfin_server" {
  key_name = aws_key_pair.jellyfin_keys.key_name
  ami = data.aws_ami.amazon_linux_2.id
  instance_type = var.EC2_INSTANCE_TYPE
  iam_instance_profile = aws_iam_instance_profile.jellyfin_instance_profile.name
  associate_public_ip_address = true
  root_block_device {
    volume_size = var.EBS_ROOT_VOLUME_SIZE
  }
  ebs_block_device {
    device_name = "/dev/sdf"
    volume_size = var.EBS_MEDIA_VOLUME_SIZE
    volume_type = var.EBS_MEDIA_VOLUME_TYPE
  }

  vpc_security_group_ids = [aws_security_group.jellyfin_server_sg.id]
  subnet_id = aws_subnet.jellyfin_a.id
  connection {
    type = "ssh"
    user = "ec2-user"
    private_key = file(".ssh/jellyfin-key")
    host = self.public_ip
  }

  provisioner "remote-exec" {
    inline = [
      "mkdir ~/jellyfin",
      "mkdir ~/jellyfin/scripts",
      "mkdir ~/jellyfin/media",
      "mkdir ~/jellyfin/cache",
      "mkdir ~/jellyfin/config",
      "sudo amazon-linux-extras enable nginx1",
      "sudo yum install -y docker nginx dos2unix",
      "sudo usermod -a -G docker ec2-user",
      "sudo systemctl enable docker",
      "sudo systemctl enable nginx"
    ]
  }

  provisioner "remote-exec" {
    inline = [
      "EBS_DEVICE_NAME=$(lsblk | grep ${var.EBS_MEDIA_VOLUME_SIZE}G | awk '{print $1}')",
      "sudo mkfs -t xfs /dev/$${EBS_DEVICE_NAME}",
      "EBS_DEVICE_UUID=$(sudo blkid | grep $${EBS_DEVICE_NAME} | awk -F'\"' '{print $2}')",
      "sudo echo -e \"UUID=$${EBS_DEVICE_UUID} /home/ec2-user/jellyfin/media xfs defaults,nofail 0 2\" | sudo tee -a /etc/fstab",
      "sudo mount -a",
      "sudo chown -R ec2-user /home/ec2-user/jellyfin/media"
    ]
  }

  // Creates nginx conf file for jellyfin server based on ALB or Domain name
  provisioner "file" {
    content = templatefile("templates/server.conf.tmpl", { server_name = local.server_name })
    destination = "/tmp/instant-jellyfin.conf"
  }

  // Lays down cron for syncing files form s3 bucket to local storage
  provisioner "file" {
    content = file("files/start-sync.sh") 
    destination = "/tmp/start-s3sync.sh"
  }

  // Creates cron job script with bucket variable
  provisioner "file" {
    content = templatefile("templates/s3sync.sh.tmpl", { BUCKET = aws_s3_bucket.jellyfin_media.id })
    destination = "/tmp/s3sync.sh"
  }

  // Lays down docker script
  provisioner "file" {
    content = file("files/start-jellyfin.sh")
    destination = "/tmp/start-jellyfin.sh"
  }

  // Starts Nginx with jellyfin conf
  provisioner "file" {
    content = file("files/start-nginx.sh")
    destination = "/tmp/start-nginx.sh"
  }

  // Starts jellyfin backups to S3
  provisioner "file" {
    content = file("files/start-backup.sh")
    destination = "/tmp/start-backup.sh"
  }

  // Creates backup script for jellyfin config
  provisioner "file" {
    content = templatefile("templates/backup.sh.tmpl", { BUCKET = aws_s3_bucket.jellyfin_backup.id })
    destination = "/tmp/backup.sh"
  }

  // Creates restore script for jellyfin config
  provisioner "file" {
    content = templatefile("templates/restore.sh.tmpl", { BUCKET = aws_s3_bucket.jellyfin_backup.id })
    destination = "/tmp/restore.sh"
  }

   provisioner "remote-exec" {
    inline = [
      "sudo mv /tmp/instant-jellyfin.conf /etc/nginx/conf.d/instant-jellyfin.conf",
      "mv /tmp/start-s3sync.sh ~/jellyfin/scripts/start-s3sync.sh",
      "mv /tmp/s3sync.sh ~/jellyfin/scripts/s3sync.sh",
      "mv /tmp/start-jellyfin.sh ~/jellyfin/scripts/start-jellyfin.sh",
      "mv /tmp/start-nginx.sh ~/jellyfin/scripts/start-nginx.sh",
      "mv /tmp/start-backup.sh ~/jellyfin/scripts/start-backup.sh",
      "mv /tmp/backup.sh ~/jellyfin/scripts/backup.sh",
      "mv /tmp/restore.sh ~/jellyfin/scripts/restore.sh",
      "sudo dos2unix /etc/nginx/conf.d/instant-jellyfin.conf",
      "sudo sed -i '/^[]*http[]*{.*/a server_names_hash_bucket_size 128;' /etc/nginx/nginx.conf",
      "dos2unix ~/jellyfin/scripts/start-s3sync.sh",
      "dos2unix ~/jellyfin/scripts/s3sync.sh",
      "dos2unix ~/jellyfin/scripts/start-jellyfin.sh",
      "dos2unix ~/jellyfin/scripts/start-nginx.sh",
      "dos2unix ~/jellyfin/scripts/start-backup.sh",
      "dos2unix ~/jellyfin/scripts/backup.sh",
      "dos2unix ~/jellyfin/scripts/restore.sh",
      "/bin/bash ~/jellyfin/scripts/start-s3sync.sh",
      "/bin/bash ~/jellyfin/scripts/start-jellyfin.sh",
      "/bin/bash ~/jellyfin/scripts/start-nginx.sh",
      "/bin/bash ~/jellyfin/scripts/start-backup.sh"
    ]
  } 

  tags = {
    Name = "${var.ENVIRONMENT}-jellyfin-server"
    Environment = var.ENVIRONMENT
  }
}

```

### Debug Output

### Crash Output

Error: timeout - last error: dial tcp 54.191.141.95:22: i/o timeout

### Expected Behavior

Should create files on the newly created aws instance

### Actual Behavior

terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): Connecting to remote host via SSH…  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): Host: 54.191.141.95  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): User: ec2-user  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): Password: false  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): Private key: true  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): Certificate: false  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): SSH Agent: false  
terraform\_1 | aws\_instance.jellyfin\_server (remote-exec): Checking Host Key: false  
terraform\_1 | aws\_instance.jellyfin\_server: Still creating… [5m20s elapsed]  
terraform\_1 | aws\_instance.jellyfin\_server: Still creating… [5m30s elapsed]  
terraform\_1 |  
terraform\_1 |  
terraform\_1 | Error: timeout - last error: dial tcp 54.191.141.95:22: i/o timeout  
terraform\_1 |  
terraform\_1 |  
instant-jellyfin\_terraform\_1 exited with code 1

### Steps to Reproduce

### Additional Context

### References

---

<div class="post-metadata">

**Author:** ![qwerty1979bg](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/qwerty1979bg/32/7227_2.png) [@qwerty1979bg](https://discuss.hashicorp.com/u/qwerty1979bg)\
**Post date:** [February 22, 2021, 7:53am UTC](https://discuss.hashicorp.com/t/tcp-ssh-timeout-while-running-remote-exec-on-aws-instance/21234/2 "2021-02-22T07:53:41Z")

</div>

Hi @sd999111 ,

Just wanted to clarify that this is not a “crash”, but just an error that Terraform is reporting to you.

It means that Terraform was unable to connect via SSH to the host (in this case 54.192.141.95 at port 22).

This can happen for various reasons, for example:

- The server is not listening to port 22
- A firewall is blocking access to the port

Please check if the AWS security group is allowing access to the aws\_instance from the IP where you are running Terraform.

Additionally, maybe it takes time for the AWS instance to start serving requests and you need to introduce some wait before you start your provisioning steps.

Also, keep in mind that [“provisioners are a last resort”](https://www.terraform.io/docs/language/resources/provisioners/syntax.html#provisioners-are-a-last-resort) and consider using other options:

- [cloud-init](https://learn.hashicorp.com/tutorials/terraform/cloud-init) via the [user-data](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#user_data) attribute of the aws\_instance resource
- using HashiCorp’s [Packer](https://learn.hashicorp.com/tutorials/terraform/packer?in=terraform/provision) to create an AWS AMI that has all of your required software / applications already installed

Hope this helps you with your task.

Regards,  
Filip

---

<div class="post-metadata">

**Author:** ![d.bayliss](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/d.bayliss/32/17088_2.png) [@d.bayliss](https://discuss.hashicorp.com/u/d.bayliss)\
**Post date:** [February 23, 2021, 9:12pm UTC](https://discuss.hashicorp.com/t/tcp-ssh-timeout-while-running-remote-exec-on-aws-instance/21234/3 "2021-02-23T21:12:25Z")

</div>

Make sure you can putty (using your ppk cert)  
and add this to your connection,  
timeout = “4m”  
You’ll know through putty that it takes a few mins for the instance to allow connections

---

<div class="post-metadata">

**Author:** ![jurusbangsat](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jurusbangsat/32/45934_2.png) [@jurusbangsat](https://discuss.hashicorp.com/u/jurusbangsat)\
**Post date:** [June 20, 2025, 7:29pm UTC](https://discuss.hashicorp.com/t/tcp-ssh-timeout-while-running-remote-exec-on-aws-instance/21234/4 "2025-06-20T19:29:19Z")

</div>

Does this work? I have the same problem.
