I spent a lot of hours to understand how things are segregating bet' workspace and folder based structure flow in terraform. After i pumped into here, got to know how simple it is. Heads up to you Will Brock. Simple and Sweet explanation. Awesome Job. Thanks a lot.
So glad someone addressed the initial confusion I had when I first was introduced to Workspace. Because I thought initially that by defining a workspace, it automatically deploys resources to different "profile" automatically on the cloud server I am working on. But that's not the case. It is more of a logical grouping with its own state file that helps manages different prod/non-prod environments GIVEN the right config.
This was a good explanation, thanks Curious, what if you define instance name in the dev.tfvars with a dev prefix. Same for PRD. Would you still need workspace? I guess to manage different .tfstate files?
Hi Will, What if, In Dev Workspace, I use -var-file prod.tfvars. this will apply the prod vars in Dev workspace. !! if that's the case, It all depends on varfiles, rather than Workspace. Then what is the use of differnt workspaces. - "is it maintaining different tfstate files for same config ? " please correct me if I am wrong.
Hi Will. Such an amazing teaching for terraform. Can you please make a video on how to use workspace for different account. Like sandbox and dev accounts in AWS>
Thanks Will for this wornderful video. But I have a question here apart from instance name which was picked based on workspace I couldn't understand what else could be the use case . If being in dev workspace you will pass prod.tfvars as argument the script will create the infra with prod configuration I beleive then what is new here ? can you pls clarify ?
So workspace allows you to have different state files for different environment . Each environment can have its own workspace hence its own state file.. right ?
Thank you, what is locals resource? I am not clear on that part. And one more doubt when run terraform apply with different variable files at that time only environments Got segregated and accordingly apply ?
Hey @Will Brock thanks for this video. Question for you - must you call the -var-file for each environment? Is there a way to add this elsewhere so it's not in the CLI. I'm trying to find use cases to use Terraform workspace over Terragrunt
i think the way Will Brock presented this is bound to fail.... someone will accidentally apply prod variables to dev environment. unless you set up a pipeline and no one actually runs terraform plan/apply on their terminal. in variables.tf you could do a map like variable "testing" { default = { dev = "abc" prod = "def"}} , then you call it like var.testing[terraform.workspace]. if you want values to come from tfvars you could probably use some other technique like an if statement
Awesome video ! Question- How can I further break down the project into infrastructure types ? Lets say under dev --> App(a folder), database(a folder). How can I maintain separate state for apps and database under dev ?
If you want separate state for databases and app you need to create two separate configurations not in the same workspace. Depending on your needs it might make sense to keep them both in the same state files.
Excellent demo and explanation. Thanks so much. I have a few questions: 1) After setting up the two separate workspaces, is there still a point for the terraform.tfvars 2) how would git keep track of the workspace?
For number 1 you can still have a terraform.tfvars depending on your needs. You would use git to keep track of the code only and not the state files. You could use git to track the state files but it's not recommend. Instead you should use a remote backend like S3 to keep track of the state.
but there is a problem! When you change workspace and run "terraform plan" the existing resource that was created previously will be marked as ... "1 to change" ...
Thanks for the awesome tutorials! Is it possible to have a workspace default to a .tfvars file? I find it easy to forget which workspace I'm in and apply the wrong .tfvars file. I have a dev, qa and prod.tfvars files and want to use them in my dev, qa and prod workspace respectively.
I am looking more like for multicloud setup where some things are spin up on own bare metal VM's, some dev envirionment on GCP and some prod env are provisioned on AWS. Or... demo env is set up on local VM's, but later transfered to the AWS. From the code organization perspective.