0

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

Marko E
  • 13,362
  • 2
  • 19
  • 28
mt_leo
  • 67
  • 1
  • 12
  • 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 May 11 '20 at 13:45
  • 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 https://www.hashicorp.com/blog/consul-auto-join-with-cloud-metadata/ instead of trying to pass the IP addresses in to static config. – ydaetskcoR May 11 '20 at 13:51
  • @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 May 11 '20 at 14:12
  • @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 May 11 '20 at 14:18
  • What was `consul_address` in your userdata template then? Can you post the templated script as well? Or create an [mcve] 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 May 11 '20 at 15:27
  • @ydaetskcoR below is sample of my template file. If I am able to pass the ip addresses here I can call my bootstrap script. #!/bin/bash echo "ip address" ${ip_addresses} echo "done " >> /tmp/bootstrap.log – mt_leo May 11 '20 at 16:14
  • @ydaetskcoR : Updated the template file variable, sorry I confused you with the variable name "consul_address" have copied it from one of the link. – mt_leo May 11 '20 at 16:16
  • Similar solution worked as per this link. https://stackoverflow.com/questions/50835636/accessing-terraform-variables-within-user-data-provider-template-file – mt_leo May 11 '20 at 16:17

1 Answers1

0

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

JMP
  • 4,417
  • 17
  • 30
  • 41
Akshay
  • 11
  • 1