Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define execution order without trigger option in terraform

Tags:

terraform

Does it make sense to understand that it runs in the order defined in main.tf of terraform?

I understand that it is necessary to describe the trigger option in order to define the order on terraform.

but if it could not be used trigger option like this data "external" , How can I define the execution order?

For example, I would like to run in order as follows.

get_my_public_ip -> ec2 -> db -> test_http_status

main.tf is as follows

data "external" "get_my_public_ip" {
  program = ["sh", "scripts/get_my_public_ip.sh"]
}

module "ec2" {
  ...
}

module "db" {
  ...
}


data "external" "test_http_status" {
  program = ["sh", "scripts/test_http_status.sh"]
}
like image 778
negabaro Avatar asked Apr 04 '18 01:04

negabaro


People also ask

What is the command to show the execution plan and not apply in Terraform?

The terraform plan command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure.

Does order matter in Terraform?

The exact names and order aren't important to Terraform. The order of variables and resources defined within the configuration doesn't matter. Terraform configurations are declarative, so references to other resources and variables do not depend on the order they're defined.

How the execution of Terraform file works steps?

The core Terraform workflow has three steps: Write - Author infrastructure as code. Plan - Preview changes before applying. Apply - Provision reproducible infrastructure.

How do you specify a dependency in Terraform?

You can use depends_on to explicitly declare the dependency. You can also specify multiple resources in the depends_on argument, and Terraform will wait until all of them have been created before creating the target resource.


1 Answers

I can only provide feedback on the code you provided but one way to ensure the test_status command is run once the DB is ready is to use a depends_on within a null_resource

resource "null_resource" "test_status" {
  depends_on = ["module.db.id"] #or any output variable
  provisioner "local-exec" {
    command = "scripts/test_http_status.sh"
  }
}

But as @JimB already mentioned terraform isn't procedural so ensuring order isn't possible.

like image 55
Stephen Avatar answered Oct 29 '22 21:10

Stephen