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?
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.
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.
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? 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 .
You can't selectively apply one file and then the other. Two ways of (maybe) achieving what you're going for:
-target
flag to target resource(s) in one file and then the other.apply
them separately.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With