Beginners Tutorial to Terraform with AWS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 within aws using terraform to build and maintain it so let's get started i have a visual studio code editor here it's one of my favorites and we're 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 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 i think i'm running yeah 204 6 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 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 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 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 give credentials to terraform so aws configure it's going to need the access key we just made that so i'm gonna paste it in the secret key it's back on the other screen grab that and let's do this in usd one 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 and you can actually see that if i go to the dot aws folder within my user profile you'll see these two files that get created if they're not there already let's 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 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 some terraform code there's lots of ways you can do terraform code 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 gonna just make one file or we're gonna make it right in the root of the code directory called main.tf and we're gonna write the code here we're gonna be working with local state we're gonna be doing some very simple commands it's 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 so i've got the first block of code is 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 providers i in this case i want to work with aws so i need aws as 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 stanza here source is where is it in this case it's on hashicorp 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 letting terraform know that it needs to grab the aws provider additionally we're going 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 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 and there it is so i've said build a resource that resource type is an aws budgets budget because it's in the budgets group and then the contextual name that i've 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 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 that's going to go grab the brighter we we kind of need that to do anything and if you accidentally forget to do an init it will tell you it'll say hey you need to init first cool we're initialized the folder that contains our code is now ready to be run i'm gonna do two housekeeping items first though the first is terraform format or fmt it's going to clean up the code i can actually show you what that looks like let's say i had like too many spaces here these were kind of off center that's not considered proper formatting so if i run this command again you can see it fixed all those little imperfections in there and 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 terraform validate 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 make sure that i have the required information things like that so we've initialized we format it and we validated 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 an 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 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 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 i'm just going to go ahead to terraform apply i could 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 that looks good however before i click that button let's switch back over and look at the budgets just to be clear i refresh the page there's no budgets here there's no smoke and mirrors so i'm gonna go back and say yeah i do want you to do that so the provider is going to create that translation between the configuration we just wrote in terraform and what needs to happen in aws and it took one second it's really quick so i'll go back to budgets click refresh and there it is so it's just that simple at this point if you need to make a 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 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 that get built to find out the reality and it's going to compare that to the state file to see what needs 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 great month we have seven hundred dollars and something like that you just change it within the configuration save the file i'm gonna clear 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 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 so i'm gonna make that change and then go in there and you can see all right it's just one second it's very very quick it doesn't take long at all and we'll refresh that 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 use the aws cli as your credentials or if you're doing things on automation you can pass with environmental variables i'll link to a blog post that has more information on this but happy terraforming me you
Info
Channel: Wahl Network
Views: 11,863
Rating: undefined out of 5
Keywords: tutorials, how to, guide, technology, tech, terraform, aws, tutorial
Id: XxTcw7UTues
Channel Id: undefined
Length: 8min 45sec (525 seconds)
Published: Wed Sep 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.