0

This is a generic workflow terraform question (I am a terraform beginner).

I need to create a new aws resource via terraform. The structure I have is

resource "some_aws_resource" "resource_name" {
   name = var.resource_name
   role_name = var.role_name

   another_key = another_value
}

The value another_value is derived from vars entered by the user through a bash script find_another_value.sh for example

resource "null_resource" "find_another_value" {
   provisioner "local-exec" {
      command     = "shell_scripts/find_another_value.sh"
    interpreter = ["/bin/bash"]
    working_dir = path.module

    environment = {
      DAR = var.aws_dar
      RAD = var.aws_rad
    }
   }

}

should return another_value

How do I pass the output another_value of the null_resource to the first resource and make sure that the non null resource waits until the null_resource returns the value?

The find_another_value.sh script will use the input variables to query aws via the aws cli to find the value it needs to return.

If there is a better/easier way that would also be good to know.

5
  • 2
    Why not use terraform variables instead of a shell script? Commented Oct 2, 2022 at 7:23
  • You can't do this. null_resource is not for returning anything to TF. I think you have to re-thing your design as Marko suggested. Commented Oct 2, 2022 at 10:46
  • @MarkoE I have to execute complicated logic in find_another_value.sh: retrieve some values using the aws cli. Commented Oct 2, 2022 at 22:30
  • What are you trying to retrieve, i.e., which values? Commented Oct 3, 2022 at 5:42
  • @MarkoE In one use case it is the organizational unit where I want to place a new account Commented Oct 3, 2022 at 14:39

1 Answer 1

1

For waiting you will use depends_on = []

resource "some_aws_resource" "resource_name" {
   depends_on =[null_resource.find_another_value]
   name = var.resource_name
   role_name = var.role_name
   another_key = another_value.value
}

To extract value from a resource We will write a resource out We will add value Edit the resource then the name and then the line we want its value

output "another_value" {

  description = "another_value from null resource find_another_value ."

  value = null_resource.find_another_value.provisioner "local-exec"
}
Sign up to request clarification or add additional context in comments.

1 Comment

The syntax is not correct for this line: value = null_resource.find_another_value.provisioner "local-exec" "local-exec" is not expected there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.