Terraform - Define variables using the -var command line option
by
Jeremy Canfield |
Updated: July 27 2023
| Terraform articles
Variables can be defined using the -var command line option. Let's say you have the following files on your Terraform server.
├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── child (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
And let's say provider.tf contains the Amazon Web Services (AWS) provider with two variables, var.profile and var.region.
~]$ cat provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
profile = var.profile
region = var.region
}
When initializing the provider using the terraform init command, here is how you could define the profile and region variables on the command line.
terraform init -var="profile=my-profile" -var="region=us-east-1"
Did you find this article helpful?
If so, consider buying me a coffee over at