Deployment automation to the Azure Cloud using Terraform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] well hello guys and welcome once more to our Channel what's I would like to do today is really just show you how to um more or less deploy um resources to the Asia cloud and in this tutorial we basically deploy an Asia function and then we can more or less begin to understand how to automate the deployment process of resources um to the cloud and this is going to be a continuation of a video we did in the past which was more or less just building um an Asia function in Rust and then deploying this um to the Asia Cloud if you look over here I have a an HTTP server already I can to car run and we can go to this endpoint I'll use Curl to quickly go to this end point so Local Host 3,000 and I'll go to API SL health and then this will of course trigger this endo and what I've done more or less is if you're looking here in main um there should be an endo called Health um which should be over here and then the logic of course is in here here so if we go to OS and then go to API we can see index so we get this message your API thisp was triggered sucessful and this is also what we got done here and then of course as the information says if we come over here and pass name and say I say like Tony and then it says hello Tony which is of course more or less what is over here nothing special again I already did a video in the past explaining how to more or less set this all up and um just in case you were wondering this is an HTTP server in Ros WR with the w framework I'll add a link to the um the to this um um um to this framework if you would want to check it out I'll also add a link in the video where that I did in the past explaining how to actually set this up from start to finish and but what I would really want us to focus on today would more or less be to deploy this function to the Asia Cloud using terraform and then test it out to see if it's working as expected and so of course now we can more or less confirm that our function Works locally and um I'll just stop the server here at least we we are pretty sure it works we can quickly do is try to run this um as an Asia function and I can quickly say F start and to see if everything works and everything seems to work but then we got an error oh okay yeah so over here and um in host Json I added that um the default executable part should be this in the Lo in the in the root but of course that is missing so I can quickly just stop this and then I copy this should be in Target and dbug and then called Edge it's going to copy to the root and now we have it in the root and then I can say funk start one more time and we should not be able to get that error again and then again we can see curl this before the only difference now is our Asia function is running at part 7071 and this should still be fine so again this is no longer um just we are not interacting with our W server anymore but we are interacting to it through um our asure on time so pretty much very straightforward um this is working so what I can just stop this and then maybe close it so we have a lot of room so but what I would want us to do now more less is set this up in such a that we can deploy it as an aure function pretty straightforward and to do that I just create a folder here um I don't want to search I want to create a folder so I pull up all of this and I'll create a folder in the roots and I can call this maybe function and what I want to do is maybe I copy all these files so again these files um are more or less what we used to define end points um that would be um available to our function so we have one for example like what we just visited is API health and inside of this is simp a single Json file called function Json and it just um Define some parameters um with which um Asia knows how to bind our functions or path so here the Earth level is anonymous and what it means is I'm letting my function decide however it wants handle authentication I'm not doing anything um let's say from on the aure side maybe using the active directory as the case may be and then and the direction of course would be in um the name is um API Health the rout is/ health and then the methods are get an option and pretty straightforward of course this can get more um but let's this is really more or less what we have for now and again that's what we have in all of these files over here they are just um files with file folders with a file inside code function Jon defining a particular route in our in our server that's just pretty straightforward and of course um for our function to be complete I would like to move all of this so host Json local settings and this executable I'll move all of this into this folder called function and what it means is that this is all we need to run this function as um an aure function again if you look down here you will see our function logic we have everything blah blah blah our setup and we use this of course we want to run this locally and then in here of course would be where our binary and all all of that would be contained but but more or less for our function to run I can quickly also show you if I CD into functions I can still say fun start and everything should still be fine and if you visit this end point one more time it should be fine so the point is more or less to let you know that our function to run as an AIA function everything we need for this function is in here again the definition of our paths host Json local settings. Json and of course ex the executable again I'm building my um I'm more or less working currently in Windows so that's why my um binary here is an executable of course windows with Linux or Mac or whatever it's going to be different but the the process stays the same actually so again everything we need to deploy our function is inside the function folder and now what I would want us to do pretty straightforward would be to set up um how we can deploy our function using terraform and for that I will create a folder called function rust and in here I will Define three file so this is going to be a our module to deploy rust functions to Asia that's really the whole point and in here I'll create a file called men. CF I'll create another one called variables variables. TF and then I'll create one last one and this would be output TF and of course if you want this to be an actual model you can also add a read me and that people can read to more or less understand how to use your module but let's move forward again we're trying to structure this in such way that we can reuse it if you want to deploy a th000 Asia functions for example we can do all of that using one module and again this module I'm going to really really really keep it as simple as possible I don't see the need for us to make it so complicated but I'm just going to keep it as simple as possible and so we can use it to deploy what we want to deploy and so the first thing I might want to do more or less is to define the variables I think that's um would be necessary to use our uh our module and maybe I can make this a little bit smaller so we have a lot of room so I need to create a variable the first one I would want to create here will be called location is going to be of type string and I can give it a description so this would be at the Asia region to deploy resource to something like this and the next um variable I would like to provide to this resource will be a function name again again it's also going to be a string and I'm just just going to keep it um simple for now and or maybe I can add okay let's we'll get back to this anyways but let's let's continue I would want to create one called prefix this is going to help me to more or less group my resources so that if they have certain preface I can already tell where they where they belong to it's going to be of type string and for this description I can say the prefix which would be used to identify um resoures B belonging together something like this and I would want to create a new one I'll call this I'll call this um I call this environment so this would be the environments where I'm deploying my resource tool um and this is going to be again and a string so this could be like stage Dev prod whatever the case may be I want to create another one called Resource Group name so the resource Group is going to be like the container um that's going to allow us to group the resources belonging to our application together so for everything let's say we have an application that have I don't know 10 10 databases blah blah blah um servers and everything dock containers whatever and they are all belong to the same like um offering the same application we can actually have them all leave within the same Resource Group because again it also helps to make it a lot easier to manage I'm so this will be the resource Group name and what else do I want to so yes I want to provide um the function call this function source code path and this would allow me to provide the path where my function folder is so I can zip it all together and deploy it and another one would be I would like to provide one called app settings so this will also allow me to provide um provide things like environment variables as a case may so it's going to be a map and I can provide a default and I can call this environment and I can call this test for now something like this and and yeah I think this should be fine and so this is really really important the function name I would like to add some um some more information over here so I like to provide a condition did I Ty that right so sorry I have to provide the validation block and validation block I have to provide a condition so the condition would be um because of course with Asia there are certain conditions that needs to be obeyed when you are giving a function in name and that's also what I want to replicate it so we don't waste our time so um it's it can only contain alpha numeric characters and of course with numbers and that's it and it cannot build more than 24 characters so that's what I would want to replicate here um so here what I would want to do is to add use this reject function that's terraform offers and I will provide um so here I can add my rejects so um I would take A to Z A to Z all um low um and of course the numbers and of course I don't want the numbers to be the end so that's why I want to have it in between and other than that I can also have um alphabetical um characters um in the capital letters as well and I close that up [Music] um and no yes that's what I want and um of course this would be my function name and so this will be one of my conditions and the next condition will also be that I'll use the length function that terraform also provides to calculate the length of this um variable so I say this length um it's also called function name should be less than or equal to 24 and I provide some error message and what I can see here is invalid value um Pro provided for function name you can you can really actually add here like some fun information that's actually more or less an error message that can actually tell what's going on so here we can say it should be alpha numeric yes alpha numeric [Music] and um I say let's keep it simple alpha numeric only something like this that's it so we try to do some validation so that when you try to use this module and you provide this value if doesn't meet this requirements we simply error out and this is the error message you print to the terminal pretty straightforward so that should be all for our variables and now we move over to our main.tf and here I just want to Define some resources that would be needing so again like I said the container will be our Resource Group so here our resource would be is your resource manager and from the resource manager we need re Resource Group this one and we have to give it a name so I can simply call it easy Dev Resource Group like this and there have there are two really really important things we have to provide one would be the name so this name we get it from our um what we defined and the next one will be our location this will also come from location so this would take take them from like what we defined over here that's that was the essence of actually defining this initially so with this we created we've created our Resource Group the next we want to create of course will be storage account so this will help us to store um files that's attend to our our aure function for now that will be the only thing that will be contained in our asure function anyways in our Resource Group so that would be more or less the only thing needing this um storage account so that was not one I wanted storage account yeah this one and I also have to give it this a name I can simply call it storage account that's fine and again key important things that we have to provide will be the name and I'll try to append this to our function name just to make it um kind of like unique so this will be function name and here I can just provide store so our function name Plus store would be the name of the um um storage account so Resource Group name again would we take it from what we created over here so this will be Asia this one and then we need the name and again location still do the same the location and of course the next important will be our account tier and for this I'm just going to use the standard account team and for S um replication type um also going to keep this quite simple I need lrs and of course we made a mistake we needed here the location and that would be more or less all we need and so of course we have our account here and our account replication account application time and that would more or less be all we need um the basics at least that we need for our storage account the next thing would need to the next resource would need to create would be um we need to create some application insights that's going to help us of course have insights to our resource um so again we get this from our resource manager application insights and I can just keep it simple again I can also call this application Insight like this and again this also going to be pretty straightforward I'll take the name again like we did here so I can quickly just copy this one put it here the only thing is I change this now to insights like this and location again come from this then location and the next one of course will be our Resource Group name so again this will come from this and we have a name attribute and application type I'm just going to leave this for now as web so now these are more or less some of the things we need for our function of course we now need to create our service plan so the service plan more less is going to tell us um or provide information of pertains to the run time of our function and and again I'm going to really keep this quite straightforward and spare you more or less of course the the really really important the the really difficult aspects um but of course in the future I hope to make a comprehensive video regarding how to work with terap if there are a lot of enough people who have much interested in this and again I provide this again and and I also make this unique generic more or less so this will be service plug again we have location exactly like we did over here location and Resource Group name um this will be our Resource Group name and then we need to say the O type I want this to be windows and SKU name this would be y1 so our function of course we can actually deploy multiple functions within the same service plan it doesn't really matter but for our own scenario it's okay to just have one it's totally fine so now to the really really important thing this would be our Asia Windows function so just Windows function app and we can just call it maybe [Music] Custom Custom Handler because again the is going to have a custom run time so this will actually be used to deploy our Asia functions in say rust or go whichever one we find quite interesting so of course the name or our function will get it from our function name again Resource Group name this will come from again from the one we created over there and location um from this one as well and what else so now we also need to provide of course quite important information like storage account name um this will come from our storage account which is this [Music] one no storage account and then name and storage account access key because of course our function needs to have permissions to access the storage accounts that we create it over there and here we'll provide the primary assess key and service plan ID this will come from our service plan and provide the ID and I would like to add a few couple features so h2b htb only and app settings um I will just um add one couple things so apart from what is provided I would also like to add a few things to to this so I'll use the merge function and in here I would like to provide the application instrumentation key and this can of course come from from our application insights um if I can [Music] type application insights and let's see first um so let's let's add this sites config so we we can get rid of all of this um errors so from our application insights we need the one we created over there I think we called it application insights and then it has instrumentation [Music] key and so we merg this with what was provided app settings and that should be fine now of course I need to provide a couple more informations here so I add like to add a couple more things like our function extension version I'll like to use of course function version four and again I'll have to like provide the path to more less where our function is deployed for now leave this as an empty string we'll get back to this in a minute and now of course we need to set up our side config so I would like to provide our application insights connection string this will come from application insights and we have connection string like this and we have application Insight key and we can again get this from this this will be our instrumentation key and then I need to provide a couple more things I can say http2 enabled um I can send this of course to true and what else and then I need to of course um we are running um custom room time I need to specify an application start stack block and for this I simply add one value for now so we use custom run time this would be true and what else can we provide maybe I can also provide information about app service logs and I just provide one variable this would be of course the retention period I can set this to 30 days that's fine and I can also provide information regarding cores so allowed origin I can actually just make this everything for now and I can say maybe support credentials I sent this to false I'm just more or less the basics and of course you can do some IP restrictions if you deploy your function into and a virtual network but that's not really our case for now so that's totally fine and I'll maybe provide a minimum TLS version and I can simply say this to one 1.2 that's totally fine now for the deployment of a file we like to create an archive down here so more or less a pre-process that's going to help us see our function um folder um for deployment so I Pro I can create a in a data section and I can call this archive file so this will be archive file and can I call this maybe con custom function I don't know package something like this and so provide a few information so it's going to be of type zip um what El s deer is one and we getting this already from function source code path this one and output don't want file mode I want output path so again I'm just going to give this the name of our function so V function name and then zip so the function name will be the name of the output file and what else I can provide again some exclude section and here you can actually um provide some values you would like to exclude so for example if you if you have I don't know for example if you're working with python and you work with virtual environments um you would have you might actually have a folder called V or whatever and but those you can actually provide them here and they will be excluded when your file is being bonded now we can actually come over here and now we can provide the path to our ZIP so this will come from data. function package this one and we need the [Music] output output path um like this this is actually more or less all we need for now of course there are a lot more configuration we can make um but for our use case this should be enough so again we have here our Resource Group um storage account and inside service plan and then our function then this here over here more or less is to help us do a preprocess to zip our file into a zip and then provide a path that gets um uploaded to again inside the storage account over here for our function to have access to our function um files and the last thing we want to do one less over here would be to provide some output variables so this actually where we decide like to put out a couple of informations that we'll find useful um and this could be our function URL after deployment so we can quickly do that I can call this function actually don't need this I only need um the function I call this function um function hosts name something like this and again the value would come from our window function where is it and we need a default host name that's more or less the only output we'll be needing and I think we are done actually one thing we can also quickly do might be to just format um our code and we can simply say Terra form fmt and do a recursive format and you should be able to format all our files for us and now um everything should be looking good so the next thing we can now do more or less is um defined our men or outputs and of course the variables that need to be provided when anyone wants to use this resource and now what we have to do is more or less try to use this resource and see how that goes um and to do that I'm just going to in the root here create a file called main.tf and and there are a couple of things I would like to provide here I will not be adding a back end because um again I would like to keep this as simple as possible so the the would be storing our state file locally so that no need to store this in the cloud so I'm going to create a terraform block and in here I will provide um required providers and here I would like to um of course I would be needing the resource manager so this would we get this from this is from hash cop I hope I tapped that right and Asia RM so for our resource manager and the next thing I need to provide will be a version and I can um try to be brave and go for 3.8 I think that's what I'll be needing here and for um for terraform what did I do for terraform I need um required sorry required version um for terraform I would need to go upwards of 1.0.11 this is what I have locally um but this should be fine um also for any version above of this one that should be totally fine and other than this I need to provide a provider block and here I call this RM so more or less be fitting what we have provided over here and for this provider I need to provide um a couple of important information so one will be our subscript subscription ID so this will be to the subscription where I'm deploying this sorry um of course not inside our feature Block it's not a feature block it will be for the provider itself so subscription ID I'll provide this later of course of camera so I don't compromise my credentials I need to provide a client ID I'll provide a client secret and a tenant [Music] ID and that's more or less what I'll be providing then I try to format this [Music] again and I think I have to go out one layer and then format again so this should be fine okay and again I'll provide this um informations of camera just so I don't compromise my credentials so I need to create a data block um this will be for my client config um so if Leisure resource manager client config and I can simply just call this current and I'm I'm going to just leave this empty for now I'm leave this empty for now that's totally fine so I think this is all we need more or less to actually use and test the the the the module that we created and of course what I might want to do would be to um would be to uh um again like I said not bother about storing our resource um our state file in the cloud for example that's also why I'm not providing a back end but again that should be totally fine for what we're trying to do and now I have to create a model section and this is where I'll try to use the model we created I can give this in name I can call this rust function and so very important things that the first thing I would want to provide of course would be the source so this will come from our function rust and now it's going to complain because it will need us to provide all this um all this variables we specified in here this this is more or less trying to invoke a function and then it's it's complaining because we have to provide all of those arguments if you if that's most convenient for you that we need to provide so one of them will be location and I will set this to West Europe because I'm in Germany and and the next will be function name I can give it something quite straightforward I say rust Funk maybe 001 something like this and you have the prefix and I can call this um easy Dev and environment I can call this test for now that should be okay Resource Group name I can call this like easy def RG for Resource Group and the last one would be function source code path and then this would be inside this folder called function the one we prepared over here so I try to format this one more time and everything is looking good and then the the so this should be able to use the model we created over here the same way you would reuse a function so if you wanted another function for example we can actually implement it in the logic just so that we can provide a count but if you want to use this model to provide to deploy a different function all we need to do is copy this provide different parameters and then we will be able to reuse this model to create a different is your function resource and now I would like to provide an output so this would um the only thing I would like to be the output of our deployment would be the function URL and just so we don't have to leave the terminal try to go to the Asia DET terine is your console to try to find the url of the function we just pluged so the output is just going to Speed it out for us and then we use it and test our resource creation so I can come over here to outputs um and I can give it a name I can call this function URL and this the value would come from our module rust funk function app host name this is exactly what we need so in the end it's just going to Output function URL in the console and give us the URL of the function we just deployed so if everything goes as expected we should be able to to plan and then deploy so I can simply say um of course we need to First initialize um this I can simply say terraform init and but before I do that I need to again of course come over here and Pro this Valu so I'm just going to pause the video for the video for a minute and then provide these credentials and then we can continue and now I've provided the credentials I can simply say terraform in it and maybe I I close this part so just so we see what we're doing and we give it a couple of seconds um for it to initialize and now initialization is done and now we can [Music] plan and the plan is going to show us the changes that will be made when we make our deployment and when we do plan and deploy for example it will of course always ask us to um approve by typing yes into the terminal if we are okay with the changes that will be made um if you are not okay with having to type this all the time you can simply add an um Auto um approve command and then it will not have to ask you this all the time so if I go to for example try to do ter form deploy it will ask if I hit enter right now it's going to ask me after a while if I'm okay with this but I can simply say Auto approve and it's not going to ask me just going to go ahead and do this straight up without asking me anything um yes of course um terraform apply and you should be able to plan as well as apply our changes and so again we give that a couple of um minutes if not seconds to complete so if we look over here um it's of course first starts with creating our Resource Group because the resource Group is going to function as a container that's going to hold everything together and once the resource Group is created is again going to to go ahead and then create all the other resources that need to be created in that so here you see like the app service plan the insights storage accounts as as as the case may be most of them are actually been created and some have been completed as you can see over here so we'll give it a couple more seconds and everything should be fine so the deployment has been complete and then if you look over here um like we asked um we have our output that tells us the function URL again if if you look in the outputs that we created over here it was actually called function URL that's exactly what we see um down here and then of course to test if everything was as expected we can just copy this UR and clear the screen and then we'll make a call request to this endpoint it's going to be this/ API and then SL health and this should still work as expected and we can simply just um of course provide append this with https so I can say https and then they should still be fine and then again like we did before we can pass a name call this Tony and then we get hello Tony blah blah blah so this was really a quick one and um of course as you can see this is like a really really handy way to deploy Asia functions in Rust of course Asia has been um rust has been getting some popularity over the um the last few months and years and um Asia has also of course taken the initiative more or less to provide opportunity for developers to be able to run like custom run times um and of course giving the developer the opportunity to develop um functions in languages of choice and I also love working with rust so I'm like of course why not try to use this to develop as of functions so this is really really um something you could more oress use as a template if you happen to have to deploy um functions um um whatever as the case may be and of course maybe in the future I really hope to do like a series um as it pertains to rust and so we will not only then be deploying like a function like a single stand alone function but it's going to be like a full-blown application that's going to have um that's going to be a multi-tier application so it's probably going to have like um up to two three tiers and would have a lot of resources in there would have databases we have um servers um containers and um of course it's all going to be defined like a really really clean architecture as you would um for um for a real life application in in an organization and then it's not going to be just an AG function but then we'll have like a lot of maybe T if not up to 20 resources that will be needing to deploy using um um terraform and then you can of course um have the idea of how you can actually structure um your infrastructure as code using terraform and then more or less harness it in such a way that um you can it can be a lot easier for you to collaborate with other developers um and anyways that will be it for now um again we try to deploy our function using terraform to um Asia cloud and I would provide a link to this um to this um solution as a uh GitHub repository so that you can maybe reference it in the future as a template should you want to um or maybe have a question in the future and you would hope to have like a starting point to um start working with ISC using terraform anyways that will be all for today and and um as always um I hope to see you next time um of course if you haven't already subscribed please go ahead and subscribe to our Channel and of course engage with me if anything was not clear um please feel free to leave me questions and I would get back to you as quickly as possible until next time have a lovely evening and bye-bye oh [Music]
Info
Channel: Easy Dev For All
Views: 25
Rating: undefined out of 5
Keywords:
Id: nF7dcbEBIv4
Channel Id: undefined
Length: 43min 15sec (2595 seconds)
Published: Wed May 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.