我正在尝试使用terraform
旋转 2 个ec2
实例。像这样的东西
resource "aws_instance" "example" {
count = "${var.number_of_instances}"
ami = "${var.ami_name}"
associate_public_ip_address = "${var.associate_public_ip_address}"
instance_type = "${var.instance_type}"
key_name = "${var.keyname}"
subnet_id = "${element(var.subnet_ids, count.index)}"
user_data = "${element(data.template_file.example.*.rendered, count.index)}"
vpc_security_group_ids = ["${aws_security_group.example.id}","${var.extra_security_group_id}"]
root_block_device {
volume_size = "${var.root_volume_size}"
volume_type = "${var.root_volume_type}"
iops = "${var.root_volume_iops}"
}
tags {
Name = "${var.prefix}${var.name}${format("%02d", count.index + 1)}"
}
}
template_file
我要做的就是使用user_data
生成一个配置文件,其中包含两个实例的IP Address
,但这无法说Cycle Error
。
有没有办法在ec2
实例即将启动时IP Address
生成文件
利用用户数据脚本中的 AWS 实例元数据终端节点获取每个实例的 IP 地址并将其放入配置文件中。下面是用户数据脚本的 Powershell 示例:
<powershell>
$HostIp = (Invoke-RestMethod -URI 'http://169.254.169.254/latest/meta-data/local-ipv4' -UseBasicParsing)
Add-Content "C:installerconfig.txt" "HostIp:$HostIp"
</powershell>
如果需要,您还可以以这种方式获取实例的public-ipv4
。
通过组合各种信息,您可以使用resource "aws_launch_template"
块的user_data
参数来调用 shell,它本身可以调用一个典型的特殊元数据端点作为回报。对于私有 IP,它将是:curl http://169.254.169.254/latest/meta-data/local-ipv4