How to give a .tf file as input in Terraform Apply command?

You can use the terraform -target flag. Or You can have multiple terraform modules in a separate directory. And then you can terraform apply there. As an example, assume you have 3 .tf files separately. But you need to run more than just one of them at the same time. If you also, need to run them more often it's better to have a terraform module.

terraform
    |--frontend
    |  └──main.tf
    |--backend-1
    |  └──main.tf
    |--backend-2
    |  └──main.tf
    |--modules-1
    |  └──module.tf

Inside the module.tf you can define which files you need to apply.

module "frontend" {
  source = "terraform/frontend"
}

module "backend-1" {
  source = "terraform/backend-1"
} 

Then issue terraform apply staying at the module directory. And it will automatically import instances inside those paths and apply it.


You can't selectively apply one file and then the other. Two ways of (maybe) achieving what you're going for:

  • Use the -target flag to target resource(s) in one file and then the other.
  • Put each file (or more broadly, group of resources, which might be multiple files) in separate "modules" (folders). You can then apply them separately.

Tags:

Terraform