Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm a beginner in Terraform.

I have a directory which contains 2 .tf files.

Now I want to run Terraform Apply on a selected .tf file & neglect the other one.

Can I do that? If yes, how? If no, why & what is the best practice?

like image 578
Ajinkya Bapat Avatar asked Dec 08 '17 05:12

Ajinkya Bapat


People also ask

How do I add a variable file to Terraform?

Set the instance name with a variable Add a variable to define the instance name. Create a new file called variables.tf with a block defining a new instance_name variable. Note: Terraform loads all files in the current directory ending in . tf , so you can name your configuration files however you choose.

How do you pass a variable in Terraform command line?

Using the -var Command-line Flag The -var flag is used to pass values for Input Variables to Terraform commands. This flag can be used with both of the Terraform plan and apply commands. The argument passed to the -var flag is a string surrounded by either single or double quotes.

How can Terraform input variables be defined?

Additionally, input variable values can also be set using Terraform environment variables. To do so, simply set the environment variable in the format TF_VAR_<variable name> . The variable name part of the format is the same as the variables declared in the variables.tf file.

What is Terraform apply command?

What is Terraform Apply? The terraform apply command executes the actions proposed in a terraform plan . It is used to deploy your infrastructure. Typically apply should be run after terraform init and terraform plan .


2 Answers

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.
like image 200
Aidan Feldman Avatar answered Sep 20 '22 06:09

Aidan Feldman


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.

like image 38
panther93 Avatar answered Sep 20 '22 06:09

panther93