You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Terraform上传Python程序至EC2遇SSH超时错误求助

Terraform部署Hello World到AWS EC2时遭遇SSH连接超时错误

错误详情

│ Error: file provisioner error
│ 
│   with aws_instance.ec2_instance,
│   on ec2.tf line 13, in resource "aws_instance" "ec2_instance":
│   13:     provisioner "file" {
│ 
│ timeout - last error: dial tcp 3.88.197.68:22: i/o timeout

我的Terraform配置

EC2实例资源配置

resource "aws_instance" "ec2_instance" {
  ami           = "ami-0889a44b331db0194"
  instance_type = "t2.micro"

  root_block_device {
    volume_size = var.ebs_volume_size
  }
    connection {
    type        = "ssh"
    user        = "ec2-user"  # 替换为你需要的SSH用户名
    host        = self.public_ip
  }
    provisioner "file" {
    source      = "hello_world.py"
    destination = "/home/ec2-user/hello_world.py"
  }
    provisioner "remote-exec" {
    inline = [
      "python /home/ec2-user/hello_world.py"
    ]
  }
}

变量定义

variable "instance_type" {
  description = "EC2实例类型"
  type        = string
  default     = "t2.micro"  # 替换为你需要的默认实例类型
}

variable "ebs_volume_size" {
  description = "EBS卷大小"
  type        = number
  default     = 10  # 替换为你需要的默认卷大小
}

Provider配置

provider "aws" {
  region     = "us-east-1"  # 更新为你需要的区域
}

已尝试的解决措施

  • 修改file provisioner的source路径为绝对路径:/Users/Ashish/Documents/Terraform-on-aws-ec2/EC2-Terraform/hello_world.py
  • 修改source路径为相对路径:Documents/Terraform-on-aws-ec2/EC2-Terraform/hello_world.py
  • 使用当前目录下的文件名:hello_world.py

以上方法均未解决问题,请问有人遇到过类似情况吗?我也参考过本地目录复制到EC2的相关内容,但问题依旧。

内容的提问来源于stack exchange,提问作者user9238493490

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 02:15:12