0
votes

I am fairly new to terraform. I am trying to create number of ec2 instances and want to run bootstrap script once instance is ready, and in bootstrap script I need to pass private ips of instance created ( part of terraform script). After so much of googling I came to know that I have to use terraform_file but, not able to use it correctly. Terraform version: 0.11.13

       /* Security group for the instances */
        resource "aws_security_group" "test-reporting-sg" 
        {
            name = "${var.environment}-test-reporting-sg"
            vpc_id      = "${var.vpc_id}"

        }
       data "template_file" "init"
        {
          count         = "${var.instance_count}"
          template = "${file("${path.module}/wrapperscript.sh")}"
          vars = {
                  ip_addresses= "${aws_instance.dev-testreporting.*.private_ip}"
                  }

        }
       resource "aws_instance" "dev-testreporting"
        {
             count = "${var.instance_count}"
             ami="${var.ami_id}"
             instance_type ="${var.instance_type}"
             key_name ="${var.key_name}"
             security_groups = [ "${aws_security_group.test-reporting-sg.id}" ]
             subnet_id       = "${var.subnet_ids}"

            user_data = "${data.template_file.init.*.rendered.[count.index]}"
        }

Thanks

1
You can use http://169.254.169.254/latest/dynamic/instance-identity/document to get the private IP per instance with the shell scripts at runtime ?error404
Are you trying to get a Consul cluster to join to itself so that multiple Consul instances are stood up and create a cluster with themselves? If so you should take a look at auto join hashicorp.com/blog/consul-auto-join-with-cloud-metadata instead of trying to pass the IP addresses in to static config.ydaetskcoR
@error404 I am writing one wrapperscript.sh where I 'll pass all the IPs, My end goal is to install vertica cluster (for this I need IPs of all the instances participating in the cluster).mt_leo
@ydaetskcoR Not sure about the consul. Haven't used it before. But the other part is yes Need to create vertica cluster, I was doing this work manually before, where I spin up instance(s) and pass their ips to wrapperscript.sh which interns call the other script where I have written all the logic to setup the cluster. But Now I want to pass the IPs of ec2 instances as soon as I spin up the instances.mt_leo
What was consul_address in your userdata template then? Can you post the templated script as well? Or create an minimal reproducible example with it in please? I'm assuming you want to create n instances and have the user data on those instances have the IP addresses of all n instances and not just their own?ydaetskcoR

1 Answers

0
votes

In resource module you can add private_ip like below

private_ip = "${lookup(var.private_ips,count.index)}"

and add private_ip values to variable file