Moving Resources in Terraform State

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Applause] [Music] [Applause] hey what's up everybody it's ned bellevant's ned1313 on twitter and welcome to the daily check-in for november 10th 2020 and today is tuesday which means it's terraform tuesday we're going to be talking about hashicorps terraform and specifically we're going to talk about how you can manage resources that exist in your state file this is actually based off of a viewer request from vomcb he asked me how can you move existing resources from one terraform state file to another if you were trying to like break up a big configuration into smaller configurations without destroying what those configurations are managing like how do you even go about that well i figured out how to do that and also some other things that you might want to do if you're manipulating your state files so that is what we're going to get into today hopefully that's interesting to you i do want to mention if you are interested in terraform and you'd like more of a formal course if you will the best way to do that is to check out my courses on pluralsight i have four terraform courses one is getting started another one's a deep dive so we go a little deeper and then there's one that's specific to aws and one that's specific to microsoft azure and yes i have been asked if there's a google cloud one coming there will be eventually i just haven't gotten there yet there's a lot of stuff going on so if that's of interest to you go to pluralsight a link's down in the description you can check it out sign up for a 30-day trial or sometimes they just do these free weekends and stuff just you know check it out see what you think before we get into today's topic i want to check in with you how you doing how's tuesday are you having tacos is it a taco tuesday i hope it is unfortunately it's not a taco tuesday for me my son really wants to have meatloaf tonight and so we acquiesced and said you know we're gonna make you meatloaf and mashed potatoes buddy because you've been asking for it and we're going to do like taco wednesday so it's cool i'm still getting my tacos this week but just not it's not going to be today so i hope you've got something good planned for food or anything else whatever whatever gets you going uh let's try let's dive right into this topic here with how you can manage objects with terraform state okay so i'm gonna go ahead and share out my screen here i'm using visual studio code as always if you're looking for these files the link is down in the description i keep all of them in my terraform tuesday github repository so they're super easy to find the folder in question we're talking about is today's date the 10th of november now to just kind of set the stage a little bit the first question was what can you do if you want to m manipulate or move objects so there let's take a look at a very very simple basic example that i've got going here let me scroll up to the top and why don't i hide the terminal for a second okay so this is a super simple configuration all it does is create two resource groups in microsoft azure so if we scroll down to the bottom we can see we've got one resource that we're calling tacos another resource we're calling enchilada and each one creates a resource group based off of a name and that's it that's all we're doing we're creating two resource groups so let's go ahead pop open the window i've already initialized my terraform config so all i have to do is terraform apply and we'll do auto approve so we don't have to do a plan or confirm all right now that i have started the terraform apply it's going to go ahead and try to create these two resource groups we should see a terraform tf state file be generated to the left there we go there's our terraform tf state file and it's going to go out and create those two resource groups in microsoft azure now what do we want to do once those two resource groups are created oh there we go see it was that fast let's go ahead and open up that tf state file and you know you're not supposed to manipulate this thing but we can just take a look so under resources we have an azure resource group called enchilada and we have another azure resource group called tacos now let's say i you know threw this configuration together and then i suddenly realized man i don't want to call this thing enchiladas it's storing burritos i want this resource group to be called burritos what can i do well the good news is we have a handy dandy command called terraform state move so if we do terraform state list it will list out the resources that we have in our state so we can see we've got enchilada and tacos now let's go and we are going to run a command and this command moves a resource from one entry to another so here's the command i'm going to break it down for you terraform state mv for move the source of source resource you want to move so we want to move azure resource group dot enchilada that's the thing we want to move and where do you want to move it to we want to move it to dot burrito so let's go ahead and grab this whole command we'll copy it and we'll paste it down here and now it's going to move this resource object within the state file so let's go back to the state file once it's done and take a look and now we can see that the name for this has changed to burrito awesome that's what we wanted we renamed a resource within terraform now we actually have to update our configuration to acknowledge that so let's go ahead and rename the resource here to burrito oh burrito almost had a typo there okay so now it should all line up if we go ahead and run a terraform plan because we moved it in state and then changed the name of the resource it should say that no changes are needed because we're not actually changing anything in the cloud this is all internal terraform machinations and i've actually been through this where i have named something and then realized that it wasn't really a good name for it and then i want to change it later and at that point it's too late see no changes infrastructure is up to date we don't have to change anything so that's one example of why you might want to use this command and how you might want to manipulate your state file what's another example let's say that i realized at some point that i want to move the tacos resource group into a module i want to use it as a module i don't want to have the research resource group created directly and obviously this is a little contrived right we're not going to do this for a resource group but what if you did want to create a module for a bunch of resources you already have in your terraform configuration how are we going to do that okay so we're still going to use the terraform state move but what we're going to do is the source will be azure rm resource group.tacos and the target is going to be module.tacos.azureresourcegroup.tacos so we're creating a module called tacos and putting the resource group.tacos inside that module and you would have to do this for all the resources that you have in your current state file that you want to be in this module so let's go ahead and run that command now and copy and paste it down here and once that's complete we'll take a look at the updated state file to see what's changed now that we've done this move all right the move is complete and now we still have our resource group burrito but if we scroll down we now have a module called module.tacos now this doesn't actually alter your configuration you still have to update your config with this information so how do we go about doing that well we no longer have the resource group directly right so we're going to go ahead and comment out this resource group and if we scroll down here i've got a module called tacos and i'm going to point this module at a source this local folder over here called tacos what's in there i've got a main.tf file in here and all it does is take two variable arguments a region and a name and then it creates a resource group and the resource group is an azure rm resource group called tacos so the addressing still lines up it's in the module tacos and it has an internal address of azure resource group dot tacos it all should line up now and before i do anything else i have to initialize my terraform configuration because i've added a module here so you have to run terraform in it first now that it's aware of that module now when i run a terraform plan it should in theory use that module and then recognize that nothing needs to change on the infrastructure side because i've simply moved resources into their own module but it's all still in the same state file so that is our second example of how you might want to use this move command to reconfigure your existing configuration sort like break it up into modules all right there we go no changes infrastructure is up to date that's what we're looking for now the third thing i wanted to show you and this is an answer to vomc's original question is how do i move resources into a new state file and a new configuration without breaking anything okay that's the million dollar question what if i decided now that i don't want my burritos config to be part of this config anymore i want to move it off in its own state file and manage it as a separate configuration how am i going to go about doing that well there is this argument you can add to the move command called state out and you can say i don't want you to update the existing state i want you well i do want to do that but i also want you to move this resource to a new state file so i'm going to point it at this directory burritos and a new state file called terraform.tf state the addressing for the resource is going to say this is going to stay the same it's going to be azure resource group.burrito is moving to that same address in the new config which means in this directory burritos i need a main.tf file that creates that same resource and make sure i make it singular okay so in this other config i now have this resource called azure resource group burrito and by running this move command i'm going to be moving my resource into a tf state file so let's go ahead and grab this entire thing and i'm going to go ahead and paste it down here and what this does is it removes this entry this resource entry entirely from the existing state file and puts it in a new new state file in the subdirectory so there's my new state file in the subdirectory boom if we look at the rate at the existing tf state file the only thing that is in it now is that module.tacos resource if we look at this terraform tf state file there's my resource group burrito and if we go into the directory and we go ahead and let's do a terraform in it right because this is a brand new config i've got to initialize it okay and once that terraform config is done we can do a terraform plan and because the resource already exists and the state file is there it's going to go and say that no infrastructure changes are necessary because that resource already exists up in the cloud i don't have to change anything and in that way i've successfully moved a resource from an existing state file to a newt state file and moved the configuration out of the existing config into the new config now since i did that what i would have to do is go all the way back into my main configuration and just like i did with the moving the tacos into the module i now need to comment out this resource because i've moved this resource out of the config entirely it's not in there anymore and if you did go up into that directory and run another terraform plan and wait a little bit it's going to tell you no changes infrastructure is up to date on this as well so let's go over what we just did we took an existing resource and renamed that resource but kept it in the same state file so if for whatever reason you wanted to rename a resource you could do that and that's definitely happened to me i gave it a bad name i want to give it a better name in the second one we moved a resource into a module so we're starting to break apart our configuration into modules a little bit but it's still all in the same state file and then number three the third thing that we did was move a resource into a new state file with a new configuration and in all of these cases the actual resources running in the cloud were never impacted those stayed exactly as they were but we were just manipulating things within the world of terraform so i know that it might be a little confusing at first but trust me as your terraform configs grow and your organization grows and starts using it more these are the little things that you're actually going to find are extremely useful so that's all i have for today i hope you enjoyed this presentation i hope you enjoyed the video please subscribe and share and check out my courses on pluralsight and that's all i got you know reach out to me on twitter ned1313 or leave a comment down below if there's a topic you want me to cover on a terraform tuesday until next time stay healthy stay safe out there bye for now [Applause] [Music] you
Info
Channel: Ned in the Cloud
Views: 1,100
Rating: 5 out of 5
Keywords: Managing State in Terraform, Terraform Tutorial, Terraform Certification
Id: 45z2pZSJEhQ
Channel Id: undefined
Length: 14min 25sec (865 seconds)
Published: Tue Nov 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.