Check out a free preview of the full Enterprise DevOps & Cloud Infrastructure course
The "Migrating to Terraform Cloud" 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 migrates the state to Terraform Cloud and removes the local state from the repository. Doppler is used since the authentication keys are required to connect to Terraform Cloud.
Transcript from the "Migrating to Terraform Cloud" Lesson
[00:00:00]
>> So we've created our workspace, right? However, there's a bit of a problem here. The big problem is that our state is local, right? So right now, if I was like, all right, cool, see you guys, have a good one, right? And my laptop destroyed somehow the state's gone, right?
[00:00:18]
And if there was thousands of repost or thousands of resources and stuff like that, I'd be kind of in a bad state, right? Get it? What we wanna do is, is we wanna move that state to the cloud, right? So that we don't have to worry about where it is and it's safety essentially.
[00:00:32]
And so to do that, we're gonna click on our workspace, actually. And you'll notice that there's a nice little documentation set here where it actually includes some example code. This is what's called a back end file, right? The idea here is is that right now if we look at my code, I'm not actually pointing my state to anything.
[00:00:53]
That's why it's local. But if I wanna make it so that my state is actually pointed to something, I create a backend.tf file or basically this file right here. So I'm gonna copy this, say new file backend.tf, and then I'm gonna edit that file. And again, you should have this in your workspace if you just created it, so you can copy this, paste it in and save it.
[00:01:20]
So now you should have a back end.tf file, a main.tf file and a variables.tf file, all right? These are the three files that we need to be able to not just automate our things, but also push them to the cloud. All right, so now what we're gonna do is, is we're gonna init one more time.
[00:01:39]
Can anyone tell me why I might need to initialize one more time?
>> Migrate the state.
>> Yes, and the main reason why we initialize is because when we initialize it, does that initial reach out to Terraform Cloud. So that's another reason why you initialize, is because it makes it so that your backend's properly set up, everything in your workspace or your environment is basically good to go.
[00:02:04]
So this time, however, you'll notice that when I go back to my Terraform in it, I'm actually gonna put Doppler in front of here. Can anyone tell me why I put Doppler in front of here?
>> Cuz you're gonna put it in HashiCorp's cloud.
>> Yes, exactly, yeah.
[00:02:18]
So we're gonna do Doppler run, Terraform in it, and then what we want it to do is because Doppler has the credentials inside of it, we want Doppler to pass the Terraform cloud credentials and then make it so that we can initialize it. And sure enough As we initialize TerraForm cloud goes, Hey, well, wait a minute, I see that you have state locally.
[00:02:39]
Would you like us to migrate your existing state to the cloud? And sure enough, I can hit it. I can type in yes, hit Enter There we go. My state is now in the cloud. So if we go back to our repo really quickly, look at my TF state file.
[00:02:57]
it's completely empty now. And if I even open it up there's there's nothing in here, right? And so what we wanna do is is we wanna really just delete all of these files, the three files here. Now note one of these says terraform.tf state backup. You might not wanna delete this file.
[00:03:15]
[LAUGH] If you're in a circumstance where you're initializing a repo that had a lot of resources in it already, you wanna make sure that it's there, stuff like that. It might be worth backing it up. However, don't commit it to the repository. There are secrets and other things that could potentially be in there and it would expose, if you just committed that to Git.
[00:03:34]
So let's just delete these. Right? And then let's do a plan one more time. So terraform plan. And again, I'm gonna put doppler run in front of it, because going forward, I need to be able to communicate with Terraform Cloud. If I can't, then this will fail, all right?
[00:03:51]
So for example, if I actually Go here, and then I hit enter, you'll see that I fail, right? But if I do doppler run, It'll actually work, right? Because we're passing in those credentials. So you'll notice that it says, hey, you got no changes. Your infrastructure matches the configuration, right?
[00:04:13]
Awesome, there we go. We have officially set up our repository in TerraForm cloud and we have automation running against it with state in the cloud so that we don't have to worry about anything. So that's the first step of what we wanna do.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops