Infrastructure deployment in AWS using Terraform with Github Actions.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] today all this is sudakar welcome to my troubleshooting and debugging Channel in today's tutorial we are going to learn how you can use GitHub actions for deploying infrastructure into AWS by using terraform what is GitHub actions first of all ideally when you have written some code and you want to perform any CI CD operations first thing which comes into your mind is a Jenkin so you will create a Jenkins Pipeline and when someone push the code into one when some developer push the code into a repository you want to kick the you want to kick out the Jenkins Pipeline and it will deploy the code into a Target machine or Target environment that is what you do but when you already have a provision in GitHub let's say your repository is a GitHub repository and you have chosen the GitHub as your version control system and you are already storing the code whatever the developer written into your GitHub why don't you use the GitHub actions for performing the CI CD operations that's where I'm going to explain you how you can write a pipeline in GitHub actions and how you can deploy a simple easy to easy to instance deployment into AWS environment by using terraform here in this demo we are going to perform two actions first we will write a local terraform file for spinning of a new new ec2 instance using terraform and we will put the terraform code into a GitHub actions and we will see how GitHub actions will create or destroy the instances so these two actions we will see first without further delay let's get started so I will start writing a terraform file first of all I wanted to create a empty repository let me go back to my web browser and let me log into my repository and here is my GitHub account and I'm click on new and here demo GitHub actions terraform so I would name it like it this is my repository name and public I'm not worrying about that much now so create repository here and this is my repository link let me copy the Repository open my visual studio code and here I'm going to clone the Repository git clone I think yeah git clone and repository name so I I actually clone the repository into my local system first I will start writing a simple file called main.tf and in main.ftf I want to write ec2 instance let me find out the steps how I can create an ac2 instance in terraform so I can simply browse for create ec2 instance in terraform AWS so here I will I will opt for a official terraform documentation for creating an easy to instance so this is my this is the official documentation and here there are there is already a configuration example available so in this particular demo we are not focusing much on to how to create a terraform code and all but our major focus is on how you can write a GitHub action pipeline so let me grab this content here and I will directly use this one so here you can see it's it's directly trying to trying to get the Ami first uh it's a flavor of Ubuntu and by using that Ami it is simply creating an ac2 instance that would be all we are doing here so we're not much we are not doing anything more so let me copy the content and put it over here so this is my major main file and let me try to execute this terraform so before you uh start this demo uh make sure you have a terraform installed in your local machine so let me execute terraform so I have my terraform already installed the terraform in it so first I need to do terraform in it so that terraform will initialize all the required files the directory has no terraform configuration file so it's pretty much problem so okay the problem is I need to login I need to enter into a repository so here is my repository and let me run the terraform in it again terraform in it so basically it is trying to initialize the data form perfect so terraform is successfully initialized and you can see it created terraform temporary files on terraform plan let me find out the plan for it so even uh you also need to take care one more thing so along with the setting up a terraform we also need to set up AWS credentials so I also I already configured my AWS credentials in the in the current CLI so I'm not going to touch anything right now in this demo so you better take care of configuring the AWS credentials and installing the data form once you have that prerequisite done so you can directly come here into the demo and so I'm gonna run the telephone apply so I ran that error from apply meanwhile let me so let me click on S so you can see here it is creating the AWS instance with the name web let me go back to the browser and I will log into you login into my AWS account and this is my AWS account I think I was using Mumbai region let me switch to Mumbai region and let me find for ec2 so VPC ID is not specified so since I don't have any default VPC in the default sorry in the in the Mumbai regia so I will do one thing I will configure reconfiguring AWS you try to change my uh uh region by using AWS configure option so AWS configure and here I'm not changing my user access key and security key but I'm going to change my region so I have a default VPC in Us East one and pretty much everything is fine so let me run the apply again then I from apply so it will prompt you for confirmation is and it started creating the instance so let me go back to my AWS console let me change my region so it's a Us East one northward Junior region and you can see here it's all over uh so it actually started creating the ec2 instance okay seems uh apply is completed one resource is created so you can see my ec2 instance is created successfully now this is this is the this is all we are doing in the terraform side so we are not going to touch anything more now let's see how you can run a pipeline in a GitHub actions when when you want to commit this file so let's say let me commit this file first get status so I don't want to commit the terraform State file and all so let me add them into git ignore dot get ignore so here I don't want to commit terraform terraform file dot terraform files and terraform TF State file start so I don't want to commit this these four files get status git add I want to add my main file and also git ignore okay it seems like this is not under the repository that's why it didn't look up flashlight Rea so this file is now now git status so I have these files now yeah so main.tf and Dot get a glorified these two files I am committing it commit my first Commit This is my first commit and git push origin my branch is mine so I committed my code into git repository so let me go back to my gate repository and this is my code so you can see uh we have committed our terraform file here now in GitHub repository itself there is an option called actions this is what exactly GitHub actions these GitHub actions will provide a facility or an option where you can configure pipeline that means when someone commit your code into a particular repository you want to trigger you want to kick off this GitHub action Plugin or sorry GitHub action pipeline or if someone create a pull request from one branch to one another Branch so when when someone create a pull request to the specific Target Branch you want to kick off the pipeline so this this way you can configure uh GitHub action and GitHub action will will perform a cicd pipeline for you so here when I click on this actions you can see there are plenty of plenty of options all already available by default and he's suggesting a simple workflow so our use case is terraform basically you want to perform you want to deploy or you want to execute a terraform configuration or terraform commands on a GitHub action so setup data from CLI in your GitHub actions workflow that is what exactly uh we wanted to do let me click on configure here and you can see uh this is the path it is trying to create a file so you can see clearly here it is it is going to create dot GitHub inside a DOT GitHub it is going to create a workplus folder inside a workflows folder it is trying to create a terraform.aml file so let us go ahead and create this file but it seems like pretty much used to understand uh for a beginners so we don't want to use this file as it is but we wanted to create the file by ourself so let's take this path as a reference and go back to your uh visual Visual Studio code development environment and here what I will do I will try to create a folder called the same folder which they are creating dot GitHub inside the dot GitHub so another folder called workflows inside the workflows as he's suggesting terraform Dot yml5 so here we are going to write our GitHub actions pipeline so let let's go back to the repository and let's see what exactly he is doing here so in order to use these configuration basically uh in in GitHub actions the pipeline is going to run on One agent that is called a runner in GitHub actions we call it like a runner in jenkit we call it like agent in runner it could be any Runner so you can you can select a runner on which Runner you want to execute your GitHub action pipeline so you can you can choose Ubuntu Runner you can choose the Centos Runner you can choose there are Docker Runners there are multiple options are available we will talk about the we will we will talk about them in a bit so when you when you run a pipeline when you run a terraform you need a terraform plan and a terraform apply on any of these Runners the problem here is the terraform State file will be stored inside the runner and that Runner is a uh non-volatile so it you will not get that Runner again the runner will be created on demand it will run your pipeline and it will be destroyed so when the runner is destroyed your terraform State file also going to be deleted so you will you will lost the track of the terraform deployment for that case when you want to utilize the terraform sorry GitHub actions pipeline or workflow you better you must create a backend uh backend should be some some different different place it should not be a local machine basically so what we will do we will create a backend as a S3 bucket so in order to configure an S3 bucket let us see how you can configure a S3 backend bucket back in terraform so here if you see here even in my local when I when I execute the terraform apply you could have find that it is created a data from State file in my local so this will work only insert local if you want to execute the same terraform code so ideally in organizations in this particular terraform code multiple dollars developers will work and if you execute on your local the terraform State file bound to your local machine so that will not work for the other developer who is working on the same terraform code for a different modules so for that you better need or better have a different common backend for terraform State file so let me go back to AWS console and first of all let me find how you can configure a terraform back it as S3 so here there is a sample configuration so let me copy this configuration I will create one more file called backend backend dot TF file so this is my terraform backend configuration here you need to configure a bucket name so our bucket is uh GitHub actions demo so as this name is the unique the bucket name should be unique so I'm just giving some random name here and let me copy the bucket name and I will go back to my AWS console and here go to S3 here once SK Market is S3 page is open here click on create bucket and here I will name the bucket name same as the packet name which I configured in my backend and I'm leaving the configuration remaining configuration as it is click on create bucket so now the bucket name is bucket name is created here and the same name I configured here the keys nothing worked on which name you want to store this um uh this particular state so I'm just I'm just giving a name as a GitHub iPhone actions iPhone demo.tf state so this is the name I'm providing to that file so yeah that would be that would be the configuration for uh backend let me re initialize my terraform here see how exactly it will populate the output terraform in it and terraform plan so basically once you configure it you need to reconfigure initialization so terraform init reconfigure and terraform okay I think I there is a problem so this file is again not inside the repository that's why it is not picking up let me move the file into a repository now it is under the repository so when I click this uh terraform unit I was expecting it will it will throw some error or message yeah similarly like this so do you want to copy existing state to the new packet yes because currently my state file is in my local and if I want to run the same uh terraform code in a GitHub action it it will not uh use the existing configuration instead it will try to create a new terraform file but before we do that action we want to migrate or copy our local terraform State into S3 bucket so that as so that when you run the same pipeline in GitHub action it will pick up the same state file and it can understand if the instance is already created so yes I want to copy this file let me click on S so successfully configure the backend now you can see successfully backend actually is configured now let's go back to the S3 bucket and see if the file is created yeah so here you can see GitHub iPhone actions demo.tf file this is the state file uh we have migrated now now we need to commit this file as well so let me commit this file get status and before commit this file so as we were writing our GitHub action pipeline workflow so let's go back to the pipeline GitHub terraform.aml file and here so we need to write our GitHub action pipeline now Let's uh let's try with the name so first of all you need to configure the name so you can take the existing here you can take this as a reference as well if you want to write so you can write this so you can just copy this information name the name is this data form on push branches main so our current branch is main right so on push to the main branch you want to execute this pipeline that is what it meant so I don't want to pull request right now so this is this is the basic configuration I'm trying on push branches is main so what else is the configurations here so yeah here you can see the job is terraform and runs on this so and so and also there are multiple options let's copy each action step by step and understand each one separately now here you as you understand this is on push to this Branch you want to run this trigger and when which which jobs you want to run so that is where you need to configure under jobs so you can have multiple jobs uh together to execute on a single pipeline so you can have a terraform or you can have some let's say you have you have a Docker file where first of all you want to build a Docker image and push it to a central Docker repository in a second job you want to consume the consume the docker image to deploy into a kubernetes cluster somewhere else so that say similarly you can have multiple jobs together so that's why we have a section called jobs inside jobs uh you will configure our we are configuring terraform which is a name holder terraform and the name and here you see so this is exactly the runner I was talking about the runner is nothing but on which uh on which server you want to execute this pipeline that is where uh here Ubuntu latest so in a behind scenes what is happening git GitHub GitHub actions will download this Ubuntu latest Docker image and on top of the talkative image it will try to clone the repositories so that is exactly what is happening in that GitHub pipeline now let's go back to this configuration and see so here you can see inside this um uh terraform job so we are going to check out our repository so let's copy this checkout option also so under under the terraform job I have a steps and the first step would be my first step would be check out the repository so check out the repository into GitHub actions Runner so this is the configuration I need to provide and the second option would be let's go back here so install the latest version of forterra from CLI and configure the CLI option so this is this is something so he is trying to set up the terraform using terraform Cloud but currently we are not going to use terraform Cloud but instead we are directly going to use a runner itself and by simply invoking terraform CLI command we are going to deploy a pipeline sorry we are going to deploy a terraform infrastructure into AWS so before that so I will I will I will write the init option directly so let me copy the init option maybe this this action may not work exactly for us so I will show you how you need to tweak a little bit so here we are trying to initialize the terraform so here um let us specify another image uses and here I I want to use a sheet card and I have I have seen this image earlier so that's why I'm directly uh putting it putting up here here so GitHub actions terraform GitHub actions and it is there a master Branch so basically uh terraform GitHub actions for image I'm going to use a Master image and inside that I want to specify some match some options it's a TEF actions TF actions version so I want to specify uh which terraform option which terraform version I want to utilize because uh if you don't specify this option it will always pick up the latest one maybe if you already have a terraform configuration which is running on a older version that may not be compatible with the latest terraform version so it is better always use uh the the compatible version for your existing environment so 0 14 3 and I want to specify uh GitHub actions and sub command so the sub command would be init so what I'm trying to do here I'm trying to do init so I'm not going to run this run command so let me remove it instead I'm going to use these options and my terraform actions current working directory I need to specify this working dir and here working directly is in the same location same same place where I have this file and TF actions comment so I just wanted to comment through and arguments so I need to pass some arguments let's say if you want to pass any arguments like uh you want to specify which region you want to configure or you want to configure which backend configuration so we will come to this part later so currently I'm not going to use any arguments so here uh we are we are initializing the terraform by using a different image than what it is mentioned in here so we're not going to use it but because this option is meant for terraform Cloud so we are not configuring any terraform cloud that's why I I change this option so once you configure this so next one is a terraform plan so let me add a section for terraform plan as well so terraform uh plan and I'm gonna use the same image as the previous one here also I think it's a GitHub actions I'm sorry about that okay GitHub actions so I'm going to use the same same configuration but here I may need to change the sub command so sub command in this case is a plan so my sub command is plan so terraform you need terraform plan and the other section would be apply so I want to configure apply as well here the action is applied Okay so so far we have configured uh a simple GitHub action pipeline where on push on Main Branch I want to run this Pipeline and the job would be the terraform job and which runs on Ubuntu latest platform and my steps are first I need to check out the repository and I wanted to initialize my data from terraform code and I want to plan it and then I wanted to apply so here it is apply so that's all so let's Commit This code okay I think I forget one more thing so as you were able to log connect from my local terminal to AWS because I have configured my AWS credentials inside the inside the terminal so you need to tell in a GitHub action pipeline which credentials you need to pass or which credentials the GitHub pipeline can use uh to connect from GitHub Runner to AWS accounts so that is where you need to provide AWS access query and secret key as part of environment variables so let me add one more section here called environment variables and here we need to configure AWS secrets AWS secrets before you configure AWS Secrets here uh let's go back to AWS sorry uh GitHub console and here you can see there is option called settings first you need to store these credentials somewhere inside the repository in a secure manner so we we are not going to uh copy paste our credentials in the git repository which is highly vulnerable vulnerable and it is very um uh easy to hack hacked by uh some hackers so I have a situation where um uh I have seen a situation some some people are trying to store the AWS credentials into GitHub repository and they are not those those secrets are pretty much active so anyone who has access to I mean when you when you configure this repository as a public anyone can anyone can find this repository and they can grab your credentials and by using these credentials they can they can spin off n number of instances or they can do whatever that that particular secretaries as permissions so it is always best practice do not put any AWS access key secret key inside the code now here when you click on the settings you you come down come down to the security section here you can see secrets and variables so click on here and click on actions so here uh you want you can specify uh environment Secrets or repository signals so I'm gonna use environment secrets and create new repository secret here I wanted to configure so and a secret called something like AWS or devops access key ID here I will paste my secret click on ADD and also let me create one more repository Secret or manage environments what is it no no I think uh Secrets only the right option so actions click on new repository secret and I will I will name one more devops secret access key and here I will configure my my secret key so let me put any effort now but I will come back and I configure it so let me I think the previous one is not configured because I did not specify the secret so devops access key and I'm just keeping it like a null but uh in a bag in a behind scene I will configure you you also better configure your credentials here and so this is the repository Secrets which I which I have added now and now I need to use the same uh Secrets inside my code so let me copy this option in a in an environment section here so I will configure AWS so I better put them in caps later so AWS axis key ID and that is here you need to fetch from secrets so Secrets Dot and you call you paste that uh ID which you created I'm sorry uh so and the second one is AWS secret access key and here you need to paste the secret access key ID so let me go back to my AWS sorry uh GitHub console copy this option and paste it here so now you have configured access key and secret access key so when when the pipeline execute it will fetch the access key on secret access key from these two environment variables and it will try to connect to the AWS I think we are pretty much done uh meanwhile I will take a little pass and I will come back uh so I will take a pass to configure my real access key and secret key okay I'm back so I have configured my access key and secret key in GitHub action secret section so let me try to uh push this code so now you can see you have added a backend and also you have added your first GitHub actions pipeline so let me Commit This code hit add git commit and you can hear a GitHub actions pipeline git push origin which is my main branch so let me push it to Main so let's go back to code and you can see my GitHub workflow is created so you can see here so the file is committed and if you have observed there is a Mark here so some Mark is popping up here and you can see one in progress check so we will see what it is actually why it is showing like that so that means you can see here a pipeline is already triggered so go back to actions here and you can see the pipeline is triggered so here our pipeline is required and it is failed let's see why it is failed so our job is this one and here it is failed because it is not able to find so configure terraform AWS provider validating provided credentials retrieving caller identity from operation HDR so AWS region is required but was not found so this is the mistake which we did we need to provide AWS reason as well so it is it is uh failing to find out the reason so how you can configure the region so let's let's find out how you can configure the region as well so here what I can do so I will do one thing what I can do I can configure a pro one more file called provider dot TF and this provider.tf I will configure provider it's awf and here I will specify the region region my region is Us East one with Junior region so here I have configured my region now let me commit this file status it add git commit and my commit is added provider file get push origin name so as when I when I push the code into main branch so you should be able to see there is a pipeline trigger so you can see here a pipeline got triggered because we triggered into your main we committed a code into a main branch so here you can see it is started the pipeline and it is trying to execute terraform in it so you can see here it's a downloading terraform 0143 that is the version We specified and you can also see successfully configure the backend S3 and terraform has been successfully initialized now and now it is executing terraform plan here you can see it is successfully able to identify the previous plan that's why it is showing no infra changes and it is successfully uh able to identify our previously created instance that means now the backend S3 successfully configured inside the GitHub actions pipeline as well and obviously the terraform apply will not will not will not do anything else so and this is how the pipeline has been created and executed in GitHub actions now uh this is pretty much cool so so far we have successfully created our first uh Sim first GitHub action pipeline now if you want to customize uh this pipeline by providing different options let's say uh I want to specify in my ec2 instance and here I want to specify a name of the instance variables or I want to specify which type of instance I wanted to configure so how you can do that so let let me put the variable here variable uh instance underscore type so I don't want to hardcore this value but instead I want to pass this value so in order to pass this value here you can see uh where dot instance type so this is the configuration I'm setting up and let me create one more file called variables dot TF so inside the variable.tf I need to specify this variable so the terraform will understand so it's a variable I'm not configuring any default value here I'm just keeping it as a as an empty but this value we will we will see how you can pass inside the uh pipeline uh GitHub pipeline now this instance type you need to pass so let's try to execute terraform plan here and we will see how it will work terraform plan so you see here a interactive interactivity is asking to provide instance type but how you can do the same interactive output or interactive input in the GitHub actions right so you cannot do that but instead what you can do here you can you can provide where iPhone so you can specify here uh what option or what value you can provide so where equal to my option is instance type so instance type you can configure here you can pass it as a variable and here uh TT earlier it is like T2 T3 small I think so now I will change it to T2 medium that's all so you are passing the variable to the plan in the command line so let me execute this and yeah so now you can see it did not ask for input in an interactive mode instead it has taken V2 dot Medium as a variable which we have passed so you here you can see the drift has been identified by tarafa and now we are trying to switch T3 dot micro to T2 media that's exactly what it is doing now in order to do the same task in a GitHub pipeline how you can perform it so how you can perform this option here that's where the arguments comes into picture so in in this particular unit section and if you see we are using this terraform action version subcommandroid so similarly you can also pass the variables in this section by adding one more option called TF actions oh sorry its arguments so arcs inside the aux you can pass iPhone VAR and here you can specify iPhone where equal to the same thing whatever you did there you need to pass here so instance type instance type equal to T2 dot medium T2 Dot media so this is the configuration uh you need to do I think you need to put this in double cut so my iPhone battery equal to uh instance type variable on Twitter median so that's all so you need to add this line in plan and apply as well okay okay one more thing here uh you are you are trying to apply a terraform but if you see when you execute apply here let me go back here let me try to run the apply command I'm sorry about that so here it's apply once once you execute terraform apply by passing this variable it will ask you for confirmation yes or no but how you can do this in a terraform so terraform has an option called Auto approve so when you run this Auto approve it will automatically approve the execution so you need to add this parameter in the apply command so here uh before you pass the variable let's add to approve so that it will not ask you for confirmation so here you are passing hyphen R2 iPhone approve and then variable variable instance type okay that's all so that's how you need to provide and let's go ahead and commit the code give let's check the status again and you can see we have updated terraform file let me see the difference so here we are adding these arguments and here also we are adding the argument and we are changing the instance type hardcoded to variable that's all so let me commit the file with ADD git commit variable instance type that is what I'm doing get push or is it Main so I have committed my code the pipeline should be automatically good let's wait for some time here so you should you should be able to see your job is still good and let's click on here so pipeline started and it is trying to execute uh the plan again and then apply so let's wait for some time okay here you can see a terraform has been successfully initialized and the plan is successfully executed and you can see instance type drift has been detected and now terraform applies executing so here you can see successfully uh applying terraform configuration in this Dot so let's give a two minutes so our meanwhile you can go back to uh AWS console and go to ec2 instance ec2 section so there you you should be able to see the change so basically what what it is doing because we are changing we are trying to change the instance type instead when you are trying to change the instance type what will happen it will destroy the existing instance and it will try to create the new one so you can see here it's currently it's a stopping so in a back end it is executing stop command first and in a short in shortly it will try to create the new instance as well so currently stopped let me replace this page ax will So currently it is executing Let me refresh this page so you can see here now it is instant state is in pending and instance type is data medium so it started creating the new instance with our variable parameter which is a T2 medium let's go back to the Jenkins sorry uh GitHub actions and terraform apply still running you can see here how long it is been running it's been running since 1 minute 45 seconds so approximately it is taking two minutes to redeploy the instance and yeah that's all so you can see here the plan is successfully executed it is trying to modify and still modifying and it took almost one one minute 40 seconds to modify and in one minute 41 seconds it created the new instance I think we have so so far we have seen how how you can configure a GitHub actions Pipeline and how you can configure secrets and you can specify the runner checkout and get in it hit plan and get apply actions as well now one more section I wanted to show you here now as you as far as you are executing only terraform apply let's say you want to delete the instance now whatever the instance you deleted you want to delete the instance in the GitHub actions itself so how you can do that in order to delete the instance basically here you need to add one more section but you cannot change the code uh every time you want to perform apply or every time you want to destroy the setup you no need to uh update the code every time instead what you can do you can specify what action you want to perform dynamically that means you no need to hardcore this option or you can have a condition where when user has chosen apply action apply action then only you want to perform terraform apply when user perform you want to destroy the setup when you choose the destroy option then you want to perform this try option so let's go ahead and add one more section here called terraform destroy so let me go here and add one more section it's a terraform destroy so in this terraform destroy what I wanted to do I want to destroy the destroy the configuration so here what I will do destroy here is a destroyer now you need to specify the input parameters so how you can perform the you can provide the input parameters here in the on Section you need to add one more section called what flow so you need to provide workflow dispatch inside workflow dispatch you need to specify inputs the input is for our input is we need to provide action right so which action you want to prefer is a terraform action so just a name terraform action and the terraform action type is uh a choice it's a multiple choice I mean uh drop down right so you need to specify the choice and you can specify a simple uh description so select terraform action something like that and inside that terraform action you need to specify what all options I have so what are all actions I can perform so you can perform apply or you can perform destroy these are the two actions uh terraform supports so I mean in order to create a dislike so these are the two options uh from the data form so you need to provide these two options now and also you need to make sure this is a required required option so user must provide this option then only you can you should be able to trigger the pipeline so yeah that would be the input parameter you need to specify once you specify this input parameter now you need to specify uh you need to have a check condition when the supply should execute and when this uh this uh discharge should execute so let's go back and add a condition here if condition if if in case it's a in a double quote GitHub dot events event dot inputs Dot terraform action that is the parameter you have configured right so there are form action so github.even dot inputs dot terraform action equal to apply when it is equal to apply then only I want to execute apply command apply section similarly you need to add this section for the disk drive and I want to execute this try only when user select destroy destroy option soil okay so now you have it at you have added a condition so terraform apply will be executed when user select the apply option and here destroy will be executed only when user select the destroy option and also now you can as you all you are already uh capturing the input from user here you no need to Hotpot this so you can modify uh this option as input terraform action so copy this option here and here you can you can specify the input whatever the input user provide you can use that input here so GitHub action GitHub event input terraform action so you can use this one here also you can replace okay so I think we have con configured enough parameters so let's go ahead and commit the code before do that let me uh do one more thing let me show you if in case I I push the code into a different branch how exactly the pipeline will execute let's show let me show you that as well so git status you can see let me copy this code let me check out a new branch called uh test so here I'm checking out into a new branch and git status git add git commit commit into new repo and git push origin test so now I have committed the code into a test Branch now let's go back to GitHub and let's see if the pipeline has been triggered or not so you can see here the pipeline has it been triggered that means when you push the code into a branch which is not specified in the GitHub action pipeline so it will not it will not trigger the pipeline now let's go back to our main branch so let me check out mail so let me paste the content I have copied the content already now you can see this is here so git add git commit like I will specify uh destroy input added and git push origin main now I'm pushing into main branch and you should be able to see the pipeline trigger so here it is triggered and here as now um if you remember we have added this input as a required parameter and we haven't specified any default action so now what do you think will happen right so here let's see ideally this one should not work but let us see what will happen refreshing the state and here you can see as the option is not specified we haven't specified any option and you can see here both the terraform apply and terraform destroy or skipped that means our conditions did not met we haven't provided any input now let's go back here let me go back one one more step and now you can see this workflow has a workflow dispatch event trigger because we have configured this workflow dispatch event that's why now it is populating this option earlier I think I should I haven't shown you this option is not there now it is showing this option and with the input parameters now you can see use the workflow from main branch and run workflow I think no not this one so it should ask you for input parameter basically let me see why it is not asking for input okay my bad I think it's a inputs not input so inputs add commit push so it has is triggered the new one let me yeah now you can see this option is coming so let me refresh the page again I will wait till till this this stage is completed because because we are handling with the terraform so you should be very careful with the terraform State file so once the data form is already executing and if you try to execute another pipeline it may try it may there is a possibility that it will correct so it is better give a minute so now it is completed let me click click here and you can see select terraform action now this is asking as an input now go back here so again there is a mistake I think uh what is that okay here again options so s is missing so let me Commit This Again push so it it actually did not start anything let me cancel workflow something went wrong let me go back here and let me run the pipeline so here now you can see uh apply and Destroy two options are coming let me select the apply first and run for our flow so now we triggered the pipeline and the pipeline should be so what do you think it should execute because we have selected the apply option so it should execute only apply section it should not execute destroy section so let us check whether it is working as expected okay so init is completed it is executing plan let us give a minute so yeah now you can see because we have choose and apply Action Now apply is executing so let's wait for a couple of seconds and apply is completed now you can see the display option is skipped so this Mark is Skip Skip to mark now let us go back again one more time and now this time run the workflow with the display option so destroy run workflow so this time apply should not be pick up but it should destroy the instance so pipeline has been started you should in a minute it should come to destroy as well so terraform successfully initialized and it is executing plan and now you can see uh terraform apply got skipped it did not execute but terraform destroy has started executing now you can see the command is destroyed so it is trying to destroy the instance let's go back to AWS console and let's try to refresh and you can see it is shutting down the instance that means the pipeline executed successfully and Destroy option has been triggered and it is trying to destroy the ec2 instance as well I think I hope um that's all for this session let me go back to the console or a visual studio and I will quickly walk rewind what we have done so far so first of all we have created a empty repository inside empty repository we just place an AC to instant creation terraform code it's a simple code and after that we have created our first GitHub actions workflow in GitHub action workflow you have specified on push on push which branch you want to run the pipeline and also you have specified which job you need to execute and inside the job you have configured AWS environment credentials sorry credentials in the environment section and also you have specified on which Runner you want to execute and also you have specified the checkout option and you have specified data from any terraform plan apply and destroy and also you have configured a input option for a user to select whether he want to apply the configuration or destroy the configuration and also you have configured uh backend as S3 and you have configured uh we have successfully configured instance type as a environment variable sorry uh input variable and how you can pass the variable uh in the uh steps uh pipeline steps even in fact you can also update you can also add this M2 T2 medium the instance type also as a parameter input parameter here so you can add one more section called instance type so instance I underscore type and here now you can specify type and you can specify a description and also you can specify options let's say uh user want to select multiple options like a data medium or T3 small so you can specify this action here and once you specify this action as you know already so you need to use this instance type parameter here so what you need to do you need to write here GitHub dot event dot inputs dot instance type so once you do this you you will need to select this instance type also in an option and you can perform apply or destroy so these are pretty much configurable options I'm not going to do uh instance type right now but this is just to give you a overview over idea how you can configure input parameters I hope you you all enjoy this session I I believe you have learned uh something new in this particular demo and thank you so much and we'll meet you again in the next session thank you
Info
Channel: DevopsDen
Views: 3,240
Rating: undefined out of 5
Keywords:
Id: 3Gm2Tuh4vpc
Channel Id: undefined
Length: 61min 52sec (3712 seconds)
Published: Sat Apr 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.