Workspaces | Terraform Tutorial | #16

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how do you folks today in this video we're gonna go over workspaces and terraform so as we've talked about before the way terraform manages all the infrastructure is with state and state files so by default you always just have one basic set of state but with workspaces you can actually have multiple sets of state so you can have like this same configuration file but you can have multiple sets of state it's meaning you can just you can just take the same file and have one server set up for it which is stored in one set of state files and you can also have another server set up or infrastructure set up with a different set of state files so in a good example might be you know you might have a dev environment and a prod environment you know those those resources might be almost very like similar and identical where you can just use the same configuration file but you want to use different state or you so essentially if you have a dev environment you don't want to you don't want to modify the dev environment for prod and you don't want to modify the prod environment for dev and it allows you state workspaces allow you to you know have your main configuration and then maybe you make big changes and you want or and you want to test those changes but you don't want to affect your production environment so like like say take this as an example so so let's say this is our production environment and that we already had our infrastructure in our instance and resources created but maybe we make some changes and we don't want to modify you know production yet but we want to modify you know just test this out on like a staging environment so we can create a a new workspace for this and in the tester I take changes and workspace is a little bit confusing and I was very confused when I first started it so I'm gonna get into some examples and hopefully what I was confused on you won't be confused on so the example I'm gonna kind of give is is we're gonna create a dev environment and a prod environment but we're gonna use this same config file for that and we're gonna pass in just different variables the terraform Doc's talk about workspaces as like get branches which which is definitely like correct III I was a little confused at that because it's not completely title I cover j'en control so I was a little bit confused so hopefully this debt this using a dev and a prod example here will make the the workspace is set in a little bit better for you in and how you actually use him so so let's get started so by default we've actually always been working with workspaces you know terraform actually when you start in create a new project you have a default workspace which everything is stored in there so what you can actually do with workspaces is you have you have certain commands on the CLI so you do terraform workspace list and terraformed workspace list will show you all of the workspaces that you currently have in this configuration so we're in our setups main here so right now we just have a default so always before you even get started on anything you always have this default and that's just what you're always working on but you can have you can have more more workspaces that store your store your state so what I'm going to do is I'm going to create two workspaces I'm gonna have a dev workspace and I'm in a prod workspace so we'll have you know our deaf you know instances and setup aw gonna be completely separate from our production instances in AWS and this is what the workplace to is it just allows you to have that same configuration for multiple places you know so you could you could use modules instead of workspaces if you want it's just it's up to you on your best the way you need to architect your specific needs but let's let's create our dev in our prod workspaces and the way we can do that is terraform [Music] workspace so everything is just terrifying workspace and then the command so we can do a new let's do prod it says switch to workspace and ieave switch to prod so if we do list now we have two workspaces prod and default and prod is selected by default you can see the asterisk there but let's create our dev as well alright so now we have a prod and dev I'm done and where we're sitting underneath the dev and for the most part if you're gonna use workspaces you're gonna want to make make the this your configuration dynamic so right now we've hard-coded or am i and our instance type and our name and our region well you're probably gonna want you might not want to have that you know hard-coded and what you want to do so what we're gonna do is we're gonna we're going to pass in some things to our our main configuration so then you know we can set we can set different variables for depend on if we're in dev or if run prod so for one let's do region let's let's let's let's grab our region from a variable and we'll need to define these in our variables file let's do region and then let's do I'm gonna comment that out because I like can use the ami little bit later we're gonna do a my let's do VAR am I so we'll pass that in and then we'll also let's pass in the instance type varta innocence type and then let's also pass in the name you know we don't want to we want to make sure we like define this as like dev or prod and what we can do is let's let's define a local to do this but what we can do is you know let's just put like you know instance name and if you're doing a real world scenario like we're doing this is very basic we're doing an instance you're gonna have a full configuration file of all this like your you know web server you might have security group something BBC and all this I'm just doing a very very basic example on all this that keep that in mind but instance name but what with with when we when you're working with workspaces you have access to that workspace name so most of the time when you do work with workspaces to make sure that all of your infrastructure is uniquely identified and you don't get runs like duplicates you can you can specify you can grab the terraform terraform dot workspace variable here so now what that's gonna do is that's gonna output either if we're in Deb it's gonna be Deb if we're in Prada it's gonna be prod so let's do that and then let's just do like instance so now we have the locals there and then let's replace this name with local dot instance name so now our our instances are gonna be named you know dev - and sense or prod - and sense depending on what workspace we're working in and then our region will be whatever we pass in alright so let's let's define these variables so let's create our variables TF file and let's see and if you actually look here we haven't ran anything but since we've created a new workspace as you see as terraform state D file whereas before our terraform our state files would just be json files inside this our main directory here but since we're doing workspaces it creates a new folder structure to store our actual our state there so you can kind of see how it's separating it out already alright so let's do what do we want to do next let's let's define our variables I'm gonna define these I will as I define these I will skip ahead the video since you guys should know how to define the variables and you don't need to waste time watching me do that alright so we've got our variables defined here so now what we want to do is we want to we need to be able pass in our variables when we create these when we run like tariffs on reply so if you recall from previous videos there's a few different ways you can you can set variables you can do a we're gonna let's make this terraform TF bars so you can add values for TF bars here if you want I'm not gonna add any right now but depending on what you how big your setup is you'll probably want to do that but you can also pass in variable you can pass in these variables through the command line if you wanted to you know so you could do let's just do we're in terraform you can do terraform workspace show which will just show the exact current workspace for n so we're working in our dev or dev environment so right now since we're on workspace dev we won't touch this is not going to touch production whatsoever it's completely isolated to the dev we're using the same config files but it's completely isolated to dev but one way we can we can pass in these variables so if we run let's just run terraform apply all I need to do in it let's do careful in it first I check for net so now if we run terraform apply it's gonna ask us for some variables so we have all that like we can enter and enter them into the CLI but we don't want to do that it's not good practice but so one way we could do is you know we could do var and get specify them on the command line if we wanted to that's perfectly valid but what I'd like to do is I would like to create a new variable file for our product and our dev environment so we can have two different files for that and then we can when we run our apply we just specify which which configuration file we want so that our entire configuration is just defined in a variables file so let's create a let's call it dev TF VARs and let's do a a prod TFRs all right so let's let's let's add some data to these so let's say in dev we want to work in us West two for the region but our production we want to work in like US East one so we can completely control that now from our variables so let's define our region am I an instance type inside of each of our var files so for dev let's do and we've gone over this that on how to specify TF bars file you know this I'm not going to go in into that but if you need if you're not sure on how this actually works there's a previous video that you can watch for the how input variables work so if you need to go watch that and then come back but region will do you know us West two for dev and then ami we're gonna do I think I commented out here will just gross grab this am I here and then we need incense type let's just do t2 dot you know medium maybe that's all we need for for productor for development so we have that so this is our dev configuration so when we actually do terraform reply we can specify this these variables for this configuration but let's specify our production in variables as well so let me go to prod use copy and paste this but in some of us Wes - let's do us east one and then I need an ami from us east one let me let me find one so let me just copy this and then and then maybe for you know production we want to have you know TT too large we won't have a bigger instance you know so you see how we're specifying we're gonna use this same configuration file but we're just we're gonna specify what what we want by using different workspaces and different variable files alright so we've got our pride and our pride in our dev variables defined so let's um let's create one of these so right now we're in dev so we wanna let's create our dev environment so our dev environment is gonna go to us West two because we've defined that so we can do terraform apply and we're gonna specify a var file and we're gonna do dev dot EF of ours and like this Tara format TF bars you know that will get automatically loaded but the the dev TFRs and the product here for ours will not get automatically loaded so we can specify that here that you know when we're in dev let's let's load the variables from here so if we run that see if we have any syntax errors or anything like that so that's good so everything looks good there so let's just do yes I so that's great so if we go to our interface we should see our dev instance here hopefully all right so we do so this is our dev - instance and that's because we specify the name here as as the terraform that workspace which is dev since that's where we're at so perfect so now we have our dev desu instance and then let's go to us east one and you can look and we shouldn't have any instances here and we don't but let's let's let's create our our [Applause] our prodding sense now and let's switch to our workspace by doing terraform workspace select trot so now we're in our prods alleges verify telephone so now we're in now we're in prot so now let's let's run our production so let's do terraform our production setup terraform apply var file so now instead of doing dev TFRs let's do prod TF bars I'm just gonna do Auto approve and this will take a a minute alright so that's great now so now we should see we should see our fraud instance inside of us East 1 and we do so we have prod instance here so perfect and then let's go let's just make sure our our development instance wasn't touched and that looks good so so it wasn't so that's perfect so let's let's um because now you can see like how your state is stored differently so since we're in you know since we're in the prod workspace right now nothing at all happens to the dev environment you know because our state is stored now inside of different directories based on the workspace so just to just to show you that so let's go to you know prod and let's let's change the the large down to a extra-large and you can see let's just make sure somewhere in T to medium for dev just we'll come back to that just to make sure that T to medium is still there but now let's change let's go from a tee to large through T to extra-large and since we're in the prod workspace it will do that it's gonna run that again and that'll take a minute I saw that completed so now we should see here we go t to extra-large and then now if we go into our US west we should still see the T to medium if that's what we had to think so we sell a TT medium so that was not touched because we were inside of the prod workspace and so that's that's the basics of workspaces hopefully you have a better understanding on that and it could get confusing on how you might structure your code so you could actually like accomplish the same thing by just creating multiple directories you know in your configuration but it's up to you on kind of what would suit your your needs you know I might make another video probably on workspaces vs. directory structure because you know we were working on the setups I explained in the last video you might have dependent different configurations are going to be stored inside of one directory so you might have in some setups you might have like Johnny and Willie and fubar and then your configuration is based on those of those directories but that'll be a different video but make sure you hit the subscribe button below so you don't miss out on any of those future videos and I will see you in the next video
Info
Channel: Will Brock
Views: 19,933
Rating: undefined out of 5
Keywords: Terraform, AWS, Azure, Linux, Cloud, Cloud Provider, Infrastructure as Code, Servers, CloudFormation, ARM, VM, Virtual Machine, Networking, Workspaces, Beginner, Tutorial, IaC
Id: JmfEKEYdxKU
Channel Id: undefined
Length: 19min 17sec (1157 seconds)
Published: Thu Jul 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.