Terraform Basics
[Music] welcome to terraform basics in this video we'll explore how terraform works and you'll see the workflow and learn about the code that defines terraform resources you're also going to understand what terraform can do for you now if you look at my screen here you'll see that I've got a resource which defines a Google compute instance terraform allows me to Define things like virtual machines inside of Google Cloud as code and then I can use a simple word work flow to create them so
here we've got the virtual machine defined and I'm not going to delve too much into this right now but let's see how I can create an actual VM from it if I go over to my terminal and I'm going to run the command terraform apply this is the kind of the core of this workflow now terraform is reading my configuration and it's showing me that it's going to create the resources things like service accounts the virtual machine and a few other bits and pieces now I answer yes to this and now terraform is Interac AC in with Google
cloud and creating those resources this takes just a little while as we're also going to be installing software like the NVIDIA drivers we're going to install Docker and we're going to set up our own artificial intelligence assistant using the aler API let's just wait for this to finish okay now it's complete let's take a look inside the Google console if we go over to the console here and refresh this what we can see is the virtual machine that I defined with terraform and and then let's look at the application as
well just to make sure that everything is working as expected I'm just going to open a new tab in my browser to verify yep Al Uli is launched and let me just log in using the preconfigured [Music] confidentials admin for the username and the password I'm going to grab that from terraform which was generated by the configuration paste that in there and when I log in you can see that everything's working as expected so what we've done is to use terraform to config biger virtual machine inside of gcp
which has being deployed an application has also been added to this which is our own personal AI assistant which might be useful for some of these videos okay so how do I use the docker provider with terraform and let's see what mistol has got for that so to use the docker provider with terraform we need to follow these steps and this is what
we're going to look at next so the example that you've seen right now is is fairly complex one and there will be a video which digs into this in more depth but to learn the basics and to learn the workflow we're going to take a slightly more simple option now before I go any further the first thing I want to do is to clean up after myself I'm going to use a command called terraform destroy this talks to Google cloud and basically removes everything that's defined find in the
configuration cleans up after myself I don't need to worry about anything hanging around or burning any money whilst I'm doing my experimentation to double check that those resources have actually been deleted let's back over to our Google Cloud console and verify the virtual machine isn't there brilliant as you can see completely gone so what you've just seen there is a very very basic example of how terraform works next what we're going to do is we're going to start digging into this how you can replicate for yourself and that
starts with installing terraform so the first thing you need to do is you need to install terraform now the install instructions are available here at developer. hasic corp.com and we've got the link just there to to sort of find that but there's a number of different ways that you can go about installing things you can use a package manager like Brew if you're on Mac OS you can download the binaries for Windows Mac Linux and and all of the various different flavors of
BSD or you can also use package managers for Linux and things like that what I'm going to do is I'm just going to download the raw terraform binary and I'm going to install it myself so I'm running Linux I am going to grab the amd64 version so I'm just going to copy that link so what I'm going to do is in my terminal I'm just going to curl this location uh I'm going to Output that as terraform do zip that's going to
download so now I've got that file downloaded let me just unzip it so unzip terraform do zip that gives me the terraform binary there and I need to move that into my path so I'm going to do that by moving it dot um move terraform and I'm going to put it in user local bin and user local bin is on my my path for for for my um computer
here which means that I'll be able to use that and that's all we need to do to get that installed let me just type my password in and we're all good so we can now test that out we're going to do terraform version and you can see there that I've installed terraform 1.9 if you'd like to follow along with this video you can download the example code from the link on the screen just below here inside that repository you're
going to find four folders AWS Azure gcp and basic we're not going to delve into the AWS the Azure or the gcp folders just for now we're going to focus on the basic folder so inside the basic folder there are two files one named main.tf and one name Docker TF these are really basic terraform examples that's going to allow us to create Docker containers on our local machine using terraform it's an excellent way for learning how to use terraform without having to worry about making mistakes with Cloud infrastructure
[Music] in main.tf in the basics folder what you'll see is this terraform block now the terraform block here defines which provider you're going to use in your configuration or providers now a provider interfaces between the terraform code and the cloud provider or in our example Docker so it's that translation between the HCL resource ources you can see here and an API
call so if we're looking at doer. TF you can see those so we have resource this is a keyword in terraform which defines a a thing that will be created in this instance that thing is a Docker image so that's the equivalent of us in our CLI running Docker pull so we Define the name of the image that we want to pull and that's on line four there you can see that we're going to use hash C Vault
1.2.6 then we can create another resource and the other resource is going to be a Docker container so that's equivalent of running Docker run specifying the port the name and obviously that image that we want to run now what you can see here is we're not actually directly specifying the image what we're doing is we're using a reference to another resource by using these references in this way terraform understands that it needs to perform the docker image action
before it creates the docker container there's no structure in terms of the order of the resources or a which file they're placed in terraform works all of this out through this integration and linking so let's see how with this works so what we need to do first is we need to change into that Basics folder and we need to run a command called terraform in it now terraform in it is going to
search your configuration in this current folder and it's going to download the plugins from the terraform registry that we can then use so you can see there that is all downloaded the next step in our workflow is to run terraform plan so let's see how that [Music] works so what the terraform plan command is going to do is it's going to analyze the the resources that we've got to find in our configuration so for instance
this Docker image and this Docker container and it's going to have a look at what already exists and it's going to tell us that difference so because we don't have anything that's been created terraform is going to tell us that it's going to create these two new resources so let's run the command and see that in action so [Music] terraform plan you see that there are two resources to add and nothing to change or destroy where possible in the output terraform is telling us the
values of things that are going to be created so if you look at the docker image you can see that the name is going to be set to Hash Vault 1.12 this is the image that we're going to pull now the image ID and the other details we don't know about because terraform hasn't actually gone and created them yet so it's telling us that we'll know after apply has been run you can see there in the docket container it's very much the same thing the ter the properties terraform already know about we see in the output the things that it doesn't it says known after
apply now I didn't run a plan when I showed you a basic demo before I just ran straight into the terraform apply and that was because with terraform apply it actually will show you a plan as well so let's see that in action let's run terraform apply and let's create our resources so terraform apply this is the command which will actually go ahead
and create the resources which are defined in our configuration now as a a sanity check it's always going to show you a plan you can see the changes that you're going to make and it's important to kind of check those to make sure you haven't made any unintended uh configuration changes but there's two resources there I'm happy with that so I'm going to answer yes and what's happening terraform is now going and creating those resources so it's running run that Docker pull in the background
and then after it's run the docker pull what it's going to do is it's going to create the container now that's completed so let's just do a dock a PS and you can see that we've got that container created 8 seconds ago and it exposed the port as we requested so let's just check that that's running I'm going to do a curl Local Host 8200 and this is Vault so we're going to do vault
sis health and let's just pipe that into JQ and we can see the output there so that everything is up and running everything is perfect now the volt version you can see 1.2.6 let's now have a look at how terraform can automatically update this container when we change the configuration so what I now want to do is I want to update this fault image and I want to update it to the latest version of fault so let's pop over to
the docker Hub and we can see that the latest version of Vault is 1 17 well it's let's run 1.17.1 this was pushed 8 days ago so I'm going to go back over to my configuration and I'm going to make a change here so I'm changing the version from 1.2.6 to 1.17.1 now what's going to happen is terraform understands that the current
version of the image is 1.26 it understands that the container has been deployed with 1126 and it's going to suggest the changes that need to be made in order to convert what is existing to what is defined inside of the configuration so to do that all I have to do is run that terraform apply again terraform apply so what we can see is that terraform is telling me it's going to add two resources and it's going to
destroy two resources the reason for this is that certain resources can't be updated and the docker image is one of those types of resource now terraform is also telling me that by changing the name from Vault 1126 to 17.1 it's going to force a replacement it also understands that the container needs to change and again you can see that it's being forced replaced on the container because the image has changed of which the container is
dependent so we're just going to run terraform apply terraform is going to delete the existing container that I've got running it's going to pull the new image and it's going to create the container so that'll just take a couple of seconds and we're done so let's just check that that is all running Docker PS okay we've got our new container which is up and running let's again curl that 8200 V1 sis health and then we're just going to pipe
that into JQ and again now what we can see is that we're running version 1.17.1 so terraform has enabled me to Define that configuration Define that container but also when I update it it understands how to actually go about doing that and you can see that we do only have that one container running here now what if I want to clear up after myself but what I can also run is a command called terraform
destroy so the terraform destroy command will remove any of the resources that you've created by running a PL so if I run terraform destroy you'll be able to see again I'm getting that plan output terraform is telling me what it's going to do and it's telling me that it's going to destroy two resources so that's our image and our container so I'm going to answer yes to that in the same way as
terraform with the apply uses that provider to interface to Docker and create my resources destroy does the reverse so if I do docka PS you can see that the container is gone so that workflow is as such we Define our configuration so we've created the resources that we want to create we've registered our plug-in and we've chosen the specific version of that plugin that we want to use then what we did did was we ran initialize we did a plan to check
what will be created and that the configuration is correct and finally we've applied it which created those resources to clean up after ourself you can see that now we can run that destroy step the beauty of defining this with terraform is that now this has been created every time I run apply the result is going to be pretty much the same so I can run apply and Destroy gives me a really nice kind of feed and workflow to be able to create those re
es now you understand the core workflow for terraform let's take a look at the concept around state so the first thing I want to do is I'm just going to rerun my terraform apply terraform apply and I'm going to add an additional flag here and I'm going to run auto approve an auto approve is going to answer yes automatically for me just a convenience method I wouldn't want to do this in production but it's it's just a
quick way that I can kind of do One-Stop apply so we're just creating those resources there and that's done now let's have a look at this if we look inside of the folder what we have are some files Docker main TF terraform State and terraform State backup now we've already looked at the the doer. TF and we've already looked at the main.tf but what is this state for file well the
state file is where terraform stores the details of what it's actually created and it uses this state file in order to be able to determine the changes that are going to be made between your configuration and the resources which are defined in the state so looking at this example here you can see the docker container you can see all of the various different properties that were created if I changed any of these details inside of here so for example say I change the
name to terraform vaults what terraform is going to do it's going to look at the configuration and it's going to compare the value inside of the state so for example here we've got the name and the state of terraform Basics vault in the configuration it's terraform Basics vaults so if I run a terraform plan it's going to tell me that there's one change to be made there because it's
doing that comparison and it's checking the state so state is an really really important Concept in inside of terraform so we've just looked at the state file itself now the state file was stored in my dis here because I'm using local state there's a number of different ways you can store State you can store it locally as we see here you can store it remotely inside of terraform cloud and many different places we will look at those later on as a more advanced topic but for now let's move on to see how we can actually
use some of the terraform CLI commands to interact with State we can actually inspect the state using the terraform CLI the most basic command is if I do terraform show and terraform show is going to show me exactly what's going on inside of the state now I only have two resources in this configuration but on a more complex configuration there could be hundreds if not more so using terraform show and just having that big
output dumped there is not necessarily going to be so useful but there are other commands that we can use we can use the terraform State command now terraform state has a bunch of sub commands it allows me to list the resources move items and to deal with things like remote State now at the moment we're only going to consider concern ourselves with list and show so let's have a look at this
terraform State list so we can see that we have these two items the docker container and the docker image let's inspect the containers state so again I can do terraform State show and then the name of the resource and then this way I can see only that one resource I can be very specific on what I want to to to look at
this is also going to be especially useful when you start getting to more advanced concepts with terraform and you start using things like terraform Enterprise because the state file isn't always going to be local it can be stored in a S3 bucket or in a cloud storage or inside of terraform Enterprise and in fact when you're dealing with production configurations that's the the way that we recommend that you do it but for now we don't need to worry about those things
so this has just been a very basic introduction to what terraform is and the basic workflow you've learned about providers and how they interact with the API it's that interface between your terraform configuration and things like Docker which we've seen in this example you've learned about the configuration and how writing configuration enables you to create instances such as Docker images Docker containers and in fact depending on the provider you use all kinds of cloud resources like the example you saw
the very beginning of this video you've seen how terraform detects changes by changing the configuration terraform can understand whether it can update a resource or whether it needs to replace it and you've seen how the destroy command can actually clean up after ourselves the next step is to start looking at the example and demo that I showed you at the very beginning of the video that is how do we create a virtual machine within a CL cloud and in one of the follow-up videos you can learn just
how to do that we're going to walk through step by step how you can create that example of running olama in either gcp Azure or AWS but for now thank you very much I hope you've enjoyed this video and we'll see you soon