Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Terraform remote exec运行简单web服务器_Python 3.x_Terraform_Terraform Provider Aws - Fatal编程技术网

Python 3.x 使用Terraform remote exec运行简单web服务器

Python 3.x 使用Terraform remote exec运行简单web服务器,python-3.x,terraform,terraform-provider-aws,Python 3.x,Terraform,Terraform Provider Aws,ami-0d44833027c1a3297是一款ubuntu ami,在/home/ubuntu/ 虽然我没有收到任何错误,并且ec2实例已经启动并运行,但http服务器没有运行 注意:如果我手动ssh并在内联部分应用相同的命令集,我就能够成功运行http服务器。任何帮助都将不胜感激 修复: # example.tf provider "aws" { region = "us-east-1" } resource "aws_instance" "example" { ami

ami-0d44833027c1a3297
是一款ubuntu ami,在
/home/ubuntu/

虽然我没有收到任何错误,并且ec2实例已经启动并运行,但http服务器没有运行

注意:如果我手动ssh并在内联部分应用相同的命令集,我就能够成功运行http服务器。任何帮助都将不胜感激

修复:

# example.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  ami             = "ami-0d44833027c1a3297"
  instance_type   = "t2.micro"
  security_groups = ["${aws_security_group.example.name}"]
  key_name        = "${aws_key_pair.generated_key.key_name}"

  provisioner "remote-exec" {
    inline = [
      "cd /home/ubuntu/",
      "nohup python3 -m http.server 8080 &",
    ]

    connection {
      type        = "ssh"
      private_key = "${tls_private_key.example.private_key_pem}"
      user        = "ubuntu"
      timeout     = "1m"
    }
  }
}

resource "tls_private_key" "example" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "aws_key_pair" "generated_key" {
  key_name   = "example_key_pair"
  public_key = "${tls_private_key.example.public_key_openssh}"
}

resource "aws_security_group" "example" {
  name        = "grant ssh"
  description = "grant ssh"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 8080
    to_port     = 8080
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
我相信provisioner在服务器进程开始之前退出(竞争条件),因此睡眠是必要的


注意:从Hashicorp Packer documentation()中获得了睡眠的概念。

在我的例子中,睡眠60很有帮助。
inline = [
      ....
      "sleep 20",
    ]