【问题标题】:Run simple web server with Terraform remote-exec使用 Terraform remote-exec 运行简单的 Web 服务器
【发布时间】:2019-01-17 13:24:03
【问题描述】:
# 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"]
  }
}

ami-0d44833027c1a3297 是一个带有 hello world index.html 的 ubuntu AMI,位于 /home/ubuntu/

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

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

【问题讨论】:

    标签: python-3.x terraform terraform-provider-aws


    【解决方案1】:

    修复:

    inline = [
          ....
          "sleep 20",
        ]
    

    我相信供应商在服务器进程开始之前退出(竞争条件),因此睡眠是必要的。

    注意:从 Hashicorp Packer 文档中获得睡眠的想法(https://www.packer.io/docs/provisioners/shell.html)

    【讨论】:

    • 就我而言,sleep 60 很有帮助。
    猜你喜欢
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2015-06-01
    • 2018-06-26
    相关资源
    最近更新 更多