How to Get IP Address of EC2 Instance in a File Created by Terraform

In this post we will store the public IP address of EC2 instance in a file. Let me explain my usecase first. I will create a ec2 instance using Terraform using Jenkins pipeline then I will configure some environment on this newly created instance using Ansible. To use ansible I need public ip address of this instance into the inventory file of Ansible
The Entire flow will be -
1. Jenkins will trigger the terraform 
2. Terraform will create a instance on AWS. 
3. Ansible fetch the instance details and it will perform configuration management. 

 The terraform module look like 

 resource "aws_instance" "devsecops" 
 ami                                   = var.instance_ami 
 instance_type                   = var.instance_type 
 subnet_id                         = var.subnet_id 
 vpc_security_group_ids  = var.vpc_security_group_ids
 key_name                        = var.key_name 

 # here i am using provisioner local-exec which will filter the ip address from the output of above module and will store in the ip.txt 

 provisioner "local-exec" 
 command = "echo ip: ${aws_instance.devsecops.public_ip} >> public_ip.txt" 
   }
 }

Post a Comment

0 Comments