我正在使用 Terraform v0.11.7 和 AWS 提供商 1.30 构建一个环境,以使用基于 Debian 9.5 AMI 构建的locust运行负载测试。
我的模块公开了一个num_instances
var 用于确定所使用的 locust 命令行。下面是我的配置。
resource "aws_instance" "locust_master" {
count = 1
ami = "${var.instance_ami}"
instance_type = "${var.instance_type}"
key_name = "${var.instance_ssh_key}"
subnet_id = "${var.subnet}"
tags = "${local.tags}"
vpc_security_group_ids = ["${local.vpc_security_group_ids}"]
user_data = <<-EOF
#!/bin/bash
# Install pip on instance.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python3 get-pip.py
rm get-pip.py
# Install locust and pyzmq on instance.
sudo pip3 install locustio pyzmq
# Write locustfile to instance.
echo "${data.local_file.locustfile.content}" > ${local.locustfile_py}
# Write locust start script to instance.
echo "nohup ${var.num_instances > 1 ? local.locust_master_cmd : local.locust_base_cmd} &" > ${local.start_sh}
# Start locust.
sh ${local.start_sh}
EOF
}
resource "aws_instance" "locust_slave" {
count = "${var.num_instances - 1}"
ami = "${var.instance_ami}"
instance_type = "${var.instance_type}"
key_name = "${var.instance_ssh_key}"
subnet_id = "${var.subnet}"
tags = "${local.tags}"
vpc_security_group_ids = ["${local.vpc_security_group_ids}"]
user_data = <<-EOF
#!/bin/bash
set -x
# Install pip on instance.
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python3 get-pip.py
rm get-pip.py
# Install locust and pyzmq on instance.
sudo pip3 install locustio pyzmq
# Write locustfile to instance.
echo "${data.local_file.locustfile.content}" > ${local.locustfile_py}
# Write locust master dns name to instance.
echo ${aws_instance.locust_master.private_dns} > ${local.locust_master_host_file}
# Write locust start script to instance.
echo "nohup ${local.locust_slave_cmd} &" > ${local.start_sh}
# Start locust.
sh ${local.start_sh}
EOF
}
locust_master
如果我在实例启动后通过SSH 连接到实例,我会看到/home/admin/start.sh
脚本,但它似乎没有运行,因为我没有看到该nohup.out
文件并且locust
不在我正在运行的进程中。如果我在该主机上手动运行相同的sh /home/admin/start.sh
脚本,服务就会启动,我可以断开与主机的连接并仍然可以访问它。同样的问题也出现在locust_slave
主机上。
什么可能导致start.sh
在 aws_instance user_data 中运行失败?在 user_data 中执行脚本时,我应该注意哪些问题?
提前谢谢了!