AI Summary
This tutorial demonstrates how to use Terraform with AWS to set up a budget. It covers installing prerequisites, configuring AWS CLI credentials, writing Terraform code, and executing commands to create and modify resources.
Chapters
Need Terraform (v12 or 13) and AWS CLI (v2+) installed.
Create an access key in IAM, then configure AWS CLI with 'aws configure'.
Create a main.tf file with terraform block, provider block, and resource block for budget.
Run 'terraform init' to initialize the working directory and download providers.
Use 'terraform fmt' to format code and 'terraform validate' to check syntax.
Run 'terraform plan' to see changes, then 'terraform apply' to create resources.
The tfstate file is a receipt; safeguard it for Terraform to track resources.
Change values in code, then run 'terraform apply' to update the resource.
Terraform with AWS makes infrastructure management simple and repeatable. Always safeguard the state file and use version control for your code.
Clickbait Check
95% Legit"Title accurately reflects the beginner-friendly tutorial content."
Mentioned in this Video
Tutorial Checklist
Study Flashcards (8)
What are the two prerequisites for using Terraform with AWS?
easy
Click to reveal answer
What are the two prerequisites for using Terraform with AWS?
Terraform (v12 or 13) and AWS CLI (v2+).
00:25
How do you provide credentials to Terraform for AWS?
easy
Click to reveal answer
How do you provide credentials to Terraform for AWS?
Configure AWS CLI with 'aws configure' using an IAM access key and secret key.
01:05
What does the 'terraform init' command do?
easy
Click to reveal answer
What does the 'terraform init' command do?
Initializes the working directory and downloads required providers.
04:40
What is the purpose of 'terraform fmt'?
easy
Click to reveal answer
What is the purpose of 'terraform fmt'?
It formats the Terraform code to follow standard conventions.
04:52
What does 'terraform validate' check?
easy
Click to reveal answer
What does 'terraform validate' check?
It checks that the Terraform code is syntactically valid and has required information.
05:21
What is the difference between 'terraform plan' and 'terraform apply'?
medium
Click to reveal answer
What is the difference between 'terraform plan' and 'terraform apply'?
Plan shows what changes will be made; apply executes those changes.
05:33
Why is the Terraform state file important?
medium
Click to reveal answer
Why is the Terraform state file important?
It acts as a receipt, tracking the resources Terraform manages and their current state.
07:11
How do you modify a resource managed by Terraform?
medium
Click to reveal answer
How do you modify a resource managed by Terraform?
Edit the .tf file, then run 'terraform apply' to update the resource.
07:37
💡 Key Takeaways
Pessimistic Operator Explained
The speaker humorously calls the version constraint operator 'pessimistic operator' and says it's 'fun to say'.
03:31No Smoke and Mirrors
Before applying, the speaker shows the AWS Budgets page is empty, then after apply it appears, demonstrating Terraform works.
06:27Quick Modification Demo
Changing the budget from $500 to $700 and reapplying shows how easy it is to update infrastructure.
07:37Full Transcript
[00:00] Hey everybody, today I'm going to show you how to use Terraform with AWS. And in this example, we're going to set up a budget with an AWS using Terraform to build and maintain it. So, let's get started.
[00:13] I have a Visual Studio Code editor here, it's one of my favorites. I'm going to go through a few steps to get this environment all set up for doing the work that we want to do. The first is making sure that we have the proper tools installed, and we're going to need two.
[00:25] First is Terraform, so Terraform version, just make sure it's installed. I'm using 13.2, anything 12 or 13 should be fine. And the AWS CLI, so AWS dash dash version.
[00:38] I think I'm running, yeah, 2.046 is fine. I think anything above 2 is fine. The reason for this, Terraform, we need that to write the Terraform code and execute it. And then the AWS CLI is used to provision credentials.
[00:51] We're actually going to make some default credentials on the box. And then when we go to use Terraform to build things in Amazon, it's going to look for those credentials and use them. So we have the tools here. That's perfect. The next thing I want to do is get some credentials that I can supply to the AWS CLI.
[01:05] For that, I'm going to IAM Management, and I've gone ahead and drawn up my user. This is my user demo account here. We're going to create an access key. It's about in the middle of the page. So create access key, and I'm going to show everything on the screen.
[01:18] Normally, you don't want to show this stuff. This is like secret password kind of stuff, but that's okay. I'm going to throw this away. So let's grab these two things. I'm going to start with the access key. Switch back to code. Now I need to configure the AWS CLI. And again, this is just so we can get credentials to Terraform.
[01:33] So AWS configures. It's going to be the access key. We just made that, so I'm going to paste it in. The secret key is back on the other screen. Grab that.
[01:45] And let's do this in USD 1. and no special output format. That's it, at this point we've made sure the tools are there and we have it properly configured so that we have credentials.
[01:58] And you can actually see that. If I go to the .aws folder within my user profile, you'll see these two files that get created if they not there already Let take a look at those So the config is more about the configuration of the tool regions and things like that as you can see on the screen And then credentials show the access and secret keys for any profiles that you have here
[02:18] So that's it for setting up credentials. We have everything set up for making Terraform actually do what it needs to do. So I'm going to clear all this off. And let's write Terraform code. There's lots of ways you can do Terraform code.
[02:30] You can split out the providers and variables and stuff. And I actually do advocate for that, but we're going to keep it simple. I'm going to make one file. We're going to make it right in the root of the code directory, call it main.ts, and we're going to write the code here. We're going to be working with local
[02:44] state. We're going to be doing some very simple commands kind of to bootstrap you so you can see how this works, but there's a lot more you can do here. I'm only scratching the surface. We're going to need a couple things. Let me grab some code and we'll walk through it step by step.
[02:56] So I've got the first block of code. It's a Terraform block. This is literally telling Terraform, hey, I need you to do some things or configure yourself a certain way. And you can see I've got this required providers block here. This is letting Terraform know, I need you to grab these
[03:14] providers. In this case, I want to work with AWS, so I need AWS's provider in order to translate my Terraform code into resources in Amazon. And then we have the fact that it's called AWS, and we've got a source and version standard here. Source is, where is it? In this case, it's on HashiCorp
[03:31] slash AWS. And the version I want is 350 or better, but no more than 4.0. That's why it has a little squiggly line there. It's a pessimistic operator, which is kind of fun to say. So that's
[03:43] letting Terraform know that it needs to grab the AWS provider. Additionally, we're going to need to tell the provider what region to use. So I'm going to go ahead and drop that in there. So I'm saying, hey, AWS provider, you probably need to know the region. I'll supply our value for that. And that
[03:57] information is in the documentation for the provider that needs to know the region. So we have Terraform and the provider configured. Last thing we're going to need is to actually tell it to make a budget. Let's grab the budget code.
[04:09] There it is. So I said, build a resource. That resource type is an AWX Budgets budget because it's in the budgets group. And then the contextual name that I given this resource is like and subscribe And so within that block that resource block I have the name of the budget that we want to build and some other information
[04:28] Every month, I don't want to spend more than $500. And that's kind of it. Now we're ready to run some Terraform commands. So make sure to save your file. And I like to do it in this order. So the first thing I'm going to do is Terraform init.
[04:40] That's going to go rather lighter. We kind of need that to do anything. And if you accidentally forget to do an init, it will power you. it will say, hey, you need to init first. Cool, we're initialized. The folder that contains our code is now ready to be run.
[04:52] I'm gonna do two housekeeping items first though. The first is terraform format or FMP. It's gonna clean up the code. I can actually show you what that looks like. Let's say I had like too many spaces here. These are kind of off-center.
[05:04] It's not considered proper formatting. So if I run this command again, you can see it fixed all those little imperfections in there. And it tells you main.tf has been modified. So that's kind of nice. I'm going to clear my screen and let's do Terraform validate.
[05:21] That's going to make sure that I have valid code. It kind of looks at the resource blocks and the Terraform blocks and everything and makes sure that I have the required information and things like that. So we've initialized, we've formatted, and we've validated.
[05:33] Now we can actually run it. There's two parts to this. You can do a plan and see what's going to happen and then follow it up with apply. Or you can just go straight into apply, look at it, and say yes or no. I'll go through the proper way. Let's clear the screen one more time and do a Terraform plan.
[05:49] And what that's going to do is look at all the resources that I want to build and see what needs to be built. Now, there's nothing built so far. There's no state file. There's no information. So it's telling me pretty plain and simple. You need to build all the things.
[06:01] This plan is going to create things. And what it's going to create is the budget, contextually called like and subscribe. And the name of the budget is monthly budget. So this is all the information of what's going to get built out. This looks fine to me.
[06:13] I'm just going to go ahead to Terraform Apply. I can just do that if I wanted to, but I'm in the habit I like to plan first just to kind of see things. You get the same kind of experience with Apply, especially if you don't supply a plan file to it. So I'm just going to say yes, that looks good.
[06:27] However before I click that button let switch back over and look at the budgets Just to be clear I refreshed the page There no budgets here There no smoke and mirrors So I gonna go back and say yeah I do want you to do that So the provider is going to create that translation between
[06:41] the configuration we just wrote in Terraform and what needs to happen in AWS. And it took one second. It's just really quick. I'll go back to budgets, click refresh, and there it is. It's just that simple. At this point, if you need to make a
[06:57] change, make it in Terraform and rerun the plan and the apply or just apply those changes. But you don't want to do this by hand. Another thing I'll point out before I go is that the Terraform state file is very important. So if you're using local state, like in this example,
[07:11] that TF state file is really important because that's kind of like a receipt. Terraform knows that it made all those changes and each time it's going to check in with those resources to get built to find out the reality and it's going to compare that to the state file to see what needs
[07:25] to happen. So safeguard that state file. If I want to make a change later, that's easy too. Let's go back to the code. Let's say, I don't know, it's been a great month. We have $700 now or something like that.
[07:37] You just change it within the configuration, save the file. Let me save the screen one more time. And I'll do another apply. And this time, instead of a create, it's going to do a modify because we've
[07:49] made some changes. It's checking the state of reality and it's been checking the state file and saying, whoa, whoa, you asked for $700, but I only see $500. We obviously need to change that. I'm going to make that change and then go in there, and you can see it's just one second.
[08:03] It's very, very quick. It doesn't take long at all. And we'll refresh that, and we should now have a $700 budget. Hey, look at that. Awesome. So that's it. Really easy to use. You basically just build out whatever you need.
[08:15] Use the AWS CLI as your credentials, or if you're doing things on automation, you can pass some environmental variables. I'll link to a blog post that has more information on this. But happy terraforming!