【问题标题】:How can a process started using terraform user_data on an aws_instance continue to run beyond terraform apply finishing?在 aws_instance 上使用 terraform user_data 开始的进程如何在 terraform apply 完成之后继续运行?
【发布时间】:2021-03-12 11:06:23
【问题描述】:

我有一个 terraform 文件,它创建一个 aws_instance 并在该实例上调用一个进程 foo,该进程应该运行 10 分钟。这个过程模拟了一些我可以在其他地方监控的流量。我可以手动 ssh 到实例并运行该进程,它的行为符合预期。

问题是,一旦 terraform apply 完成所有设置后,进程似乎停止运行(这是我的假设,根据我何时停止看到流量并看到 terraform apply 完成来判断)。

如果我的假设是正确的,有没有办法以这样的方式开始这个过程,它会比 terraform 完成更长寿?

我的 terraform 文件像这样创建 aws_instance,其中 foo 之前已上传到另一个存储桶:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  user_data = <<-EOF
              #!/bin/bash
                aws s3 cp s3://foobar-bucket/foo ./
                chmod +x foo
                sudo ./foo
              EOF
  tags = {
    Name = "terraform-example"
  }
}

【问题讨论】:

    标签: amazon-web-services terraform


    【解决方案1】:
    resource "aws_instance" "example" {
      ami           = "ami-0c55b159cbfafe1f0"
      instance_type = "t2.micro"
      user_data = <<-EOF
                  #!/bin/bash
                    aws s3 cp s3://foobar-bucket/foo ./
                    chmod +x foo
                    sudo nohup ./foo & disown
                  EOF
      tags = {
        Name = "terraform-example"
      }
    }
    

    您可以找到&amp;、nohup 和disown here 之间的区别,或者根据您的需要使用其他组合。

    How can I start a remote service using Terraform provisioning?

    【讨论】:

      猜你喜欢
      • 2019-06-28
      • 2021-03-08
      • 2021-12-10
      • 2022-12-19
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 2020-04-16
      相关资源
      最近更新 更多