Exploring Terraform Data Types - string, number, list, set, map, object, tuples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign in today's video we will see how to use type constraints in terraform let's get started so there are two types of type constraints which is primitive type and we have complex wipes oh so in primitive type as you all know if you are already aware of programming any kind of programming language there are three primitive types which is accepted by terraform which are strings number and bull so these are very important uh when if you are going for a certification perform certification or if you actually want to use in your work and next important type is complex types in complex types there are two types again there are sub types which are collections collection type and structural type so these two types also have their own sub categories so the collection types are nothing but uh list map and set we will look into what are their users how to use them where to use them all those things in a while now we have structural type in structural type we have objects object and triple so one important difference between the collection type and object type is always remember the collection type is something which can store multiple values of a similar type right whereas whereas structural type is something which can be declared which can store values with a different type again there are a few rules and all for each and every time I will go in detail how to in the actual Hands-On so let's move on to visual studio code for this tutorial I will be using VBS code so let's assume I want to create firstly I want to create a folder for this lecture I will name it as lecture Phi okay and in this lecture file I will create a new folder called terraform or I will say variables or type constraints type underscore points range now under this type constraints folder I'll create a new file called main.tf and always remember main.tf is uh is an optional you can name it as whatever you want but main is just a standard naming convention and so you can use whatever it is so now first you need to have a terraform block so I'll just copy it from any of my existing thing uh not this I can go with someone this one yeah I have a telephone block here I'll just copy this block so this is my sample terraform block so remember one thing for testing these variables and all you don't really need to provision any kind of infrastructure on any of the cloud providers you can just use either use terraform console I'll also show that how to use how to test your type type constraints on terraform console but before that as on the in the real time we basically use uh terraform config files so let's see over there first so let's assume uh starting with the Primitive types right foreign types we have three types right one is string the other one is a list oh sorry string number and what is that Bool so string is nothing but if you want to store some kind of a name or some kind of a name of a VM or whatever it is which is a string you you can use string primitive type if it is a number you need to use a number primitive type and if you just want to store true or false you just need to go for bull so let's see an example that assume I have a variable let's say the name where one okay in this I want to store a name uh let it be something like and again every variable you can have a default value or not I will just give a type and here I can make it a string so type is nothing but which says what type of value can be stored and here you can define a value which is uh I can give something like tutorials right so this is my variable and I want to display it so I'll just give I can give the same name and value I'll refer this value where dot where one so now I want to see how uh what exactly it will display so that I'll just go open the internal terminal uh not in this way I will right click on this one I'll open an integrator terminal and then I will okay we are in type constraint so I'll just issue the raw form in it so once I issued at a four minute it will try to initialize the directory and download all the required the reform version and plugin so one thing is actually we do not need this provider so I can delete it give me a second let's see yeah so I will remove this provider which is not at all required because we are not using any functionality of okay we are good now so now let's try and let's just shoot that off for a minute one more time let's just to be sure now let's do a terraform apply so this shows if you see here our output as tutorials let's assume I am giving a number here by mistake oh you can see it still works but if you see these double quotes what happens is when you declare it as string it will consider it will convert that value or number directly into a string format let's assume uh you have given or okay you have given a number here and if you try to run it if you see now you launch those double quotes and it is considering it as a value so that is a major difference always remember even if you give string and give a number it will be automatically converted to a string and one more important things regarding this variables is by default it will decide what type it is so let's assume I comment this type here and if I try to execute this one it will automatically data or form has a capability obviously it is using go in the back end so it will decide it will determine what kind of type it is whether it is a number string or ball okay this is let's keep it as string and give some name as reals that's fine now let's assume I want to Define Boolean so for Boolean is nothing but uh which accepts only toward false so let's assume I am giving a true here let's see what exactly this will be considered because I'm not giving a type terraform has a capability to determine whether that is a Bool or a number or a string so now I'll just try to where dot where to so now if you see it is uh it is it assume it already uh determined that it is a Boolean you can also give the type here uh manually if you want when that works the major use of the types will come when you are trying to use collection uh the collection or structural type so we will see uh what how exactly these uh primitive types will be useful in that demo okay so this is how you need to use even if I execute this one more time it should give the same error oh wait type is not expected here oh sorry I'm giving this in a wrong location so you should give underwear so even if I execute it here it will give you the same where one is one where two is true so one important thing before going there going to the next topic let's assume I will try to make it a number so terraform should detect it and throw error because tutorial can't be a number right so it will throw error saying wrong spelling so it will say the default value is not compatible with variable type constraint a number is required so that is what terraform has a capability to identify what type it is uh it will automatically determine what type if you don't manually mention it and all so this is what it is so uh this is what primitive types are so now let's look into uh what was the next topic complex types right let's assume let's write a small comment here just for our understanding first start with let's start with collection types okay this is very important topic if you're going for an uh terraform certification so let's assume uh user want to store multiple phone numbers or multiple ec2 instance IDs in a list so in that case you need to define a list right so I'll say list one it'll declare a variable list one and type is equal to list so there are few rules with the list uh the list is more flexible we'll look into that and type is equal to list and default is equal to so here there are two ways of defining either you can go obviously you should always have the square brackets if you're going for any kind of collection type whether it can be list I was forgot to write that we have list we have map we have set right so set and let's start with similar map is a bit different so let's start with the list so always remember all these three types need square brackets without that it won't work so let's assume I want to Define five members okay whether it can be a string or whatever it is I have five numbers here and you can have duplicate numbers there is no difference that is a beauty of list you can have duplicate elements it will uh it will it will not throw any error if you even if you have duplicate elements in the list so now I'll try to do a output and I will just say list one itself and I'll do a value and I'll print this out okay so now this is a list so now let's try applying this so now you can see there's a small message it says two lists and it is return on the list now let's assume this can also be written in a string as well so that can be done by string oh sorry I'll just write some names here Ram Ramesh Peter and done so we have four people and this is all I mean basically this is a list which contains all strings so so now if I execute this the terraform has capability to determine what type it is and it will just it will accept whatever the type it is let's assume I am passing 1 here which is a number and if I try to run it it will still automatically convert that one to a string let's assume there is a scenario where you want to restrict the list to have only strings so you just need to add circular brackets after the list under the type and give the specific type you want list to accept okay I will only accept strings in here so you just need to click here and still it is converting okay so even if you mentioned the string so it is automatically converting to string so let's assume I will make a number here what will happen see so it will throw an error now similar to how we have seen here where we have tried to convert this tutorial accepting number here right as of now it is number so how this will throw an error if I mention number here similarly the list also determines it can't convert that number to a string obviously Ramesh can't be a number right or Ram or P Docker those are strings they can't be converted to number so that is what it is always remember if you are restricting with a number it will always consider it as a number let's try with Google I'm not really sure if Pool Works I never tried Bulls yeah even Boolean won't work with strings and all obviously that is expected let's make it true false and true hope this will work Dr UE sorry for that I'm making more of typos yeah if you want to accept booleans uh only two and false you can give this let's assume I am making one here I'm it should throw Arrow if it won't that's okay okay that's true it won't it will throw error saying that one can't be a Boolean right that's obvious so that is what that is how you need to use list and always you see one important thing uh let's see whether uh list has a capability to sort stuff and all okay I'll give something in a jumbled format and I will make this a list and if I try to run it no it is not automatically sorting but there are a couple of functions in build functions in terraform which can be used for sorting we will see that in the next lecture and now let's move on to the next topic which is set so set is something which accepts a specific only a specific sorry it will accept it is similar to list but it won't accept duplicates let's see that so let's assume I have a set here and it should also be having the same syntax and you can also use it or won't use it doesn't matter so let's assume the same or else I will do like this 6667 and I'll write two eights here and I'll try to print it out and I will try to comment this and see output I'll say set one type is equal no type sorry value is equal to where dot set one so this is what it is now I'll try to run it sorry yeah so if you see the set type construct requires one argument specifying the element types always remember set requires it will only accept one data type at once for example if list is something which you can you know matter what you give over there whether you give a string or number or not give anything that a form will detected list will detect it what kind of it will convert by default it will convert everything to string whereas set is something where you need to mandatorily give a type for example here I will give string and let's see what it will do okay so it will convert all these values to string all these values to string so let's assume a scenario and one one thing you might have observed here most important thing here you have five six seven double eight here you only have five six seven eight so it will remove all the duplicates that is one feature always remember there can be a question about it as well and it will it should you should for sure declare and mention what type should be accepted okay now let's assume and and the other vice versa is similar let's assume I have similar kind of names here I mean to say strings and if I try to save it everyone it works awesome what if I make a number here it should throw error for sure so if you see it wrote an error saying that number is required so always remember for set it's mandatory to mention the type and again if I try to mention a number here sorry not number uh I think I'm doing a vice versa that's true so if for that to work I need to make those two number and again duplicacy right I want to show duplicacy if I say Ram here and if I say string and if I run it it will remove the extra Ram from there so that is what it is that is how set works now let's quickly move on to the next type which is the map this is the final type in the collection so again Define I'll just copy the same thing or else that's fine I will write it quickly and this can be a map type and map is nothing but something in which you need to mention key value pair for example name is equal to name will be the key and the value of the name is the name of a person can be the name of the person whatever it is based on your requirement so remember it is more like a key value pair right I can quickly write here key slash value pair so here type map and default the default again this also accepts same no it won't accept the same square brackets you should give map you know curly brackets always remember I might have mistakenly said in the beginning of the video that even map accepts square brackets no it's not it you should mention curly brackets so in this curly brackets you need to give key and value pair or number is equal to one two three four five so now I'll try to do a quick output map and here I will see value I'm going to print it out let's see if it works or even map and okay awesome it's working so again remember map is something which do not need a declaration and all but always remember it will only accept uh everything should be of the same type you can't have uh some kind of a number at one or for example let's see let's see another example like it says number it automatically converts that to a string and here you can only pass one type you can't pass multiple types like this you can only pass one type remember that if I give string it will consider if I give number here it gonna fail saying that the value is not compatible with a variable type constraint number is required so in here you can write something like one 2 1 is equal to one two is equal to 20 whatever it is it should work so always this is considered as string whether you go for a number or what number or string and followed by this is a number as you have mentioned number all the keys will be considered as a number let's try uh something like this right we'll let accept like this or two here no it won't so you sure it will only accept one and only one and it can be as a string a number or even Bool it will accept I guess let's try Bool okay so let's say Bool and true false so if you say number now you're gonna fail obviously true and false can't be a number it will do an arrow so that is how map that is how Maps can be used you should declare this basically represents a value type whatever the value you are passing that type will be accepted by the map and it is optional but for set it is mandatory remember that difference and that concludes collection types now comes most important type which is structural types so there are two types in structural types one is object and followed by triple so now let's start with object so I will go with object first because uh map and object are more relevant so I'll just copy the same code which is written here just to save some of the time okay so this is object key value pair when object accepts key value pair I'll say object one and here again I need to say object so let me uncomment this first sorry for that okay so then you can have proper coloring or not okay now let's the one important major difference between map and map and object is in map passing a type is optional and it will only accept one type at a time for example if I mention number all the values should be numbers right only this should be number this should be number if I give Bool all the value should be bold by gives ring all the values will be string even right whereas when it comes to object it's more flexible you can mention what or which key will accept what kind of value for example in my case let's change this from better uh naming convention let's say exchange like this so I will have name key which accepts string comma phone number which accepts bull wait a second am I making some mistake hold on I have tested it somewhere before let me check it should accept let's check the documentation so here is the documentation you can refer under structural types we have objects we should give a key okay one major mistake what I'm doing is the way I'm declaring it it won't accept this uh you should give curly brackets over there while even declaring it yeah like this it should be okay now it should work so now let's see uh we have name is equal to string phone number is equal to number it's not bull okay now let's assume this is what it is more flexible each key I can Define different uh data type okay so always remember key should match what exactly you are passing that is one kind of limitation or restriction you should mention the key in while you are declaring it in the type so now I'll try to print this object checked one one second what happened required oh sorry see I am passing Boolean here that's the reason I throw an error I need to pass some number something like this and it'll work I want to try something else what if I pass this number in a string will it convert it yeah terraform has a capability to convert this string which is give I mean the number which is given in double go to number or integer so this is a major difference between map and uh object this basically used object is basically used in the scenarios where you really want to restrict what kind of value you a user can pass to that object or to specific key like uh the phone number will only accept numbers you can't pass some kind of a string or a name to that phone number key so in those scenarios this is very useful and this is very important so we make sure you understand these two concepts the differences between map and object now comes tuples tables are quite similar with the list and set so I'll say triple one let's say uh not default sorry it should be type and I'll show the default value this also will accept I will show the major difference what is the difference between Tuple and all sorry my comma was not working right okay let's assume I have this coded like this triple one and value as vad or triple one so we have triple here we are trying to print we are passing only values and again remember Tuple set and list all should be declared inside the square brackets map and objects should be declared in the curly brackets or declared the curly brackets right so now let's see uh let's apply this and see what will happen so it says triple type construct record one argument so always remember the structural types for structural types it's mandatory to pass a data type at least one right so for this one as I have how many one two three four five right so I need to I think even one works let's try passing one and see if it works so so it says triple requires total number of variables I'll just reduce number of variables here let's make number number so if you see it worked so what exactly Tuple is expecting is it will accept different kinds of data types at once so the list can contains a string number or a Boolean at the same time whereas list it won't accept all different kinds at the same time nor the not the set will accept all different kinds at the same time triple is only one uh which can accept which is more structural you can change the structure that's the reason it is under the category of structural type similar with the object whereas collection lists map and set are something which comes under the collection collection of same kind of items you can remember that if you understand the meaning of it right so that's what default is equal to one comma six which says this so let's assume I want to pass a string now so for that I need to declare a string and I need to say uh this Ramesh or something Peter whatever it is right I'll try to trigger it so so it will accept multiple things whereas number if I give number here it gonna fail saying that the default value is not compatible with variable type constraint number is required so this is how it works in the same case if you are the it will throw similar kind of Errors if you actually use the if you have mistakenly coded something wrong in the article declaring the triple like if you're passing a string but you have declared a number then you will see similar kind of error so always remember it will accept the exact type whatever you have declared in your declaration so these are the major things you need to know about uh type constraints different kinds of type constraints and these are the sub uh subtypes and different kinds of collection different types of subtypes inside each each and every type right for example in our case these are the complex types before that we have seen primitive types right so if you know this I think this will be more than enough for cracking or cracking any kind of question either in your terraform associate exam or majorly you won't be going more deep even in your real-time use cases right so that's it for this lecture thanks for listening see you in the next lecture bye
Info
Channel: DevOps Tutorials
Views: 588
Rating: undefined out of 5
Keywords:
Id: Gm2hE8FvTBc
Channel Id: undefined
Length: 26min 49sec (1609 seconds)
Published: Mon Aug 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.