Is there a way to run apply on multiple modules at once?

Is there a way to run terraform apply on multiple modules at once, that is, to all subdirectories?

For example, if you have a structure like this, can you apply all main.tf files at once? That is, without a third party tool?

root
├── backend-app
│   └── main.tf
├── frontend-app
│   └── main.tf
├── mysql
│   └── main.tf
├── redis
│   └── main.tf
└── vpc
    └── main.tf

terraform apply works with only one configuration at a time, so if you want to apply all of these modules together then you’ll need to write a wrapper module that calls all of them and then apply that module as your root module.

module "backend_app" {
  source = "./backend-app"

  # ...
}

module "frontend_app" {
  source = "./frontend-app"

  # ...
}

module "mysql" {
  source = "./mysql"

  # ...
}

module "redis" {
  source = "./redis"

  # ...
}

module "vpc" {
  source = "./vpc"

  # ...
}

I wrote the paths in the example above as starting with ./, which means that the file containing those module blocks would need to be placed in the directory you labelled “root” in your example directory tree.

Then you’d run terraform init and terraform apply in that root directory instead of each of the subdirectories separately. Terraform will consider it all to be a single configuration and so will plan and apply everything together.

If you already have state with existing remote objects in each of your subdirectories then you will need to manually migrate to a new single state associated with your root directory if you adopt this approach. There is no automatic way to achieve that refactoring because Terraform CLI can only work in one directory with one state at a time.

1 Like

Thank you for the information and quick response