I have an EC2 instance with Jenkins, Ansible and Terraform installed.What i want to do is to create a Jenkins job which runs a Terraform script for creating a new EC2 instance and Ansible playbook for installing Tomcat in the new one. My problem is that when i run terraform apply i want to have in inventory file the new ip of the instance. How can i change that ip everytime i create a new instance?
Here it is a part of main.tf file:
resource "aws_instance" "my_vm" {
ami = var.ami //Linux AMI
instance_type = var.instance_type
key_name = "DevOpsKey_Licenta"
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.terra_tomcat_sg.id]
associate_public_ip_address = true
tags = {
Name = var.name_tag
}
provisioner "local-exec" {
command = "echo ${aws_instance.my_vm.public_ip} >> /opt/infrastructure-pipeline/inventory"
}
}
I tried to add everytime the new ip with that command but also i have the last one.
Here is inventory file:
[tomcat]
32.153.123.41
12.100.99.3