有没有办法使用 Terraform for EC2 确认 user_data 成功运行? [英] Is there a way to confirm user_data ran successfully with Terraform for EC2?

查看:27
本文介绍了有没有办法使用 Terraform for EC2 确认 user_data 成功运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能知道用户数据中的脚本何时完全执行?

I'm wondering if it's possible to know when the script in user data executes completely?

data "template_file" "script" {
  template = file("${path.module}/installing.sh")
}

data "template_cloudinit_config" "config" {
  gzip          = false
  base64_encode = false

  # Main cloud-config configuration file.
  part {
    filename     = "install.sh"
    content      = "${data.template_file.script.rendered}"
  }
}

resource "aws_instance" "web" {
  ami           = "ami-04e7b4117bb0488e4"
  instance_type = "t2.micro"
  key_name = "KEY"
  vpc_security_group_ids = [aws_default_security_group.default.id]
  subnet_id = aws_default_subnet.default_az1.id
  associate_public_ip_address = true
  iam_instance_profile = "Role_S3"
  user_data = data.template_cloudinit_config.config.rendered
  tags = {
    Name = "Terraform-Ansible"
  }
}

在脚本的内容中我有这个.它告诉我 Terraform 已成功应用更改,但脚本仍在运行,有什么方法可以监控吗?

And in the content of the script I have this. It tells me Terraform successfully apply the changes, but the script is still running, is there a way I can monitor that?

#!/usr/bin/env bash
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
echo BEGIN
sudo apt update
sudo apt upgrade -y
sudo apt install -y unzip
echo END

推荐答案

不,您无法从 terraform 确认用户数据状态,因为它会发布启动脚本,该脚本会在 EC2 实例启动后执行.但是您需要在 init 脚本上做一些额外的工作才能进行检查.

No, You can not confirm the user data status from the terraform, as it posts launching script that executes once EC2 instance launched. But you will need some extra effort on init script that one way to check.

如何检查用户数据在 aws 中启动实例时的状态

如果您在完成用户数据后执行上述操作来制作一些标记文件,那么您可以尝试进行检查.

If you do something that is mentioned above to make some marker file once user data completed, then you can try this to check.

resource "null_resource" "user_data_status_check" {

  provisioner "local-exec" {
    on_failure  = "fail"
    interpreter = ["/bin/bash", "-c"]
    command     = <<EOT
          echo -e "x1B[31m wait for few minute for instance warm up, adjust accordingly x1B[0m"
          # wait 30 sec 
          sleep 30
          ssh -i yourkey.pem instance_ip ConnectTimeout=30  -o 'ConnectionAttempts 5' test -f "/home/user/markerfile.txt" && echo found || echo not found
          if [ $? -eq 0 ]; then
          echo "user data sucessfully executed"
          else
            echo "Failed to execute user data"
          fi
     EOT
  }
    triggers = {
    #remove this once you test it out as it should run only once
    always_run ="${timestamp()}"

  }
  depends_on = ["aws_instance.my_instance"]
  
}

因此,此脚本将通过执行 ssh 超时 30 秒,最大尝试次数为 5 来检查新启动服务器上的 标记文件.

so this script will check marker file on the newly launch server by doing ssh with timeout 30 seconds with max attempts 5.

这篇关于有没有办法使用 Terraform for EC2 确认 user_data 成功运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆