how to put in varable.tf to run script with data external in terraform

百般思念 提交于 2019-12-24 12:19:17

问题


This is the continutaion of the following contents i asked before.

I want to identify the public ip of the terraform execution environment and add it to the security group

My terraform direcotry structure is as follows.

root/
 ├ main.tf
 ├ test.sh
 ├ modules/aws
 │          └ ssh.tf/
 │          └ variables.tf/

test.sh

#!/bin/sh -xe

echo {\"ip\":\""`curl ipinfo.io/ip`"\"}

main.tf

module "aws" {

  source = "./modules/aws"

}

variables.tf

variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}

ssh.tf

resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0 
    to_port     = 0 
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = { 
    Name = "${var.name}",
  }
}

I load the .tf file under the module/aws directory using the module option in main.tf

and I would like to run my local script, identify the public IP that ran terraform and use the result in the ssh.tf file.

I'm using data external option to run my local script.

data "external" "example" {
  program = ["sh", "test.sh" ]
}

but, in data verification of myself, data external option is not executed properly except main.tf.

When running data external in main.tf and use it in ssh.tf, we got error following that such value does not exist.

output 'commandout': unknown resource 'data.external.example' referenced in variable data.external.example.result

My configuration file when I faced an error is as follows.

main.tf

data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {    
 source = "./modules/aws"    
}

ssh.tf

output "commandout" {
  value = "${data.external.example.result}"
}

resource "aws_security_group" "ssh" {
      name        = "${var.name}-ssh"
      description = "Allow connection by ssh"
      vpc_id      = "${aws_vpc.main.id}"

      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["${data.external.external.result}/32"]
      }

      egress {
        from_port   = 0 
        to_port     = 0 
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }

      tags = { 
        Name = "${var.name}",
      }
    }

is there a way to run most rapidly tf.file in terraform?

If there is such a way, i would like to use data external option to execute it first, and use it in ssh.tf

or I would like to put the result of data external execution into a certain value of var.tf

Please let me know if there are other ways to solve this problem. Thank you for reading my question.


回答1:


There are a few changes you need to make for this to work correctly. The test.sh script needs to only output json format as a result, but as it is right now (or when I was running it) I get output from the curl command as well. I had to wrap the curl command and ignore the output, and then use a variable to complete the json like so:

test.sh

#!/bin/sh

{
  IP_ADDRESS=$(curl ipinfo.io/ip)
} &> /dev/null

echo {\"ip\":\""$IP_ADDRESS"\"}

Now when you are calling your aws module in the main.tf we will want to set the ssh_ip_address variable to equal the "ip" output of the test.sh script result.
Also, I noticed that in your ssh.tf file you are trying to reference the "${data.external.example.result}" inside the aws module, but since that data is defined outside of the module it won't work. I suggest creating an addition variable of type "map" in the variables.tf which you can then set in the main.tf; in my example I have added the "command_output" variable.

Here you can see I added the command_output variable:

variable.tf

variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}

variable "command_output" {
  type = "map"
}

variable name {
  default = "test"
}

Here is what the main.tf not looks like (notice that we are setting the variables defined in the aws module):

main.tf

data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {
  source = "./modules/aws"

  ssh_ip_address = "${data.external.example.result.ip}"

  command_output = "${data.external.example.result}"
}

Lastly, here is the ssh.tf file:

ssh.tf

resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.name}",
  }
}

output "commandout" {
  value = "${var.command_output}"
}

Hope this helps!



来源:https://stackoverflow.com/questions/47311916/how-to-put-in-varable-tf-to-run-script-with-data-external-in-terraform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!