Check out a free preview of the full Enterprise DevOps & Cloud Infrastructure course
The "Moving Resources" Lesson is part of the full, Enterprise DevOps & Cloud Infrastructure course featured in this preview video. Here's what you'd learn in this lesson:
Erik uses a moved meta-argument to prevent resource deletion during updates. Once the change has been planned and applied, the moved meta-argument can be deleted.
Transcript from the "Moving Resources" Lesson
[00:00:00]
>> So again, you should have basically this code set up to where you have a locals now, you have a backend now, and you have a main now, right? So let's do what we always need to do. What's the command you think I need to run next?
>> Terraform init?
[00:00:14]
>> Nope, what's the next one? We've already initialized, so we're good there.
>> Is it validate?
>> Yes, validate. Looks good. Now if we wanted to, we could init one more time just to be safe. But you only really need to initialize when you change this value for a project.
[00:00:32]
So for example, if I went flippyflop, You'll see the module is not installed, right? Now why would it say that when we've already done it, right? Terraform is also kinda dumb. It might have magic but it's also very dumb in some regards. And when we create or when we copy our modules, If I go back in here, this looks like it says project and workspace, which might make you think, cool, cool.
[00:01:03]
Now, those are the resource names, those are the actual resource names. So for example, if I was to do another init, See how flippyflop's there now? Terraform is kinda dumb in the sense that it'll copy the same thing over and over and over again because it needs that directory for that module.
[00:01:29]
It's just going to a directory and saying, hey, I need this. But if you're iterating over a for loop, then it needs to make sure that it doesn't actually go to the wrong path, right? So Terraform will look for all of your modules you're using, and then it'll download all of them, right?
[00:01:45]
>> And it didn't get rid of your project resource?
>> Because I didn't change the name of it. Cuz down here it still says workspace. You mean, why didn't it delete the existing one? Just doesn't clean them up. Yeah, it just doesn't do clean up, yeah. So let's go back here and then go to project again.
[00:02:04]
Let me reinitialize just so that I'm good. And then, yeah, we're gonna do a validate. Figuration is valid, fantastic, okay, cool. So let's do this, let's do a plan, we're already there, right? We got a bit of a problem. Can anyone tell me what this problem might be?
[00:02:30]
Give you a hint somewhere around here.
>> Gonna get rid of them.
>> Yeah, we're actually gonna destroy them. That's not good. Well, let's take a look and see what we got here. Well, okay, I see my workspace. Cool, look at this. You could see it's adding the key now, so we know that this is the new workspace it wants to create.
[00:02:52]
But because we renamed it, technically Terraform's now going, I don't know what this resource is. I created module.workspace. I didn't create module dot workspace dot blah blah blah, right? So how do I fix that? This is a problem that a lot of people run into where they'll create new automation and then they'll change it and they'll go,like, no, what have I done, right?
[00:03:18]
Don't worry, there's a way of working around this. There used to not be, but about a year ago, TerraForm realized that this was a terrible pain point, and they made it a bit easier for us. They added another meta argument called moved. And just as you think it might do, it's going to take something from one location and tell TerraForm that it's somewhere else now.
[00:03:43]
So it's literally just from and to, right? And so what we wanna do is, is we wanna tell TerraForm, hey, whatever you thought was gone, in this case, module.workspace, I'm gonna copy that, I'm gonna go back to my main and I'm gonna go from, because that's where I was, right?
[00:04:07]
But where I want to be, Is here. Right, we're using that key value targeting again to tell TerraForm, hey, don't delete this, please, [LAUGH] I need this, right? And we need to do it for one other thing, which is our project, right? So both resources that are going to get deleted must get moved values for them.
[00:04:38]
And sure enough, you can see that it's saying, okay, cool. I see that you're changing stuff, right? However, this isn't, yeah, this is good. Okay, cool, sorry. So let's go here, project. Now if I save this and I do a plan again, you could see now, look at this, 0 to add, 0 to change, and 0 to destroy, right?
[00:05:06]
So we've now told TerraForm hey, these got moved, so please don't delete them. Please keep them and keep managing them. And you'll see here that it actually says, okay, cool, I see it. So it will tell you even when it recognizes that things have moved, which is nice.
[00:05:21]
So if you get here but you don't see that things have moved, you might wanna take a look and see why that might be going on. So now, what I can do is I can actually apply this, but you might be like, what are you applying? Why are you doing that?
[00:05:38]
Well, technically, we did update state, even though we didn't actually change any of the, right,we didn't actually change the configuration of a project or a workspace, right? But we did tell TerraForm stuff technically moved, right? So we do still wanna apply that to make sure that it knows that those changes actually happened, right?
[00:05:57]
So we're gonna do terraform apply.
>> Do you have to make the move block and apply to reflect the changes it wants-
>> Yes
>> And then you can remove it? Or does the moved need to exist in this case forever in the repo?
>> Yeah, it doesn't need to exist forever in the repo.
[00:06:14]
Thankfully, [LAUGH] they realize that that would be very annoying, cuz think about it, then you move something again, how do you do that, right? So you move it once, and then you apply it, and then after the move, you can actually go in here and go into your main file now that we've applied it, right, And then do a plan again.
[00:06:37]
Oop, sorry, dang it. Don't Ctrl+C TerraForm while it's doing stuff, it doesn't like it [LAUGH]. State is locking and all that, it doesn't really work well. So yeah, there you go, you see how I've got no changes now and my moves are now gone. So yeah, you definitely can get rid of them.
[00:06:55]
Again, thankfully they made that nice for us. So, cool. So we now understand that we can do some cool things, right? We understand that we can have our main resources, right? We also understand that we can create this really cool concept of a configuration domain language of our own, right?
[00:07:15]
And then we also have our backend, which enables us to push to the cloud. And again, the biggest stuff I really want you guys to take away here is locals is really powerful. It seems like it's very not, but TerraForm does have a lot of different types of variables, right?
[00:07:31]
You can have lists, you can have strings, you can have maps. And so what's really powerful is, is you start using that in your configuration. So if you wanna do for loops, if you wanna do just direct values or nested values, you can really start to lean on the language here to be able to do that.
[00:07:49]
And so yeah, congratulations, you have now officially set up your first dynamic configuration within TerraForm.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops