aws - instance
resource "aws_instance" "ec2_node" {
depends_on = [local_file.efs_ip]
count = length(var.nodes)
ami = data.aws_ami.latest_amazon_linux_2.id
instance_type = var.instance_types[count.index]
monitoring = true
key_name = aws_key_pair.key_generation.key_name
subnet_id = aws_subnet.subnet_a.id
availability_zone = var.name_az
private_ip = "${var.ip_az}${var.nodes[count.index]}"
vpc_security_group_ids = [aws_security_group.sg.id]
associate_public_ip_address = true
iam_instance_profile = aws_iam_instance_profile.iam_ec2_profile.name
root_block_device {
volume_size = "${var.instance_root_block_size}"
volume_type = "gp2"
}
ebs_block_device {
device_name = "/dev/xvdf"
volume_size = 30
volume_type = "gp2"
tags = {
Name = "${var.cluster}-ebs-node_${var.nodes[count.index]}"
}
}
# executar codigo na instancia ec2
connection {
type = "ssh"
user = "ec2-user"
private_key = file("${var.cluster}-key.pem")
host = self.public_ip
}
provisioner "remote-exec" {
inline = [
"echo 'SSH online'",
]
}
# executar codigo local
provisioner "local-exec" {
# Script local a ser executado após a criação da instância EC2
command = "python3 helloworld.py ${self.private_ip}"
}
# executar codigo local. ansible
provisioner "local-exec" {
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i '${self.public_ip},' --private-key chave-key.pem -e 'IP=${self.public_ip} IP_PRIVATE=${self.private_ip} ' ping.yml"
}
tags = {
Name = "${var.cluster}-node_${var.nodes[count.index]}"
}
}