Why You NEED To Learn Terraform | Practical Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
that took 32 seconds can you believe that that would have taken me 15 minutes to go into AWS and click around and get all of that stuff built out the VPC the subnet the route tables internet gateway Security Group the ec2 instance that would have taken me 15 minutes and this is completely repeatable what's up everybody it's Travis here from travis.media so I've been on this devops kick now for a couple of weeks I figured I'd just keep the momentum going and today we're going to talk about one of the more important Tools in devops called terraform and before we actually get into it let me tell you why you need to know it in 2023 or 2024 whatever year it is I think that's the title of my video so let me address that first so if you're looking to get into devops or site reliability engineering or cloud computing it's a requirement it's literally on every single job posting out there you might see cloud formation you might see palumi or something like that but you're always going to see terraform so if you're in those camps you're looking to get into those fields then you must learn terraform and if you're a developer you may not think this really applies to you I mean terraform is for the devops Ops guys I'm a developer I don't need that but to get ahead in this industry you need to have a basic understanding of a lot you need to know the gist of many things in the more well-rounded you are in this way the more you'll get ahead in the industry so being aware of terraform how it's used being able to use it in your discussions maybe offer to utilize it or suggest it in your day-to-day work I think is very very important second we all have side projects all devs have some kind of side project they're working on and whenever they start that side project they have to spin up resources and get things set up why not use terraform to configure all of that for you so that it's push button to get that set up and then when you're done push button to tear it back down and last infrastructures code isn't going anywhere in fact it's basically the standard now nobody does the click Ops stuff anymore it's all code the sooner you understand it the better and that's why I think you need to learn that this year even if on a surface level so in this video I'm going to talk about it tell you what it is some of its benefits and then we're going to do a practical dive into it so whatever field you're in if you're a developer if you're a devops if you're looking to get into any of that stuff by the end of this video you'll understand what it is and you'll be able to use it because I'm going to show you how so first things first what is infrastructure as code infrastructure's code is basically writing your infrastructure which is your servers your databases your networks all of that stuff writing that to version controlled scripts or code it's your entire environment and infrastructure represented in a configuration code base you can use one command build the whole thing every detail of it and use another command to tear it all down and since it's version controlled teams of Engineers can make changes to the code and update the infrastructure in one single source of Truth so in short infrastructure's code is just what it says it is it's your infrastructure represented and maintained by code second what is terraform then well terraform is an open source infrastructures code tool created by hashicorp you can Define cloud and on-premises environments in config files that are easy to read They're version controlled they can be automated and shared with one another with terraform you can easily work with major Cloud environments AWS Azure and other tools like kubernetes Docker Helm Etc and you work with these Services through third-party tools called providers so there's a whole registry of these terraform providers there's the AWS provider the Azure provider kubernetes provider the docker provider a provider for each one of these services that allows you to interact with these services and one of the best things about terraform is it's agentless you don't have to go installing agents on all your servers or databases or whatever you just use these providers these providers allow you to talk via an API to these services so terraform is an infrastructures code 2 you essentially declare what your environment should look like in terraform will easily spin it up update it or tear it down for you now before we get hands on with this let me give you some benefits of terraform itself number one it's multi-cloud by using terraform you don't get locked into a cloud provider if you're using cloud formation and you set up all these templates to Define your AWS infrastructure you're locked into AWS cloud formation is AWS only you can't take that config file and then go to Azure and do anything with it you have to go to Azure and use their tool and figure out how to deploy it all there with terraform you're not locked in like that you can build out AWS infrastructure Azure infrastructure gcp infrastructure all in one place you can deploy on all three with one configuration file so it's multi-cloud two it's stateful it allows you to track your resource changes throughout your deployment in a state file which is the single source of Truth for your environment terraform uses this state file to determine the changes that you're making to manage your infrastructure to make sure that it matches with your configuration so you make some changes to your configuration it looks at the state file sees the changes and updates accordingly so it's stateful number three it's version controlled all of your developers Engineers on a team can all work on this one code base nobody goes into AWS and starts clicking and building new servers in secret without anybody knowing guessing at different parameters it's all in one place it's in public and it's Version Control people can pull down the latest changes and add to it push it up everybody else pulls down those changes Etc next it's declarative you declare the end state of what you want your infrastructure to be in terraform gets you there you don't need to write out a bunch of steps like this happens first then do this then do this you just declare what your entire infrastructure should look like in hand it over to terraform and it will create that for you and then like I said earlier your team can just make changes to that configuration like we want to change this tag apply and it'll go and change the tag so that's number four it's declarative number five you don't have this manual click Ops like your manager says hey we need three more servers uh Travis add three more servers I gotta go in and click launch instance and then I gotta decide a name and then the instance type and all this stuff you don't have that that wastes time that as I said earlier is a secretive approach nobody really knows what you're doing and nobody knows how to recreate that number six you can save money with terraform I was on a project a couple years ago where we had this development environment that everybody was working on and it got pretty elaborate everybody was putting their stuff in there and it just got expensive and so we decided hey let's use terraform to declare this environment that we have and everybody leaves the office at like set by seven in the evening and nobody really starts until seven in the morning how about from 7 PM to 7 A.M we just destroy the whole thing and then at 7am we build it back for for everybody to work on and if anybody needs to add anything just add it to the configuration and at seven o'clock we tear it down so 12 hours a day we didn't pay for the infrastructure number seven it's great for Disaster Recovery if you can spin up your entire environment in five minutes it may not justify you having this instantly ready to go Disaster Recovery environment burning through money every day that you're not using it may not make sense to keep it running all the time in number eight with terraform you can minimize user error as humans we make mistakes we click the wrong things we add the wrong vpcs and things like that with terraform we can declare it and test it and deploy it and get it perfect so those are eight quick benefits to terraform I hope you see the importance of it now so with all of that out the way let's see how this works so like always go make you a cup of coffee I have my tea and we're going to learn how to use terraform so the first thing we need to do is install terraform if you just go to google.com and type in install terraform it'll bring you to this page and you have a couple of options you got your manual installation I'm on Mac so I just did a Homebrew so run Brew tap to install the hashicorp tap and then install terraform with this Brew install command then of course Brew update and Brew upgrade to make sure you have the latest version of hashicorp once you have that you can type in something like terraform help to make sure that it works and if you get this you've installed terraform next jump into your code editor I'm using vs code and create a new directory to do your work in I have a directory called terraform-vid for terraform video and we're ready to begin so the first thing I'm going to do is just create a new file called main.tf that's terraform a TF file is a terraform file and the first thing we want to do in this file is declare a provider remember earlier we talked about providers there are third-party tools we use to talk to different services like today we're going to use the AWS provider that provider allows us to talk to AWS and interact with AWS so my block would look something like this and basically it just says provider is AWS and it has a couple of parameters so profile is default I'll tell you about that in a minute in the region I'm using Is Us East one so we declare the provider and to actually authenticate with AWS you need to have the AWS CLI if you haven't downloaded that just go to AWS just type in AWS CLI and right here just install or updating and this tells you how to install the AWS CLI if I was on Mac OS I would just download this package file and once that's downloaded you need to get a key in secret from AWS so if I go to AWS and click on this drop down up here and go to security credentials I can generate a new pair and once that comes up just scroll down to the section that says access keys and click on create access key and click on command line interface and then click this box to confirm what you're doing and then click next and you can set a description tag I'm not I'm going to create this access key so here's my key what I want to do with my CLI is I want to make this a little bigger I want to run to configure it I want to run AWS configure and it's going to ask me for my access key so I'm going to copy this access key paste it here and then I'm going to copy the secret key and don't worry I'm going to delete this before this video airs if I don't feel free to hack me and so I'm going to paste my secret key default region name I'm going to keep it Us East one just if you don't have anything there just put in your region so Us East one and then output format I like Json so I'm going to choose Json and now if I go to CD dot AWS and then do a cat on credentials I can see I have a default profile with an access key and a secret access key this authenticates my CLI with AWS in terraform can use that also to authenticate so when you go back to the code and it says profile default that uses my default profile if I had something else here like Travis it wouldn't work and then here's my region Us East one so that's my provider block now whenever you create a new config or you pull down a already existing config that you're trying to use you need to run a command called terraform init and make sure you save the file first and what this is going to do it's going to download your provider so right here finding latest version of hashicorp Slash AWS and installing that provider so let's give that a minute to install that once that's installed we can communicate with AWS but remember anytime you have a new configuration or you're using one that you pull down from somewhere run terraform init first all right and now if you have that option enabled you'll see these hidden folders here's a terraform folder with a another folder providers if you go all the way through it you see the AWS provider has been downloaded and there's also a lock file next we have what we call the resources block so this declares an ec2 instance so the resource is AWS instance and the name of it could be anything I've called it app server it has an Ami I've put in this Ami ID which is the Amazon Linux 2 server Ami instance type is a T2 micro and I put a tag on it called my terraform instance now where do we get this AWS instance at well if we go to the terraform docs so terab let's just do terraform AWS click on this and we should be able to see all of the options for an ec2 instance so these are all the resources in an AWS provider if we scroll down and find ec2 and then we see all the resources we have an Ami we have an ec2 host ec2 tag all of these things but if we keep going down to AWS instance this is what we want it provides an ec2 instance resource this allows instances to be created updated and deleted so that's what we're using if we go back to the code we are creating an AWS instance that's in the first quotes here and the second quotes is the name of it how we can reference it in our code and I've just called that app server it could be chicken wing but app server is a little more appropriate so we've declared an ec2 instance we've declared the Ami the instance type and we've added a tag how do we now deploy that to AWS well all we do is type in terraform apply and this is going to apply the changes to AWS so let's hit enter and it would be helpful if I save this file first so let's save it and try that again and look what it does here so let me open this up it does kind of a git add subtract versioning here it gives you a rundown of all of the actions it's about to perform and if you see everything with the green plus it's going to create that so the AWS instance is going to create all of these things so you see Ami here's the Ami ID if you see known after apply this is because things usually don't get IDs and things like that until they're created so this means we don't know yet we'll only know after you apply it and run this command so we can scroll down and see everything that's going to be applied we have an EBS block device we have some metadata options a network interface those kind of things if this all looks good which you should always check this over if it looks good then down here where it says enter a value it says do you want to perform these actions we're going to say yes and we're going to deploy this ec2 instance all right apply complete so if we go to AWS now in the ec2 section of AWS and you'll see that we have our instance my terraform instance running and it should have tags tags of name my terraform instance awesome now let's look at one other thing when we did that it created a state file so if you go to terraform.tfstate you'll see the entire state of your infrastructure this is the single source of truth of what your infrastructure looks like so here's an array of resources we have a type of aw instance and all of the information about that instance so when we make another change to our configuration it's going to compare it to our state and make changes accordingly so if I want to take this name and change it to my new terraform instance and then I run terraform apply let's see what happens it's going to tell me here now now you see these yellow tilde I think it's called a tilde symbol that tells us that these resources are going to be updated in place it doesn't have to recreate anything it can update that resource in place you'll see here it's going to update the name of my terraform instance to my new terraform instance that looks good come down here and type yes and it will make that change so you'll see and you'll see here modifying modifications complete after one second if you go to AWS and refresh I should have the new name yep my new terraform instance pretty cool now what if I want to get rid of this instance I can run terraform destroy and when I do that it's going to tell me what's about to go down see all of these red minuses this means all of this will be removed if I like it type in yes and my ec2 instance will be destroyed after about 30 seconds my destroy is complete if I look at AWS refresh it should be terminated yep instance is terminated but now imagine that you have a lot of resources like you have 15 instances a couple of vpcs this really doesn't scale because if you want to change something you got to search through the whole thing change values everything's hard-coded we don't want hard-coded hard-coded doesn't scale so what can we do about that well let's create a new file called variables.tf and this really could be named anything because terraform looks at each file each terraform file when it runs and it'll see these variables but we're just going to call it variables.tf to make sense and I'm going to paste some variables here so we declare each one with this variable declaration and we're going to call this one instance name we have a description a value of the name tag for the ec2 instance type a string in default is my new instance we have another variable ec2 instance type description AWS ec2 instance type type a string and default is T2 micro now that we have these variables we can go back to our main file and where we see T2 micro we can do VAR dot ec2 instance type to use that variable here and for name we can do VAR dot instance instance name and save that and if we run terraform apply it should run fine hmm just like it did before let me type yes here to confirm and that ran fine because we have default values for our variables now what if we want to declare it to be something else not the default So currently the name is because of a new variable my new instance what if we want to make that something else we don't want it to be the default well when we run terraform apply we can add vars to it so we can do Dash VAR and in quotes here we can say instance name which is this variable equals my new name ec2 we can run that and it overrides the default by declaring variables so you can do terraform apply Dash VAR this Dash bar that and declare all of the bars you need to set these variables if I hit yes it should make the modification and that should now be changed to my new name ec2 but you see the problem here already that doesn't scale either if we have 100 variables I don't want to write Dash VAR something equals something 100 times so what we can do now to make it scalable we can create a file called terraform dot TF vars now look how cool this is here I can just put ec2 instance type equals to do micro instance name equals my instance name from file just some other name and what I can do is in my variables I have variables declared right in my main file I'm using these variables well in this terraform.tf vars file I can just do easy to instance type equals this instance name equals this and I can have my Hundred variables here and this will override the defaults so when you need to update your terraform configuration you have this file which will have all of your variables in it with their values and you can just update the values here in one file pretty cool so if I want to change this to T3 micro this is actually going to have to recreate a new instance I think so if I do terraform apply and I don't have to put any VAR parameters on my command here so let me hit enter and it's going to read this tfvars file and it's going to see what my configuration is and actually it doesn't have to create a new one it can update it in place so let's put yes and see what happens apply is complete after 43 seconds let's go back and refresh and I should have a T3 micro now yep T3 micro so I hope that made sense to you I have the main file where I'm declaring the resources and then I have this variables file where I'm declaring variables I'm giving it a variable name to where I can assign that variable name some value and then I have this tfbars file that's just going to be my key value key value all the way down this is where I'm going to change all of my values all of my configuration values now another important thing for terraform is outputs so let's create a new file called output.tf and a lot of times when you run this you want it to Output some data like if you're going to run this in a pipeline and you need to know the public IP address so that you can ping it and see if it's online or something like that and you need it to Output some data you can use outputs so let me paste this here and we're going to have two outputs here the first output this is the output declaration is the first one is going to be instance ID this is going to be the ID of the ec2 instance and the value here is AWS instance dot appserver dot ID now what is all of this well if we go back to our main the AWS instance is the resource so AWS instance Dot and then the resource name app server and then the ID of that resource and you can see all of that in the documentation if we scroll down here we should see ID somewhere and you'll see it here in the examples like for vpcs you have AWS underscore VPC which is the resource then you have my VPC which is the resource name that's what you named it dot ID and that's to get the ID so what we're doing here is every time this runs we're going to Output the ID of the ec2 instance and the public IP address of that ec2 instance so here it's AWS instance dot app server same thing dot public IP so to make this work you have to do another terraform apply and changes to outputs let's do yes and you'll see outputs instance ID is this an instance public IP is this and anytime you want to see it without applying you can do terraform output and it'll show you the outputs and that data is actually being queried so that's querying data with outputs now I want to jump to a more realistic example so this was just to show you some of the features of terraform let me show you kind of a real world scenario so let's kill this first let's do a terraform destroy to get rid of this so we don't get charged for that yes to destroy it and while that's destroying let me pull this up so this is a more realistic example so here we have kind of the same thing set up we have our main file we have some outputs same outputs we have variables we have one two we have a handful of variables we've got a VPC cider range VPC name subnet side arrange subnet name internet gateway name ec2 Ami and ec2 name and then we have the VAR the tfvars file where we can just declare overrides here and this is actually going to deploy an AWS environment so if we look at the main file and if you don't know what some of this stuff is a couple of weeks ago I did an AWS networking video where I explain all of the basic networking in AWS you might want to go check that out it was really helpful for a lot of people and it'll help you understand what's going on here I'll put a link to it above but here we have the VPC we have the subnet for that VPC you'll see that it's associated with the VPC here by supplying the VPC ID we have a internet gateway that's our route to the internet we create a route table with the internet gateway route we associate that route table with the subnet we create a new Security Group open to http traffic and we create an ec2 instance and here you see we have a little more info we have an Ami an instance type we have the subnet ID we have the security group ID we declare a public IP address for it or we associate a public address to it and then we have some user data with ec2 instances you can include user data that runs when the instance spins up so this is actually going to install an nginx server and print an H1 message on the page and then finally we have some tags so we have the whole thing here we have the whole networking the route table the security group and the ec2 instance so if I'm going to run this and this is a lot to do manually if you have to do this manually it's going to be at least 15 to 20 minutes to click everything and wait for everything to spin up let's see how long it takes with terraform so terraform apply seven resources to add if you look at this a lot of stuff is being added this manually would take a long time let's type yes and see how long it takes that took 32 seconds can you believe that that would have taken me 15 minutes to go into AWS and click around and get all of that stuff built out the VPC the subnet the route tables the internet gateway Security Group the ec2 instance that would have taken me 15 minutes and this is completely repeatable let's say I need to do some work in AWS I can spin this up in 30 seconds do all of my work destroy it a couple of days later need to do some more work spin it all up do my work destroy it 30 seconds up 30 seconds down pretty good so let's take a look at what this built in AWS so we have our vpcs I should have two Now the default and then the one I just built here's my test VPC here's my test subnet my route table with an internet gateway route my Subnet Association here and if I go to my ec2 instance Let me refresh click on my test ec2 and if I click on this public address I should have an nginx server running and let me actually copy this and try on a different browser to http and this is my new server my nginx server is running all in 32 seconds that all got spun up we open this terraform destroy there it is that's like 35 seconds 32 seconds to spin it up do my work 35 seconds to spin it down I don't have to go in AWS and click around in waste time that's the power of terraform if you're in devops you're an SRE cloud computing you gotta know this this is the industry standard if you're a developer this will seriously help your career out and help you out with your side projects in your development day-to-day I hope you found this helpful if so give it a thumbs up consider subscribing and I'll see you in the next video [Music]
Info
Channel: Travis Media
Views: 99,073
Rating: undefined out of 5
Keywords: terraform tutorial, infrastructure as code, terraform tutorial for beginners, hashicorp terraform, devops tutorial, terraform aws, terraform explained, terraform basics, devops terraform, terraform aws tutorial, what is terraform, how terraform works, introduction to terraform, terraform tutorial for begginers, aws terraform tutorial, you need to learn terraform, how to learn terraform, terraform in 15 minutes, terrform for beginners
Id: nvNqfgojocs
Channel Id: undefined
Length: 27min 32sec (1652 seconds)
Published: Sun Feb 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.