Building Azure Infrastructure with C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Using this way seems a lot easier and safer then using some of the other tools like Terraform that likes to delete and recreate, etc. for just small changes. And I like the idea that you can integrated in many different things and hide the complexity.

Now I wonder if we would be able to take on AWS provisioning as well via it's SDK, so if you need to setup environments on a multi-cloud setup, you can? Thinking the possibilities now.

👍︎︎ 2 👤︎︎ u/bluephired 📅︎︎ Jun 10 2021 🗫︎ replies
Captions
hello youtubers uh this is a new video where i get to walk you through step by step through the um idea of provisioning uh azure instances in the cloud with c-sharp code without having to deal with arm templates or yaml code or any of that stuff i'm going to walk you through the entire process for developing enterprise ready uh pipelines uh to allow running a c-sharp code in your ado azure pipelines to actually provision azure instances in a very very secure way uh without having to learn any additional technologies so let's just start from the top here here's here's a blackboard here and let's talk about what what we're really trying to do here assume that you have a c-sharp file just a very simple c sharp file and this file has some code in it and you want this code to go and say i want to be able to provision an entire environment in the cloud right this is your non fabric kind of you know kubernetes environment where you get to describe you have a description of what your environment is going to look like this is just a simple application that you're building maybe a micro service that has an app service and a database and and and maybe some stuff around that you know maybe app insights and maybe you know a q here or here or there um i want to write some c sharp code so i can create these instances in the cloud without having to learn about arm templates i want it to be simple i want it to be easy i want it to be targeting the engineering experience as a first class citizen how do i go about doing something like that well for starters let's just go ahead and create a simple console application together let's just start from scratch let's build a little simple console application so i'm going to go ahead and create new console application here we go and let's just call our console magic azure azx and it's dotnet 5 let's just go ahead and create dot net five here we go and here we go so simple console hello world app and we gonna turn this very simple application into something that creates an entire environment for us all in c sharp pure c-sharp you don't have to learn anything else well first first of all we need to install some libraries we need a little bit of libraries we need the sdks something that can turn the apis that allow us to communicate with azure into something that we can work with here's my fluent api has five million downloads not super popular but maybe after this video people will start looking into this and make it make it popular so okay so i have azure management fluent i'm going to go ahead and install that and then i'm going to show you how powerful this is it's a fluent api fluent apis meaning fluent api is an api or an sdk that doesn't require too much reading or documentation it basically lends itself out for you to be able to work with i'm going to show you that in a second let's just say you have an i azure in here which is basically a very easy simple client that allows you to do everything and i'm going to assign it to null for now just for a second look what you can do with this client i can go ahead in here and say from app services i want to create a web app see and i want to call that web app my app xyz like this excuse me and then i want to say this is with a region and this is a region from let's say west u.s west uh or u.s west u.s west like this u.s west 2 right and you can go and say with existing resource group or with new resource groups see how it's it's it's so fluent it allows you to create an app service you don't even have to read a documentation about it right so in here basically it says with existing resource group okay let's go ahead and create a resource group so here's my resource group like this and i can go ahead in here again from that azure guy i can go and say resource groups define look my resource group like this and i can go and say with the region us list 2 and then create so just like that i created a resource group and i can take that resource group and throw it right here boom now i have a resource group now what what is what else does this guy have this guy wants you know with with a new new windows plan it wants a plan right can we use an existing plan let's see with existing resource group with windows plan i can create a windows plan and here's the pricing tier for this guy look how easy this is and i want the pricing tier to be see and here let me let me hide away the camera in here in the pricing tu tier you can go and say basic b1 basic b2 all the the pricing tiers that you can think of normally i just go with standard s1 and then you can define authentication you can define source control where the information is coming from you can define everything you can really define every everything right so in here i can say let's see create so this basically will create an app service for me right but what if i have an existing plan i don't want to create it this way you know i think you should be able to go and say var plan equal azure dot app service dot plans app service plans dot define my plan like this dot with the region here's your region uh us west 2 and then is my existing resource group of course i want to use my existing resource group and then um with with pricing tier so here's my pricing tier dot standard one and then with operating system and i get to pick up the operating system and i go with windows and then at the very end i'm going to create so this is me creating the plan so in here i created a plan and then i want to use that plan in here right so i can go ahead in here and say instead of with new windows plan i want to use an existing plan with existing plan why it's not letting me do that part me a second there should let's see i think that comes before with yeah with existing windows plan right there so with existing windows plan so i don't need to define a region because the plan defines the region and i only have to define the resource azure the azure resource group right so this is all good and dandy right so you see the power of this you can create a plan you can create a resource group you can create all of this great but how do i how do i initiate this guy this is where things get a little interesting right in order for you to be able to access any azure subscription right you need to create what we call a service principle think of a service principle as the guy that represents you your automated process in your azure subscription on your azure platform and that guy you send it commands you send the api calls and you tell hey create a resource group create an app service create a database and it will take these commands and it will go and provision whatever you're asking it to do all right so how do we go about creating something like that let's start from scratch from very very top you know nothing nothing before that nothing after that let's go ahead in here and here's what we need to do we're going to go and create an app registration so i'm going to go ahead in here and say magic what did i call my app i i have too many magic azure a z s k z easy x is the x okay magic azure a z x okay that's my app i'm gonna just create and register that app first and in that app you'll see a bunch of information in here right i have an application client id let's copy that guy let's go back here and put that guy in here i'm going to create a little method i'm going to call it create azure and i'm going to just copy that information so my client id is this guy what else do i need i need a tenant id from here let's go ahead and get the tenant id which is the directory id here's my directory id or tenant id right and the one thing that's really important above all of this is the secret so let's go ahead and create a secret right so it's a nuke secret my super secret and and it's going to give me this weird looking value and i'm going to put it in here don't worry about this i delete all of that stuff right after the demo so we should be fine so that's the secret right now i want to go ahead and create that guy how do i go about creating that azure guy here's what we need to do i'm going to go ahead in here and say this is azure credentials credentials like this so this is credentials and then out of these credentials i'm going to call sdk context i don't know i don't know that off the top of my head i'm just creating it i'm reading it from another place because it's not something that you would remember and then from service principal and inside that guy it's going to ask you for these things so i need a client id i need a client secret and i need a tenant id like this okay i need those three things i already have those three things there's also a fourth thing that i need which is the azure azure environment which is in our case here we will just go see the china german us government and known environments we're going to go with global cloud right because that's where we're at now let's just copy that data in there so here's my client id here's my tenant id and here's my secret now what now i have these credentials let's go ahead and return azure dot configure so this is my credentials and then with log level and i'm going to go ahead in here and say let it be basic and authenticate and this is the piece that i care about this is the credentials and then with default subscription your default subscription if you have multiple subscriptions that's a different story but in our case here we're using the default subscription which is the main subscription that you have if you don't like this guy you can always go ahead and say with particular subscription but then you're going to have to tell us what subscription you want to pass in there in our case here it'll be the default subscription because it's the only subscription okay i don't need these comments anymore comments are bad don't have comments in your code unless it's code that no one can read then go ahead and document it now i have this azure guy i can go ahead in here and say create azure like this and i can create all of these little resources right for for the time of this video i'm just going to comment those for now i'm going to show you the whole thing into end i'm just going to let it create this for me i'm going to go and give it a different name like magic azure resources okay so c sharp code creating azure i got the principal create and run let's go ahead and run this guy and let's at the end here let's have it say done just so we know that we're actually done let's go ahead and run this guy so just going back to my azure manager cloud here obviously this is going to fail and i'm going to tell you why in a second but this is all the resource groups that i have in this particular subscription there isn't much going on here if i refresh like this there's nothing going on if i run this this is going to fail at once why because i didn't give this app just because you created app and you have a secret it doesn't mean that this app has all the powers it needs right it doesn't have all the powers it needs to actually do something so this guy's going to go ahead and say the client id is null or something to that effect there it is so it's basically saying in here the client subscription id cannot be now because it doesn't have access to that we didn't give it x let's go ahead just for the for the purpose of this demo i'm going to go into subscriptions like this here's my azure subscription and i'm going to go into access control and then i'm going to add add a role role assignment and let's find the app that we just created now bear with me because this is very interesting we called it azure we call it magic azure something here's our app right there so i went i went into azure subscriptions access control uh check access ad and then i went to magic azure and i'm gonna make this guy an owner just for now right you can you can find the right role for it just for the purpose of this demo let's make it an owner for now so now this guy's an owner can actually take away and create anything it wants right now that i gave it that power let's go ahead and see what happens so these are my resource groups again and i'm i'm about to create a resource group that is called magic azure resources right let's go ahead and run this sucker here here we go and it says it's done is it really done let's find out if i go back here it should show magic azure in a second just give it one second sometimes it takes a minute if i refresh this whole page there it is magic azure resources do you see it it's right here so what does that mean it means with a little console application you can create a resource group you can create a an app service you can create a database every single product that you can think of that exists in azure today as a a cloud service provider you can create simply from a simple little console application that you have running on your machine if you give it the right credentials but that alone is not enough that alone is fun and games great you can do these things but if you're in a big enterprise you have a you know 20 30 40 team members engineers and all of them are trying to push code to be able to create instances and they're building you know ginormous into end microservices architecture you know you're going to need to standardize things a little bit right you're gonna need to create the code that doesn't have secrets in it it doesn't expose any secret doesn't expose anything about your your business but at the same time it can be deployed into your azure pipelines so you can actually execute on an agent and actually create create an instance for you let's start simple i'm throwing a lot of stuff at you i don't want to confuse you let's just make it really really simple let's go back to our blackboard here we did this piece we built a c-sharp code that execute an azure right but we can't just every time we want to create an instance we go to our machines and just run the script why is that because if you're in a 20 team 20 member 20 engineers team you know you can't just change the azure resources without having it being reviewed and approved by the team members right maybe your code has mistaken it maybe you're taking down an entire resource group as you can through this code you can actually start and stop an app service and you can delete a resource group like we're going to talk about you know the build and destruction for uh resources um you can't just run it from your machine you shouldn't be allowed to do something like that directly from your machine what should happen is that your your nice little code here should go through code review so that's source control let's say get you should throw it should through go through get like this and then from get it should deploy into azure it should be accessible so you see how the picture is getting bigger it should be accessible to a ado azure devops so the code goes right there and then azure devops will kick off the process of running that script to run into the cloud but azure devops is not doesn't know about your secrets because your code doesn't have this principal id and tenant id and all that crazy stuff that we just talked about we will get to that one in a second but for now i'm just gonna tell you that there's this amazing way you could have azure devops talk to key vault which is a very specific um way of storing your secrets so you can pull these secrets in and actually run it without having any issues you know storing your secrets or any of that stuff so nobody no one knows the secrets right only ado has access to key vault and the admin who set up all of these things is the one that knows about that but at the same time team members can push code and control instances in the cloud without having to know these secrets and without having to monkey around with your uh azure portal so let's just start small i want to create this guy so we got this guy already done we want to create this guy here this guy in progress so this guy in progress and this can progress and this is not started yet we want to push this code into a project in ado or get and then we want you know to have this run a very simple console app not even something that creates an instance or anything how do i just run a console app in general an ado that doesn't do anything other than saying hello world or done whatever let's go ahead into into our browser here and let's create a new project i'm going to call it magic azure azx i think i call it azx magic azure azx okay let's create the project magic azure azx just a simple project like this there we go okay and then it has a repo of course so let's copy that repo let's open up the command line here's my command line and let's just clone that guy in you can also clone directly through um microsoft visual studio if you want i am a little bit of an old school i do it my way so this is explorer here so this is how i cloned this code let's copy the code that we had in here this magic azure code let's copy that guy and throw it right in there so i'm going to copy this whole thing open folder and file explorer here it is right and then i'm gonna go back to my repo here and i'm gonna open my code from there let's kill that guy let's let's run it from here there we go so this is just me i just want to run something that says hello world so that means everything i have in here i'm going to hide i'm going to hide this guy you can't push secrets in your code that would be a big no-no because that puts you and your organization in danger so please don't do that let's just run a simple console application in azure devops so this is console right right line put done in there and then i'm just gonna push that code obviously there's a lot of junk in there that we don't need so let's just ignore ignore let's ignore all of that stuff we don't need any of that stuff yeah all i need really is the get ignore the program cs which has done and we have the azure let's push in this i'm just gonna say init project i'm just initializing my project and let's push that upstream there we go why does it still have init let's try this again commit all and push here we go i didn't stage them or something there you go this guy should okay here we go so that so i pushed that out so now i have this code out there in my repository so i have this code right here now let's build a pipeline task and this task all it's going to do is going to run this little console app for me that's all it's going to do and then we're going to add on top of it and i'm going to show you the magic that you can do you know to build a proper pipeline that can take care of your instances and all that kind of stuff so let's just go ahead to pipelines in here let's create a new pipeline and see there's the yaml stuff that i really don't like so i go with classic there will be another video where i'm going to show you how to how do you write the build steps in c sharp so you don't have to do yaml ever again all in c sharp but that's a different video so let's let's go ahead and build that pipeline in the pipeline i'm going to take advantage of the existing asp net core even though i'm not building asp.net i'm just going to get rid of the test piece this piece i don't need and i don't need this piece remove remove remove all of this i just want the restore and build and i'm going to add one more task on top of it it's called command line which is this guy that's my command line here and in my command line i want i want to be able to run my stuff right so i want to be able to go and say run this magic azure azx azx so how do i do that it's very simple script you can say magic azure azx.exe so that's the execution but before you forget you want to make sure that you find the right directory sometimes it won't give you that kind of directory right you're going to have to write it by hand so it's something to the effect of if i'm not wrong something like bin release uh knit 5.0 we'll see in the build but it should kick that off um accordingly so let's just go ahead and cue this guy and let's see if we can build this console app like that here we go this is really fun you know you can run any console application in ado and it can do anything you want it gets a little bit more interesting when you're accessing secrets and variables that are in your pipeline and the secrets are not really in your pipeline i'll show you that in a second here we go it's building building let's hide this guy i failed let's see why i failed so basically said command not found command not found command that found let's see why i said did i forget that little uh let's see edit pipeline let's put this on windows first so this is magic azure azsk exe i think that's right uh one more thing let's look if it's actually building things see see the build will tell you if it's putting things in the right place let's find out so then here if you're going to build it'll tell you this is where i put things this is this is your pipeline right here magic add your bin oh i probably i probably put the uh i always forget the uh it usually goes in the opposite direction so let me just fix that let's let's put that in here we go build okay this is all good good good command line okay let's just double check this guy yeah this guy is perfect and the command execution is perfect i just need to be running on windows then that's all i bet that's that's what it is let's go ahead in here pipeline windows let's go here let's try this again there might be a way to do it in linux as well i don't know what the command is to execute obviously i don't think linux will have an exe i think you're gonna have to run it as something else unless you have wine installed or something but let's just see if this one executes all we want is that this guy to first just run in the pipeline and then i'm going to show you a couple of crazy things that you can do and then finally be able to kick off that pipeline and it will actually provision a a resource group in the cloud while i'm at it let me delete this resource group that we just created so we can play around with it again just take that guy here delete yep let's let it delete and then this guy here will be basically kicking off that build leave this guy [Music] still running linux maybe we'll see yeah no it's running an image from windows so we should be fine here we go command line script boom executed did it print done here's done so that's it i just ran a simple console application in azure pipeline now think about the possibilities think about what we can do here we can access all environment variables we can do anything we want right but the trick here is that i want to be able to run this exact same script that i had originally this guy here with this guy without having to submit my variables in here how do i go about doing that this is the trick right first of all and and pay attention with me on this one because this is where where all the good stuff happens there's a little piece here on the side called library right this library allows you to create variable group we need to attach this variable group to something called key volt if you go back to the to the dashboard here so we did this and we did this this this is both successful right so we created both of these now i want to talk to this key vault guy this key vault guy will have the secret so i don't have to push them in my code the trick here is to put these secrets in there and then allow your um add your pipeline to be able to access these secrets without actually storing it and saving it locally this is the trick this is how you do this properly as an engineer so you don't have to expose secrets out there how do we go about doing that let's just go ahead and create a key vault here's resource groups secret group i'm gonna go ahead and create a new key vault here's a key vault this is where you save your stuff and let's call our key vault magic azure secrets like this standard sure create just a little thing that hides your secrets and all that kind of stuff and it has all the magic encryptions and everything else takes it takes it literally a second because it's basically a storage right so here it is a storage was a little bit of fun stuff on top let's give it a second let's go let's go so this is us running in here so while we're at it let's just go ahead in here group name how do we we need to connect that group to a key volt right how do we go about connecting this to a key vault i'm going to show you that in a second let's just variable group security this is all good just wait for it to allow access so you can allow access to all pipelines and you can control particular link secrets and azure key vault as variables this is where things get interesting right this here i'm going to call this magic azure secrets like this and then i'm gonna go find my subscription which is my subscription i need to authorize so now i'm authorized to access that subscription so i'm authorizing my pipeline this is this is doing some crazy magic behind this creating a service principle and then connecting it to the to the to the subscription it's crazy so that's this guy i i really appreciate the work that people put into things like these but then let's go and see if we can find let me refresh this guy this guy is going crazy let's give a second there link secrets here's my subscription and then let's see see if i now that i refresh this guy i see both key vaults that i have we did magic azure secrets right let's authorize so we're giving it the axis and now we can't see my variables i don't have any variables yet but i'm going to do that in a second so here's my variables in the key vault i'm going to go into the secrets like this and then i'm going to start creating some secrets so what are the secrets that i want to keep in there i want a couple of them so i want client id client secret and tenant id okay client id what's the client id is i want this value here so that's client id create let's create another one client secret let's put the secret in there here's this guy create the last one i want to create here is the tenant id i mean the client id and the tenant id to be honest is not really a secret but let's just keep them all in one place tenant id it's the secret that's really a secret but um here we are okay so i created those if i go back to this library and just refresh things a little bit see here see as soon as i clicked add now it sees all these three secrets that i have so i want all of them to be there so here's my secrets right and now all i have to do is save so now i'm saving this ah why did i call it new variable group let's call it magic azure secrets so that's the library that's that library piece in here it's far far away from your azure and now if i go back to my pipeline i can modify that pipeline i can edit that pipeline like this and i can go into the variables and then variable group and then link to variable group and now i have my three secrets right here see this magic now it's linked now the question is how can i access these variables from that console app so i can create resource groups from the pipeline how do i do that it took me a little while to figure this one out it's not exactly something you can find online but here's what we can do well we can do and there could be better solutions out there if you go to environment valuables variables let's create these things azure client id and i'm going to access that guy like this client id see this let me actually hide the camera here for a sec see this guy here on the on the side i'm basically creating an environment variable and that environment variable is reading straight from these uh secret group let's do this again azure client secret and that would be client secret and let's do that little dollar sign and then azure tenant id and in here i'm going to go ahead and say tenant id like this so now i have access to these environment variables and i'm going to show you now how our console app can can okay and even if you try to print these variables out it will print it out like stores which is beautiful that's added security to it it won't let you actually expose this or print this out only to utilize it okay so i'm gonna save this guy just saving it now let's go back to our console application and instead of saying here's my console app instead of saying go ahead and pass this secret i'm gonna go ahead in here and say environment watch this dot get variable get environment variable and in here i'm going to say azure client id and the same thing happens with each and every one of those watch this azure client id and in here i want azure client secret and in here i'm going to say azure tenant id all right so now i don't have any secrets in here my secrets are being pulled through the pipeline and assigned to environment variables that are also secure and i'm just reading these environment variables and moving on with my day so just to make sure that we're actually doing the right thing i'm going to change the resource group in here the name of the resource group in here a little bit so you can so you know that this is the new change right so in here let's go ahead and say provision code so i'm pushing my provisioning code in here so push all here's the code going up up into get an adl so this is gone and now that i have those now let's go ahead and queue and see if that works fingers crossed i am looking at my resource groups in here and i don't have anything in here that indicates this new azure group that i was just talking about by the end of this run i should be able to create an azure resource group in the cloud without me having to touch azure dashboard that's basically the goal so that means a whole bunch of engineers can submit code the code gets reviewed and then after the code review you know the the the deployment or the kickoff of the process is what's actually going to make this happen in the cloud so let's see if that's really the case let's give it a shot there here we go restore moment of truth so many moving parts it's not really that simple but hopefully i'll be able to simplify it even further there you go restoring and building let's take another look at our code so look what it's doing it's basically pulling these environment variables that i created and i assign the secrets to them from that variable group and then it's going to use that to actually create very secure very beautiful let's just see if it works so this guy says i'm done is it really done did it actually create the security group we'll see we have to refresh azure does this all the time do i have it do i have it we didn't see any failures which is good and this guy is completed let's just make sure that our code actually yeah we went and said create this resource group yep that's all good so we should be able to see a resource group in here there it is do you see it new magic azure resources straight from ado to to the cloud and it's creating resources for you that means you can create anything you want right that means we completed this part this part is done see how simple that was it wasn't that simple to discover but now it's very simple we created c sharp code put it into the ado attach ado that key vault and then key volt push it to azure now what's lift to actually turn this into something that people could use you want to leverage the broker service exposure kind of pattern to allow people actually to use the system so we can't just go and tell people here is the system now that you can access a console application go ahead and use it like that we don't want to do that we want to organize our world a little bit because we need statuses we need you know to understand you know all the bits and pieces of the system we need to make sure that your code is easily extensible and even testable if you want to test it although that i think testing your infrastructure code is a little bit of an overkill but you know some people might go to that extent if the code is clean enough so here's what i did let's go back to the blackboard let's go back to the theory the theory is i want some brokers and i want some services right so i need a i need few things i need a cloud broker and the cloud broker is basically something to abstract away the azure sdk far far away from my lodge logic kind of logic right that your infrastructure logic and i also want a logging broker and this login broker is what's going to tell me okay i created this resource group i created this database i created this i created that all of these little things and then i also want you know on top of all of this um so you got the cloud you got the logging broker what else do we need to complete this little of course i need a little bit of i need a service i'm going to have use two services in here one is a foundation service and one is a processing service and this guy is going to use each and every uh one of those um the last one i want is a configuration broker something that reads those environment variables for me and bring them down to me right because that's very important so the configuration broker so each one of those has a responsibility this guy will expose apis that i can read to create azure instances so this guy will give me the capability to create a a resource group it gives me the capability to create a database an app service eq all the things that i need and this guy will help me report what's happening in the process as i go because it takes a while to provision a database and an app service and all that so it's nice to see a log of how things have happened and then this configuration broker will talk to a file and this file will have which environments the team wants to create or destroy so it's a little app settings in here right and this is here just a json file that has your configurations what environments you want to create and what's the name of your project push this through source control don't let this come through variables because that means anyone could just type it in there and create you know resources it has to be source controlled right so all of this is source controlled and then you want to have a provision service and you also want to have a provision processing service so what's the what's the responsibility of this and what's the responsibility of that this guy will combine all of these sorry just these these two mainly right and it will go and say go find the configurations create an instance and log status this guy here will say iterate for each and every environment and at the very end in here that would be the executor which is your exe this is the exposure which is your program cs like i told you before it's not always a controller it will never be a controller all the time sometimes it's a ui sometimes it's a it's a console application sometimes it's anything so if you structure your infrastructure code like that that means that you are allowing everyone on your team to be able to contribute to creating new instances or destroying older instances or creating new environments it's all source controlled but at the same time it's intuitive it's c-sharp it's a better engineering experience then creating these you know templates and you know doing yaml code and all of that stuff we don't want to do any of that we just want to do it in c sharp so what does that look like in real life let me show you the execution first let's go to one project that i created here so here's a demo project right let me execute first let me show you if you want to say see what it takes to make a demo like this look at all these trials and errors lots and lots and lots of trials and errors okay let's let's first go ahead and run the pipeline i'm just going to run the pipeline i'm not going to do anything i'm just going to run the pipeline and show you what happens these are my resource groups let's delete this one since we don't need it anymore this pipeline that i'm running right now it's going to create two environment dev and you at development and user acceptance and not just that on top of all of this it's going to go and say create app services databases and uh plans everything related to an environment that you want to create so pay attention to what's going to show up in here this is going to be you know what this pipeline is doing let's take a look at this pipeline this is a full enter in production ready you know code that can actually run everything talks to key vault it reads you know everything you need it basically talks to ge volt and it just and i'm going to show you the code in a second but i want to show you what it actually does so you see this command line in here see what it's saying in here let's let's zoom in a little bit and hide the camera look at this see creating z-o-o-g-i-i resources dev zeo gi resource div created see all of this is configured it's creating the entire literally the entire environment for me if i go and start refreshing this guy here do you see z-o-g-i-i for dev and it's creating the plan it's creating everything literally everything for me in here so let's go back to this guy let's just make sure it succeeds so i created a resource group it created a plan it created an app service which is an api and now it's creating the database server let's make sure it makes the database server right all of this is coming from azure key volt it's coming back and it's processing and creating these environments you know very seamlessly all in c sharp and then i'm going to show you the code in a second you're going to love the code a lot the code should definitely match 100 what i just described to you right here right so is every other project in the world right they should have brokers that talk to the outside world so this cloud broker talks to azure sdk this guy will just talk to it literally talks to console.writeline but it's an external resource it's not something that we own so it has to be pushed away just like daytime and then the configuration which to read from a json file let's just see if this guy is done see some of these things take a while like provisioning a database server and all that kind of stuff and sometimes they may fail this is why it's fail safe like if you run the script again it's not going to blow up and tell you oh we already created this resource okay it looks like i created a db server so if i go back here and refresh this guy look i have the i have the api right here and i have the plan and the database server should show up really there it is and it's not just that it's creating the div environment it's going to create two environments for me and i'm going to show you how simple it is to be able to create the um to create the configuration because everything is already in place all you have to do is either to add an additional capability that you want to use or you want to you know just add another environment just add another environment just add another environment and that's it obviously this is this is very very like new stuff you know i understand people are gonna have a lot of questions i'm gonna use this in my real work real environments you know just to see how far we can extend it and how how much production really it is but it's definitely very very promising all right so back to this guy if i refresh here this guy should be done if i go out of here it probably already started working on the you at sea it already started working out so it's spinning up all three environments for me you add div you know um just like that right i don't think i included that in the configuration the prod one but it will create two environments for me so it's already in the database server so if i go down here and i refresh this guy should show me you at let's refresh this guy here's refresh here's my you at environment c i got z-o-o-g-i-i z-o-g-i-i resource you add an inside you add that's creating all all the things for me beautiful full automation for your provisioning work now that we talked about the automation now that you know it's doing its job and it's finishing this let's go ahead and take a look at the actual code what does this code look like and don't worry i'm gonna share this with you on github you're going to have everything you need no worries let's just start from the top so we have a cloud broker an icloud broker and a cloud broker and this cloud broker basically goes and reads all these environment variables right and we could definitely make this as part of a configuration that's not a problem it gets the client id client secret tenant and then admin name and admin access admin name and admin access is basically what you set up your database for who is the admin and what's the password what says their name right so that's not a big deal and this is the code that we have to authenticate azure so all of this is encapsulated within the root file and then see every other partial class after that creates a plan this one creates resources you know with the input parameter it needs let me zoom out a little bit and then you have storages this one creates a sql server and a sql database you have web apps it creates an app service this is your cloud's broker and your configuration broker it's actually reading from your app settings your app settings is where you describe what project you want to provision and what environments you want to spin up and then you determine each and every project in this particular code i'm making each and every project as a a project that needs an app service and a web api a web api a database and a plan that's it but if you want to add stuff on top of that by all means go to the broker and add whatever you want so this is a configuration broker right here and then there's the logging broker and this login broker basically calls console right line every time something happens it just calls console.writeline if you move up just by one into the services provision service you'll see all of these things that i offered through the broker are still there but they're encapsulated with more things like if i open this guy here it'll basically fix the formatting of the name it will log that it started the creation process and then it will kick off the creation process and then it will log that completed and then it will return the actual instance so that's this part here this is the guy that takes each one of those and it and creates little still primitive primitive methods but added a little bit of stuff on top of it if you move up to this guy the provision processing service which is this guy all what this guy does is that it goes and calls the configuration broker it picks up every single environment and it creates for every single environment see that separation of concern one adds it's one step one step one step one step until you reach the processing service where it goes and say see how simple it is create a resource group create a plan create a web app create a sql server create a database inside that sql server done cleanest thing you can get to you you would love to test something like that because it's so test driven it's so uh easy to work with and you know performs interfaces not much for dependency injection because it's just an infrastructure code if you go at the exposure the equivalent of a controller or ui component this is it that's your guy it goes and says initialize i provision processing service and just provision provision what provision whatever in this app settings you see what i'm saying so if i go back to my pipeline here here's my pipeline it's basically saying i provisioned everything if i go to the cloud here everything is in place it has a database it has everything in place both environments has been created within two minutes maybe 10 minutes you don't have anyone touching anything some people might come and say well i want to add particular settings into my app service absolutely everything is accounted for in this api if you go into if you go into the your api here for the clouds if you go into web apps in here into the implementation everything is accounted for like if you go here you can go and say let's see there is web app settings you can set up the settings you can set up one just one value for the settings like key and value or you can set up and and again the same pattern if the if the values that you want to set up are secrets secret values then put it in key vault and pull it into key vault through you know your system if it's not in key vault if it's not a secret then go ahead and define it in your configurations it's all available i'm going to put this on github i'm going to drop the link in the in the in the description uh sorry about the super long video it's almost an hour uh but uh you know that's that's pretty much it it's it's it's a completely different mindset you know you don't have to deal with arm templates uh you don't have to deal with any new technologies that you don't have to learn just it's all c sharp it's all making sense it's broken into components applies strongly religiously applies the principle of a separation of concern and single responsibility and at the same time the configuration is very simple and very easy you know i will run through the entire process in a semi production environments on the enterprise level and i'm going to share my experiences with you this is this is much easier and again it you know the first class citizen is your engineering experience you are the target of doing stuff like this as an engineer to be happy to be able to write code and control it doesn't matter whether you you don't have to you know do something crazy to get access to azure so you can provision your own instances by hand or learn a crazy technology all you have to do is just create a project like this in your solution and have your whole team set it up right set up that pipeline like i showed you to run that console app and the rest is history you know you just configurations add new things just making sure you're doing the right thing it's source controlled your team is looking at the code that is going to execute and create instances they don't have to worry about learning arm templates anyone literally anyone that knows anything about c sharp should be able to look at your code and know what it's doing so i hope that was useful to you and again i apologize for the long video if you have any questions or comments or concerns please free to drop a comment in the comment section i will put all the links and all the things that i've showed you today in the description and don't forget to like and sub and like and subscribe thank you so much for watching and i'll see you in another video
Info
Channel: Hassan Habib
Views: 1,248
Rating: undefined out of 5
Keywords:
Id: ht0xhQyF1x4
Channel Id: undefined
Length: 55min 41sec (3341 seconds)
Published: Mon May 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.