Day 2: Master HCL syntax and Learn how to write Terraform Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
HCL or hashicorp configuration language is used to create terraform configuration files in this video I'll be teaching you a CL right from the scratch it's very important for you to understand HCL and its syntax to learn data form faster so watch this video till the end let's start Welcome to Cloud champ we are here on the computer screen and in this video we are going to learn about HCL or hashicorp configuration language that we use to write our terraform scripts this is the second video in our terraform playlist if you haven't checked out please check out the data from playlist to learn terraform from scratch in this video I'll be teaching you everything about HCL starting with data types in HCL what are blocks in HCL what do you mean by attributes in HCL what what are functions and how to write them how to write conditional statements in HCL also what is resource dependency and so much more in this video is going to be very very important foundationals video to help you understand the complex topics we are going to learn in the further sessions so make sure you watch the video till the end and also practice along with me also if you want to learn more about HCL you can check out the official documentation by hashicop and their official repository as well the links are going to be in the description but this video is all you need to learn HCL from scratch so let's start with learning it I'm going to open my terminal and create a new folder to learn HCL so let's learn and open this in vs code for you to practice it make sure you have vs code installed I have it already installed so here's my vs code and let's create a file name it main.ts remember every terraform file should end with DOT TF extension and we are not creating any resource or anything as of now but still we are going to name it main.tf to make sure we follow the practice now I'm going to add a comment saying learning HCL with Cloud chat this is how you are a comment in HCL then if you want to add a multi-line comment you need to do it through slash and a star symbol and you can see this is a multi line comment and you can close this by star and slash so this is how you add a comment in HCL today we are going to learn about what are blocks so blocks in HCL and there are different types of blocks that we are going to look into next is next is attributes so what do you mean by attributes and what are they after that we're able to learn about data types so different data types like string numbers Boolean Maps list Etc then you have to look into conditions so what are different conditions and how to create conditional statements in HCL what are functions in HCL and how to write them lastly we're going to see the resource dependency and more things so this video is going to cover few more things that are not there in the documentation so make sure you follow the video till the end and let's start with what is vlog so if you look at the official documentation and try to understand what is block according to HCL a block is a container for other content and this is an example here so it's a resource block and there's a network interface block inside resource block so a resource block has some arguments and then it starts with curly brackets to help you give you a simple lesson so here's a simple syntax of block block is going to have a block type and then attribute and value there are many different block types in in terraform there's a provider block a resource block data block variable block output block and so much more so depending on the Block type you might have parameters in front of it or you might not but there will be attributes and value here so what do you mean by attributes so attributes are key value pair there's going to be a key and a value so key equals to value so let's try to understand attributes and block with an example of creating an instance so here is the terraform code to create an instance so this is a resource block and it has a resource type known as AWS instance there is attribute named as Ami and this is the value of the attribute similarly this attribute nameless instance type and the name of the value of the attribute let's try to add some more attribute so you can add attribute named as count saying 3 which means I need to have three three instances and we can also add enabled equals to true so now there are four different attributes here Ami instance type count and enabled and if you look at the different values this is the value which which is enclosed in double quotes because it is in string this is also in string so it's in double quotes this is a number so it is not a double quotes and this is a Boolean value so it's not in double quotes which means there are different types in HCL so there's string which should be always in double quotes this number then you have Boolean which can be true or false and you also have list and Maps so let's look more into list and Maps now so what do you mean by list list means list of something it can be list of security groups it can be list of subnets it can be list of servers databases and so much more so list is always defined in square brackets this is what you will Define and it will have items inside it so item one comma item two comma item three so to give you an example you can have a scenario where you need to attach multiple security groups to an instance so it can be list of security groups it can be security underscore groups equals to in square brackets it will be SG hyphen something then comma SG hyphen something again okay similarly you can add a list of subnets and so much more indifferent the resource type now let's look at what are maps in HCL so what are Mac what is map data type in HCL in HCL maps are defined using curly brackets and they follow the key value syntax similar to dictionaries in Python and other programming language so you can see here we have a variable block defined with map as a data type and here is the key value pair this key value pair can be of different data type it can be a string it can also have a number in it it can also have Boolean types in it to give to provide with another example let's try to add the details about John and here is the name of the John name equals to John do age equals to 30 is admin equals to two so this is what maps are and if you want to get the value of any of these things here let's say you want to get the age of John how can we get it we can get it by locals dot my underscore map and inside square brackets we can Define the term we want to get so we want to get the age so we're going to say h this is how you can this is how you can get the value of particular attribute inside map next let's go and check out condition statement conditions in HCL are very important to make decisions or to execute a certain block of code and there can be various scenarios where you might be using condition uh some the first scenario would be different environments let's say you have production and staging and development environment and you need to create different resources let's say in production you wanted to launch instance type in development you want you to small uh in staging you might need T2 micro or something like that so you can use condition saying that if the environment is test then create this instance another example would be you can also have conditions on certain criterias if the feature is enabled then do this for example if the database is public don't connect it to ec2 but if it is private then connect it to ec2 that then you can Define this in the form of condition also you can say that if the CPU of my first instance is 50 then launch another then launch another server so to demonstrate this let's try to create a resource here so I'm going to say resource of AWS underscore instance so AWS instance and we are going to use the example of the environment so let's create a server and inside this I'm going to say instance type so instance type can be generally T to micro or it can be instance type can be T2 micro or T2 small but here we want to have a condition saying that if the development if the environment is development then it should be T2 micro if it is not development then it should be T2 small so to Define this in condition we are going to define the value of the environment in a variable we are not creating it but this is how you will be doing it so variable of environment is development in double quotes development then start the condition saying the study condition will start with this question mark saying if the value of environment is development then I should be using T2 micro but if the value is not this then I should be using 2 to small so if the value here in the environment is development then you will be creating an instance which is T2 micro and if it is production or staging it will be creating T2 small this is how you will create conditions uh I hope this makes sense if you have any questions regarding conditions let me know in the comment section and let's move to the next thing which is functions in HCL functions in HCL are very helpful that can help you perform operations calculations and Transformations within your code and HCL provides you with so many different functions that can use use to manipulate data and validate inputs and so much more let's have a look at all the different functions that I'd see at CL has so here you can see this built-in function uh this is the max function to check out the maximum volume on all these three so the maximum value is 12 here but there are also more functions for numeric functions you have Max minimum pass and floor all these things first string you have charm Pence width format join upward lower all this stuff you also have functions for collections for encoding and lot more along with this if you want to have a custom function you can also create it using modules let's try to create to show you this with Hands-On or with a practical use case let's try to demonstrate this using a local block so in this example I will try to create a value for a person named as John and I would create a message using all these different functions saying hello hello John I I know you like mangoes apples and bananas something like this so let's try to create an attribute named as name and the value would be John Cena John Cena and here I would include Goods list of fruits so fruits equals to in square brackets I'm going to say apple I'm sure Johnson also loves bananas I'm going to say banana and who doesn't loves mangoes I'm going to say mango as well okay now I have two three fruits here let's create a message using all the different functions that you can use for string so I'm going to say message equals to hello and I want John Cena to be all in capital so I'm going to say uppercase using this you can add functions or you can also this is known as interpolation so it's going to be in dollar sign and curly brackets inside this I'm going to say upper and I want to have this in capital so the name which is inside locals so I'm going to say uh with double this local dot name and I'm going to say hello John Cena I know you like apples banana and mangoes I want to All Join all of these in one sentence I'm going to say this dollar symbol again with curly brackets and inside this I'm going to say join and in this I'm going to say join this with a comma Sim so in double quotes I'm going to say comma I want to join all the fruits so I'm going to say local dot fruits so once this is executed and we the value would be something like this so this hello John Cena I know you like apple comma banana comma mango this is what I output would be when we use upper which is going to format everything in capital letters and join which will join all these things in one sentence so you can check the uh more about upper and join here so let's let me show you the join function in the string so here we have string and here's a joint function you can see this is how you can join it so here the separator is a hyphen we used this separator and we have all the values defined in the local so we are not using this this is how you can use different functions which can help you modify the data in your code and create Dynamic messages as well now let's let's talk about dependencies now let's move to the next topic which is resource dependency in HCL resource dependency in HCL is very important and you might be using this every time resource dependencies are usually used to create relationship between two different resources and also to define the order on how they should be created and there are two types of dependency implicit dependency and explicit dependency implicit dependency is done automatically by terraform and you don't need to do it but if you want to explicit Define that there's a dependency between two resources you can use depends on wet argument which you'll be learning later on so to help you with an example let's say you have instance and a security group created separately but you want to use that security group with this instance so this instance depends on this Security Group so let's try to show this with an example I'm going to create two resources one resource for AWS underscore instance and another resource for Security Group so I'm going to say AWS underscore security underscore group okay so in this instance I'm going to Define all the different parameters like Ami and all this stuff but the main one is the security group ID so I'm going to say VPC Security Group ID equals to this year inside this I have defined inbound and outbound rules whichever I want so I'm not going to Define that but I'll show you how you can use this Security Group inside this instance even though they are separate resources so to use this you need to say AWS underscore Security Group which is the name of the instance here or which is the name of the resource your resource type so I can also copy it and paste it here dot this is the name so I can I can copy this like this can be anything let's change the name to my Security Group so instead of this I'm going to say my SG and Dot ID because we need to add ID here this is how you can create dependencies resource dependency implicitly uh without using any depends on bit argument and this will know that I'm going to use Security Group here inside this instance similarly you can attach this Security Group to multiple instances as well and if you want to put let's say another use case can be you're creating a VPC or you can I can show that to you as well like this so you I'm creating a VPC here and then I'm creating a subnet so I need to create a subnet in this VPC so I'm saying VPC ID equals to awcpc dot my newvpc.id this is how you can Define dependency and I hope this this makes your I hope the concepts have been clear let's revise what we have learned till now so we have learned how to define comments in HCL we also learned block what are different block types like provider resource locals and all the different types of blocks we have also understood what are attributes which are key value pairs that Define the settings and the configurations inside different blocks then we learned about the different database like string number and Boolean string should always be in double quotes number can be in codes or without codes booleans are true and true and false value which are without codes then we logged into list list of items that are side square brackets and it can be a list of Security Group list of subnets list of availability zones and so much more then we also looked into maps maps are dictionary which are key value Pairs and you can choose to remove any value whatever you want and value can be either string or numbers or Boolean anything then we looked into conditions conditions Define condition can help you make decisions depending on different criterias and we'll look into an example of how you can change the instance type depending on the develop depending on the environment next we'll also load into functions and there are different functions you can use to change or to transform your data lastly we looked into the resource dependency on how you can use two resources together to make things more interesting let's try to create a challenge and I want to I want you to do it and also post this on LinkedIn if you are able to do it he has the challenge to test your knowledge on everything that we have learned to learn in this challenge we are going to create a file in a local machine using terraform and we are going to insert some message in that file using string functions you can use as many string functions as you want to test out your knowledge and to make sure you understand functions properly so let's create a resource I'm going to create a resource and name it as null resource I'm going to create a resource and the resource type is null resource because we are not creating anything in the cloud but it is in a local machine and I'll be naming this as file this name is just for terraform reference not the thing that you'll be seeing here we are going to name the file something else inside there's another source I'll be using a provisioner provisioners are used to run commands and there are three types of provisioners it's an advanced topic so we are going to learn about it later on in the later sessions so I'm going to use local exec provisioner so I can run this in my machine and the command is going to be Echo command so Echo the message and the message is going to be message colon I want to type hello world but everything it should be in capital so I'm going to use the upper function so I'm going to say upper and inside this so I want to have Hello World all in capitals I'll be typing hello world here which will be Capital using this function and I'll show you once we run the commands so hello worlds and then I want this to be saved inside a file named as challenge.txt but before I do that make sure you save close this bracket here as well and now save this in a file name Dash challenge dot txt once this is done I'm going to save it and go to my terminal in my terminal if I show you I only have two files and after running few commands I'll have another file named as challenge.txt we should have a message message a colon Hello World online Capital so I'm going to run Tera for a minute the First Command you need to run to make sure everything is in place and in order to install some things and if you check here you will see that few files and I'll be here present automatically which I'll be explaining you tomorrow in the provider section in the next video in this next reminder from plan to see what is going to be created and it says one thing is going to be added which is a file and the resource dot file is going to be created null resource dot file and now if I'm okay with that I'm just going to run terraform apply so let's run terraform apply command it will ask me if you're sure type yes if no then type you can type anything else once I type yes you will see a resource has been added and it says challenge.txt has been completed and you can see a file name that cylinder takes is there which has the message called in hello world so this is how you can create a file in your local machine using terraform I would have made the challenge is more complex by creating this creating a server on AWS or on Azure but I don't want to confuse you with those things because we have to learn about it tomorrow we are going to learn about providers and resources in the third video so make sure to check it out and after we run providers we are going to create repositories on GitHub we are going to create servers on AWS and lot more interesting challenges so make sure you watch that video and I hope this video was informative you have learned everything about HCL syntax to make you to make sure you can work on it if you have any questions any doubt feel free to let me know in the comment section and also try to complete this Challenge and share it on LinkedIn and don't forget to tag me thank you and have a good day
Info
Channel: Cloud Champ
Views: 1,518
Rating: undefined out of 5
Keywords: how to write terraform code, how to write terraform scripts, how to write terraform script in visual studio code, terraform, terraform tutorial, terraform tutorial for beginners, terraform syntax tutorial, terraform syntax, terraform syntax example, hcl syntax, hashicorp language, hashicorp language tutorial, hashicorp configuration language tutorial, hashicorp tutorial terraform, hashicorp terraform, terraform fundamentals, terraform aws, terraform from scratch, cloudchamp, devop
Id: UR665AHN9yE
Channel Id: undefined
Length: 19min 0sec (1140 seconds)
Published: Mon Jul 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.