Cloud Infrastructure: Startup to Scale

S3 Bucket for Terraform State

Erik Reinert
TheAltF4Stream
Cloud Infrastructure: Startup to Scale

Lesson Description

The "S3 Bucket for Terraform State" Lesson is part of the full, Cloud Infrastructure: Startup to Scale course featured in this preview video. Here's what you'd learn in this lesson:

Erik creates an S3 bucket in AWS to store the Terraform state and ensures the AWS CLI is still logged in and can see the bucket. The .gitignore file is updated to ignore any Terraform-related files that don't require source control.

Preview
Close

Transcript from the "S3 Bucket for Terraform State" Lesson

[00:00:00]
>> Erik Reinert: So one of the things I do want you guys to do really quickly is just make sure that your previous branches were merged so that we can create a new branch off of the main and at least make sure that you've got a branch for this stage to work in.

[00:00:14]
So yeah, just please make sure to do that while I'm going through all this stuff. So once we've got our new branch, we want to go ahead and basically get started setting up Terraform. So actually this first step, I will say go ahead and follow me because it's just the AWS console.

[00:00:31]
But what we're gonna do is, we're gonna step up no is we're actually going to set up our state first. Does anyone know what I mean by State when I say what does it mean to set up our state first? Terraform has to know or needs to have a place to store how things are built and allocated and behavior.

[00:00:50]
Yeah, so basically, when you run Terraform, it keeps data that is referential to your resources. So it knows like, I created this instance, I know that this is the ID of it. Right, like all these other things, so that it keeps track of everything that's been created that's called Terraform State.

[00:01:11]
Terraform State is something that you want to hold on to very closely and take very good care of. Because if that state disappears, gets deleted or anything like that, then you have to delete or manage all of your resources manually. And then on top of that, if it does get removed, or if any of the state gets removed, then you also need to re import all of those resources.

[00:01:30]
So overall, State's a very sensitive piece of data that you need to take care of and make sure that it has backups and all that kind of stuff. What's nice about this though is that Terraform does provide you the option of storing State in multiple locations. You can store it in the cloud, you can store it on another server, you could really put it wherever you want.

[00:01:54]
Because we're using Amazon, we want to store it on Amazon. What we want to do is we want to actually store it on S3. S3 is completely redundant. S3 has version support. S3 makes a really good option here because again, we have high availability, redundancy, all of that good stuff.

[00:02:13]
And even versioning, if we wanted to, to be able to have multiple versions of that state, what we're going to do is we're going to go to S3 and then you'll see I have a whole bunch of buckets that already exist here. But what I want to do is I want to create a new bucket.

[00:02:32]
So I'm gonna click Create Bucket. Now here's the fun part. I don't think we'll be able to create all the same bucket names because I think we'll. Isn't that how S3 works? Yeah. So I think if I create a bucket name and then you try creating the same bucket name, it won't work.

[00:02:53]
So what I'm going to do is I'm going to do FEM FD service and then I'll just put like, I don't know, the account ID maybe. Yeah, I mean, no, you could put the account ID if you wanted, I'll just put like, I don't know, black glasses, I guess, whatever.

[00:03:12]
You can put your own name there. But yeah, just some kind of unique identifier so that you know that it, you know, you won't conflict. Does anyone know what I'm talking about, by the way? Can somebody tell me what am I talking about? Well, I think it needs to be globally unique.

[00:03:28]
Exactly. Yeah, Exactly. So Amazon S3 buckets are global. They're provisioned across the entire world, basically. And so because of that, you're at the mercy of the uniqueness of the name of a bucket. So somebody else could have a bucket name that you may want and you cannot use it because now they're using it.

[00:03:50]
It's actually one of. It's kind of interesting, but it's also kind of annoying as well. So, yeah, I'm just going to do. Actually, I'll just do Alta 4 because that makes more sense. So I'm gonna o FVM FDService Alta 4 and then that's it. That's all I need.

[00:04:04]
I'm just going to click Create Bucket after that. Cool. Okay, so I have a new bucket called Fem FD Service Alt F4, but I have absolutely nothing in it yet. So let's go ahead and put something in it. Okay, so the next thing we're going to do is we're actually going to go back to the console and I'm going to make sure that I am logged into Amazon.

[00:04:35]
Okay. And then I'm gonna do an AWS S3. Oops, nope. AWS S3LS just to make sure. Cool. Yep, looks good. I can even see my bucket right there. Cool. Awesome. And then the next thing I'm going to do is I'm going to actually add a couple of things to my git.

[00:04:53]
Ignore, right? Because we're going to be using Terraform Terraform generates a lot of files. We don't want every file to get committed. So the first thing we're going to do is we're going to do star.tf plan. So we want to make sure that none of our plans get committed.

[00:05:06]
We're also going to do Terraform just to make sure that any directory or anything name Terraform doesn't stay. And then we're going to do one more called overrides. Txt, which is a unique file that we will be creating a little later. So we're gonna add those three things to our git ignore.

[00:05:27]
And then what we're going to do is we're going to make a directory called Terraform.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now