Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define Module Dependencies in Terraform

How can we ensure that the first module “frontend_1” is executed before the rest?

module "frontend_1" {
  source = "/modules/frontend-app"
}
module "frontend_2" {
  source = "/modules/frontend-app"
}
module "frontend_3" {
  source = "/modules/frontend-app"
}

Also - there're a couple of recorded Gitissues re this problem but a good workaround has not been presented in any of them. As a start - you can check this one so that you can get familiar with the essence of my question - https://github.com/hashicorp/terraform/issues/10462

And how to make an external for a module resource be built before the module gets executed - so that the "count" can be computed if it depends on a value which is calculated from such an external resource? For example - if you need to use the ID of a newly created VPC in the "count" in a module which creates multiple AWS Security Groups within that VPC?

like image 335
Xtigyro Avatar asked Aug 04 '18 11:08

Xtigyro


People also ask

How would you define module dependency in Terraform?

Unlike resources, there is no built-in mechanism to create module dependencies as of Terraform 0.12. This means it is not possible to create an entire module only after another module has been created.

What are module dependencies?

Module dependencies are classes, archives, libraries and resources that your module files references. While a library is a set of class files stored in an archive or directory. Export check means if checked then this library will be implicitly added to the other module that references this one.

How do you define a Terraform module?

A Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more . tf files is a module. So in this sense, every Terraform configuration is part of a module.

How do you reference a module in Terraform?

Modules on the public Terraform Registry can be referenced using a registry source address of the form <NAMESPACE>/<NAME>/<PROVIDER> , with each module's information page on the registry site including the exact address to use.


1 Answers

As mentioned in the question, I was able to make a workaround for you using the depends on variable.

Refer to this for Terraform dependencies.

Assume we have 2 modules, one which defines the vpc and subnets, the second to define the various range of security groups to be used in the Infrastructure.

Since we have the dependency such that all security groups are to be made only after the successful creation of vpc in the VPC module, it can be met by the following strategy.

variable "vpc_arn" {
   description = "The ARN of the VPC which is created in the VPC module"
}

resource "null_resource" "vpc_found" {
  triggers = {
    vpc_name = "${var.vpc_arn}"
  }
}

resource "aws_security_group" "allow_all" {

  depends_on = ["null_resource.vpc_found"]

  name        = "allow_all"
  description = "Allow all inbound traffic"
  vpc_id      = "${var.vpc_arn}"
  ......
}

Refer to this for null resources.

like image 90
Kishor U Avatar answered Sep 21 '22 14:09

Kishor U