Getting Started With Terraform | Terraform Tutorial | #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back so let's get started to create our first code to create our infrastructure so this video will take you from beginning to end to create a basic basic AWS ec2 instance I want to cover a lot of things that terraform does in this video so this video might be a little bit longer than some of the other videos just cuz I want to touch base on a lot of different things to get to get a good oak so you can have a good overview of everything and then in later videos I will go over everything in more detail on everything that that terraform has to offer so first thing is terraform has its own language called terraform language which is based on HCL so what we'll do the first thing we want to do is every terraform has its own configuration file so I just have a directory here course which is empty so let's make a file so you notice I'm gonna name it TUC main TF in most of the time you'll have all your configuration files named main TF and the dot TF is terraformed specific you may end up having other files like variables and different things like that but for the most part you're always gonna have a main dot TF and it could be named anything you want it could just be food at TF terraform will recognize it but it's good practice to name your like main configuration file main TF so one thing is like I'm using vs code so if you're not familiar familiar with vs code vs code of just a nice editor to use for code it's very popular for JavaScript and Python and everything like that so so that's what I am using here and I have an extension installed for tariff 1 specifically so I get syntax highlighting and different things like that so you might not get cent tax highlight highlighting out of the box so if you're nvs code and you go to the extensions pane here search for terraform and i already have it installed but i will give a link to this and in the description but what you want to do is just just install the terraform Hoshi Corp is what is the parent company of terraform so this is this is their specific extension which is nice so that's the most popular one you see house it almost six hundred thousand downloads so just install that that'll give you a lot of syntax highlighting and extra benefits now let's go back to our code let's clear that out alright so we have a blank main TF file and these files that to you the terraform language is just like any other language it could be like a JavaScript or Python or anything like that it's just terrible and specific language and then it just compiled down to whatever they can read all right so what we want to do is we want to create a ec2 instance in AWS so normally if you want to create an instance in AWS you might go to your like I'm assuming everybody's familiar with with AWS and Azure and all that so you have a background in cloud computing and all that so that aside so let's say we're gonna go and create ec2 here you know so go to instances and we need to you know let's launch the instance here you know so this works fine so what we're going here we just click it well we don't want to do that we want we want to do this by writing code so if the first thing is there are providers in terraform and what the provider is is like a AWS or asher or Google Google Cloud or it can be github it can be get lab there's a bunch of different providers that that terraform offer offers the main ones that I use and that a lot of people use I'm just gonna be AWS as you're in Google Google Cloud so those who that's what we're mainly going going off of and every provider has a bunch of different resources with it so the AWS provider you know has its own way to create ec2 instances where as the azure provider has its own way to create virtual machines as well so let's let's take a look at the AWS provider since that's what we're gonna go through if we look through this list we have AWS and there's some basic basic code for that so you can see the the provider here you know every one of your your main TF so we have our main TF file here it's always gonna have a provider block and you can have one provider block or you could have five it kind of depends on what you're doing but for the most part you'll have one or two most of the time just one it just kind of depends and you can see our provider here we have a provider block here and then inside of that instead of this string is AWS so that provider eight this AWS corresponds to you know our AWS provider if we were using a different provider like Asher is it's actually a RM which would be the provider here so you stick that right in between the strings here and then you also have a version block the version block is not required but it's something you always kind of want to have because else if you don't your your provider version could change as you go and then your your code might not work so it's good to always specify that and then for AWS specifically you have a region so as you know like when you create an instance you have you know in the top right here you have your you know two different regions that you select for them so if I'm in US West organ right now so when I create this instance I'm gonna be in US West for Oregon so for the provider here we're specifying the region here for this is specifying the region here of us east one and there are some other options that you can go into in the provider some of them are like authentication so there's a few different ways you can actually authenticate so so when you actually need to create resources like a virtual machine or in this case here a a B PC you need to be able to authenticate to your AWS account or your Azure account so there's a few different ways you can do that with AWS the one that I use a lot and definitely I'm developing locally is I use the AWS environment variables so just like if you use a AWS CLI which a lot of people are probably familiar with you can it'll lead from your AWS access key ID so an example here yeah so it'll you can have it automatically read from it'll automatically look for this if you want so your access key ID and your access key secret and that will validate you to be able to use to create resources inside of AWS that's what I mainly use for local development and everything does also know you can provide it here and there in the in the provider here this is not the best way to authenticate because you know you can see a warning here you know your your your credentials can get leaked so for the most part for now just just use the environment availables won't just be will be just fine you can also set a default region if you want and then there's also a shared content cadential x' file as well i'll let you all read up on the different ways to authenticate if you want that's that so the next thing we have so you mainly you have the provider block and every configuration so let's let's let's create a provider block here let's see the provider AWS so we're using AWS so the AWS is is the provider here and then you have these the curly braces these are what provide the configuration instead of this this provider so what do we have we had a version that we could do and I think the current version is two point six five if I'm not mistaken for for what I'm doing so we're gonna add the version and then let's see let's do the region so with AWS you always need the region in there unless you are su hard-code the environment of fo region but we're gonna do let's do US West too so we'll do US west too and you can see the region here US west too so it's gonna be what's what's in this right right side of the the region's here all right so good so that's that's our provider so every configuration is always gonna have a provider here if we were using Azure our provider would be as your RM which as your RM means as your resource manager and then inside the curly braces we would have our details like like these I'm gonna go over Azure in one video in it and I'm gonna go over as you're in the configuration compared to AWS and video in the future so look for that but for now we're just gonna do AWS yeah but you could have you could have you know this provider here and you could have a sure you can have both of these providers in the same config so maybe you want to create a virtual machine and Azure and an ec2 instance and AWS you can do that just fine so that's where it's nice to have terraform you can just do it all in one file versus if you were to use you know cloud formation or as your resource manager arm you would have to create two different configurations for all that whereas terraforming is do it in the one but we're just gonna keep it simple for this and just do one provider are the next thing we have is resources so every provider has a bunch of different resources and these resources are like your virtual machines or your your VP sees your subnets your security groups and different things like that so if you see in this example here this has a BBC so this VP see this creates a virtual private network for AWS and this is a resource that belongs to AWS and evie 8 and resources into find with the resource block here so you have a resource and then the resource name and then you have a a name here so you see this as an example this this name here can be anything you want it can just be fubar it's whatever you want to have it called in your file this this the value here does not get uploaded to your cloud provider at all it's only me it's only for Tara forms purposes for when it's stores state and I'll get into state a little bit later but this can be anything it just has to be unique so if you have AWS VPC example you can't have another you you have if you want to have another VPC resource block you'd have to name it like AWS VPC fubar or example 2 you can't name the same resource with the same name here and there's literally hundreds of different resources depending on what provider to use and to keep it simple what I'm going to do is I'm going to create a basic ec2 instance with with AWS an AWS if you could the ec2 instance from terraform it automatically will create a attached to your default virtual private cloud and all that so we're not gonna create that but let's just create a basic easy to so so most the time when I'm working with terraform I Google this all the tightest goggle it so like I need to create a ec2 instance so what I usually do is I will Google terraform AWS ec2 and most of time what comes up is going to be the correct thing so if you look that there's there's Doc's here let me just make sure I'm on the right one now yeah I'm on the right one all right so here's here's a resource every resource has a documentation in the terraform Doc's that kind of goes over everything that the resource can do so here again you have your provider then you have you have this this data thing which we haven't gone over yet I'm not gonna go over that yet so ignore that for now but then we have our resource here our resource block this is what we're looking for so this is what we need to create our resource so if you lay the description you see provide an ec2 instance resource this allows instances to be created updated and deleted all right so let's let's start creating that but first let's look at the argument so if you look at this page there's arguments so there's some that are required and some that are optional so like when you go through the interface and you want to you know create an instance it's always going to ask you for like what you want to create and all the details of your what you're creating and the first thing you always have to select is you have to select the the ami so it allows you to select that so that's one of the required parameters of this resource ami it's required the ami for the resource so if you look here it's specifying the am I here another thing that's required at the instance type I believe is also required if we go down yeah so you have instant type here the instance type is is the size of the instance so do you want t to micro t to large m5 large it kind of just depends on what you want and these are the way we have here these are the very basics that we need to create create this instance so let's let's go into our curtain and do this so if you look here this is the resource name this is the this is a unique identifier for this you know so when we want to create a AWS ec2 instance this is what we need so let's go in our code and let's let's create that resource that's AWS and if you notice the providers you know the resource name is always gonna be prefixed with the provider underscore so we have AWS that's always gonna be AWS underscore something in our case is gonna be instance and then we just need to meet we need a name here and this is just for Terra phones purposes so we can name this fubar if we want and then we have a block and then inside these curly braces are our configuration so first argument or our first configuration let's do our AMI so you always have like the configuration the argument here which is AMI and then it equals something so what we need to do when you have specify an ami so there's different ways you can do to get an ami so for this video we're just gonna do a very basic and we're gonna grab the Nami from our interface so let's go back to here and we'll use we're going to use Ubuntu so if you look here it has the ami ID here and this is what we're gonna use we're gonna copy this if you wanted to use Red Hat you could copy this or Adam and what and since you have you can you determine what you want we do that so then we're gonna use double double quotes here so any double quotes here to enclose that since this is technically a string it needs to be enclosed in double quotes if we had a different configuration that was like an integer or number we wouldn't need the double quotes but since this is a string it needs to have the double quotes all right so we have that so we have our ami so now we need our instance type here so let's do instance type again this is gonna be a string and the instance type needs to be like this the size of the the instance so let's let's just walk through the interface so let's select here and so we've selected our ami in the previous screen now we need to specify like what type of instance do we want you know so we can do a general any of these so you know let's just say we wanted a tea to medium so usually in the interface you're just selecting here and go next well all we got to do here is Ooty to medium so we have it there perfect so this is the the bare minimum that we actually need to be able to create this instance and this this assumes that you already have a VPC set up virtual private cloud through AWS and what this will do is this will actually just attached to that VPC by default but most of the time you'll either create a VPC or you'll you'll specify a bpc or a subnet in this which we'll get into in a couple of videos but for this i just wanna i wanna be able to create this and kind of show you so now this is all this is all we need to create the instance so another thing with terraform is maybe we wanted to do a comment so you can do a comment in a few different ways you can do a comment like that if you needed to let's just say like we had some notes you know you can do a comment like this like in some other languages you know so everything inside this the /and Astra's is a comment until you get to this last asterisks and forward slash so things are comment there you can also do comments through with a pound sign here so you can see all this is this commented out now so that's kind of the basics on how you do comments and terrifying all right so now so we've got our configuration file setup and and we're ready to create our our resource so how do we do that alright so the way we're gonna do that is through the terraform CLI so we have the terraform the terraform command installed so what we're gonna do is we're gonna execute well we'll use the terraform clan command to create or a resource so we have the terraform command installed and we can verify this by doing terraform version so good we do have an install so we can pass different arguments to this to the terraform command to do different things and the first thing we want to do when we're creating new projects or resources is we run the terraform and knit command tear for minute and what knit does is it basically will it'll look at like the provider blocks and it will download the providers so in this case it's gonna download the AWS provider and it'll also do a few other things like it'll download do you think it'll download state if it needs to you don't really need to worry about that now but let's just do tear home in it and you only have to do this once most of the time so you just run the terraformer net one time and then you want to do it again alright so now we have the ada best provider everything's initialized so now now we can actually start creating our resources so let me clear this out okay so the main command that you're gonna be using all the time is gonna be terraform there's gonna be tariffs form plan terraform apply and terraform terraform destroy so let's run terraform plan all right so what terraform plan does is it basically just gives you a dry run of what is gonna happen when you want to when you're gonna create your your resources so plan doesn't actually do anything to AWS so when you do plan it's not gonna do anything it's not gonna actually hit the cloud and do anything all it does is just kind of give you an overview of what its gonna do you know so you've written you say you've written the code here and you know you just gotta kind of see what is it actually gonna do so you come here and you can see you know before you actually want to commit your changes to the cloud provider you can see what it's gonna do so you can see here it's kind of looks like a code diff if you if you look at it so you have an ami you know and then it tells you a bunch of stuff so it's we're basically it's just saying we have a bunch of green pluses so basically we're just creating a bunch of or since every every detail is a creation so you don't see any minuses or anything like that which you'll see and a little bit later but you can see it's got T to medium and you've got a lot of things that say known after apply you know so once it's not gonna know these until it actually creates the the ends the instances of the resources so terraform plan is nice you know to just run and kind of see what it what are your change is expected to do you can see here it says plan one to add 0 to change 0 to destroy so here all we're doing is adding one resource here alright so if you look at if you do plan and everything looks good you know so we're ready to actually create it so the way to actually create it is form apply so let's run that okay so when you notice telephone play you kind of have the same output from plan so what terraform apply actually does is it it runs a plan first so it runs terraform plan first you know so before you before it does the apply it does the plan and then it ask you are you sure you want to perform these actions and you can either say yes or no so we're just gonna verify again everything looks good so we're gonna do yes so before we click yes if we look I don't have any instances created here so there's nothing here at all but I click I'm gonna hit enter yes as long as we filled out everything correctly this is now going to start creating our instance a resource which is an AWS instance into the cloud so this depend on how much how many resources you're creating this could take a couple of minutes it could take 20 seconds could take five minutes it just kind of depends on how much you're creating okay so now it's created so you can see apply complete resources one added zero changed and zero destroyed so it added one resource which is our instance so if we come back and we refresh we should see an instance and we do we have our instance here and still initializing but this is the instance we created so that's pretty cool so you can see it's a t-to medium you know we did T to medium and it created it from our am I here and it's in u.s. West too so Oregon is us West too so perfect so we have our instance created so that's that's pretty cool so let's go back here so now if we look at our directory structure if you look actually here you see it added a couple of files automatically when we did that apply or the init the Annette when we did Tara for magnetic created the dot terraform directory but when we actually did terraform apply it created this terraformed TF state direct file and you don't need to really worry about this you can kind of ignore this file completely but the way terraform works is it stores State so terraform needs a way to know like what's created what wasn't created and different things like that so like if we change this to tea to micro you know terraform needs to know that that's going from a micro to or going from a tea to medium to a teaching micro so one way it's able to do that is by storing everything that it has done in this state file so that's what terraform uses internally to store state so you don't really need to worry about this too much right now but that's kind of how you might hear me talking about State a lot and that's what this terraform state is is because that's how it knows what it needs to actually do when you do an apply or a plan or a destroy which we'll get to right now so perfect so now we've got an instance created so we're kind of done with this like we've created our instance we know what providers are now we know what resources are so now we're done we're done with it we just kind of like we've tried it out we've tested it so let's let's now let's remove this and since so a lot of times instead of going going to the interface and like clicking innocent State terminate since this is all so that state is now under control of terraform you know so if we wanted to we could we could delete this in the interface here and it would work just fine but since we have this under terraform control what we can do is we can run terraform destroy and what that will do is that will just get rid of that instance completely and what you'll see here is you'll see it doesn't do it automatically you have to do terraform destroy and then it's gonna prompt you to make sure that you actually want to destroy the resources so you can see here it's all minus signs where before it was all plus signs so this is basically it's going to destroy everything that we have in this file it's gonna destroy it and if you had other instances so like if I manually created a new instance here and like the name is fubar or I had ten other instances here when you do destroy it it's not gonna destroy those so don't worry about that I know when I was first starting I got a little nervous I'm like I'm I'm creating this stuff and I'm gonna do a destroy and I'm like is that gonna delete everything in my AWS account and it's like no it won't terraform knows to do that based on this state this state file so it's only gonna do things that are tracked through terraform so it's not gonna you have to worry about it touching any of your AWS resources but here we can verify it's gonna be removing everything and we can click yes and we're good to go and this sometimes could take a minute it could take you know a few seconds it kind of just depends on how many resources you have okay now after you know 30 seconds 31 seconds it's it's destroyed so now if we look and we go back here you see it says running if we refresh the page I should say like terminating or terminating so now is this is terminated so now that instance does not exist anymore on a system so it's gone perfect so now we've created a provider AWS we've created a resource we've gone over you know our our terraform language syntax and all of that we've gone over creating files we've downloaded the correct vs code extensions to get the terraform syntax and we've destroyed our resources as well with terraform destroy so that's that's the basics and the starting point of a lot of things and that's kind of all we're gonna go over today in the next video we're gonna go over updating the infrastructure so maybe we wanted to you know we're gonna change this from a tee to micro or we did it have we had that teaching medium so maybe we want to put it at a large you know we'll show you how to do that it's very simple but there's just some things you want to look out for when you do do it so we'll go over that so make sure you hit the subscribe button below so you don't miss out on the next video and I will see you in the next video
Info
Channel: Will Brock
Views: 19,437
Rating: undefined out of 5
Keywords: Terraform, Ubuntu, Tutorial, AWS, Cloud, Azure, Servers, Infrastructure as Code, Linux, Resources, Providers, State, vscode, Beginner, ARM, CloudFormation, VM, Virtual Machine, IaC
Id: t7XUh0xyhTI
Channel Id: undefined
Length: 30min 49sec (1849 seconds)
Published: Sun Jun 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.