GitHub Copilot Tutorial | How useful is it for Cloud and DevOps?
114.39k views6541 WordsCopy TextShare
TechWorld with Nana
Does GitHub Copilot make you faster as a Cloud or DevOps engineer? The what, why and how of GitHub C...
Video Transcript:
let's talk about GitHub co-pilot is it really this super revolutionary tool that makes you work as a software engineer or Cloud devops engineer and so on way easier way faster or in some cases even replace the Need For Engineers is it really as good and what are some of the common use cases of using GitHub copilot and in this case we're going to be specifically focused on the work of cloud and devops engineers and even compare that to GitHub copilot's value and usage for software development so let's get started first of all what is GitHub copilot well in very simple words it's an AI assistant for coding that means giup copilot is used in a code editor or in an integrated development environment like Visual Studio code intellig so any kind of IDs where you integrate GitHub copilot and it helps you write the code within the editor so compared to cat GPT for example we have cgp in a browser so you have to switch from your code Editor to the browser to type in your request and it gives you some code with explanations in this case you have that AI tool integrated within the editor so you don't have to leave your Editor to get the replies and get the code suggestions and so on and the way GitHub copilot works is that it has way more context than ched GPT for example that means that it actually takes your code your existing code whatever you have written and whatever you're writing as a context to answer your questions and also very important to know that the context that gets uploaded and sent to GitHub co-pilot is not your entire workspace because you may have hundreds of code files that may not be relevant for whatever you're asking so usually whatever you have open in your editor currently so you may have 5 10 20 files that you're working on currently in the editor plus you have the file that is currently open where you're working inside so it takes all of that as a context assuming these files and the code inside are relevant for your current question because that's exactly what you're working on right now and this edit context is actually a huge Advantage because you don't need to provide the entire context to co-pilot you can just say give me whatever for this high Ed code or give me whatever for my entire workspace or code base so how does GitHub copilot work behind the scenes GitHub copilot actually uses an open AI large language model behind the scenes and it's been trained with billions of lines of code and text like code explanations that are publicly available so based on that large amount of data it can give you suggestions based on the best practices and so on in addition to just replying to your request when you explicitly ask something from GitHub copilot it actually gives you code suggestions itself so when you start typing out your code it actually gives you kind of auto complete Suggestions by trying to predict what you're going to write and again as I said because it has all the context if you already have an project that you are working on so you're not just starting from the blank page and the first file it actually takes whatever you have open as a context and based on that it will try to guess okay this is what you're trying to do next and it will give you code suggestions based on that it will even give you comment suggestions with the entire block of code for example if you're creating is2 instances and you have that configuration inside it may suggest you to create Security Group with firewall configuration for those E2 instances for example if it detects that you don't have Security Group configuration yet so it tries to guiz and give you suggestions what you should write in the code now and this is a very important part there is one small problem with GitHub copilot which we're going to see later and that is that it's not up toate for coding or programming specifically GitHub copilot actually is way more helpful than for writing infrastructure as code however as I said there are still some use cases and some advantages of using GitHub copilot even for infrastructure as code scenarios and we're going to see both sides in the demo so we're going to see the usage how it helps and how it may help in writing code but also limitations of GitHub copilot so we're going to see kind of both sides in the demo so let's go directly to the demo and let's see how it all works and in the demo we're going to be using GitHub co-pilot obviously with Visual Studio code and for the infrastructure as code tool we're going to be using pumi with the Python language now if you're new to pumi or python I actually have very detailed tutorial on both of them separately so you can watch them first and then come back to this one so as a first step of course let's get our prerequisites right and let's do all the setup that we need in order to have everything installed everything up and running and ready for our demo projects and this is everything that we need for our demos first of all we need our AWS account I'm logged in as an admin user and I'm going to be creating infrastructure in in this specific region you can choose whatever region you want so we need AWS Cloud platform because we're going to be creating infrastructure here with our infrastructure as code project we also need pumi installed locally because we're going to be executing a pumi project with pomi commands and you can very easily install or download it by following their installation guide for whatever operating system you have I have mechos and I'm actually going to use the package manager to install it you can also downlo download a binary and just install it with a graphical user interface so whatever operating system you have whatever installation option you want you can use it to have pumi installed locally so I don't have pumi yet so I'm going to install it using Brew package manager and installation was successful let's validate it by executing a pumi command so let's do pumi version and this is the version that I have just installed as I mentioned with palumi you can actually choose whatever programming language you want to work with and in our case we're going to go with python and again if you're new to either plumi or python I have separate extensive videos on both topics so you can go ahead and watch those separately if you want to learn them properly first so since we're going to be using python for palumi we also need to have python installed locally and actually have python version 3 already available if you don't have it again just a simple install Python 3 search will give you the download guide on the official documentation for whatever operating system you have so you can download it as a binary and just install it easily like this we're also going to need Visual Studio code that's where we're going to be writing our code and this is where we're going to integrate GitHub co-pilot to actually help us in writing the code in our editor just search search for it here's a download guide and again choose a download for your operating system and install it locally and the final thing that we're going to need is of course the GitHub co-pilot itself so the way we're going to get this is first we're going to need a GitHub account so if you don't have one just sign up in GitHub it's free it's easy so just register and I'm actually going to look for GitHub co-pilot here so we're going to get to the link directly there you go and if I click on get started with co-pilot you see different payment options so of course as an individual engineer you don't need a business or Enterprise license so we can go for the individual one and this one actually has a 30-day free trial which is great because it gives you enough time to actually play around with the tool and decide if it's actually worth the investment if it really makes you productive in your work or not so that means you can follow this tutorial along completely free using a free trial so I'm going to choose this one as well and now we're going to sign into to our GitHub account I'm going to authenticate and there you go and here's an interesting note that GitHub copilot is actually completely free so you don't need to even pay the individual license if you are an open source maintainer teacher a student which they check on a monthly basis so if you are one of those you can actually use GitHub pilot for free so I'm going to click on get access to GitHub pilot and we're going to select a couple of options here regarding the policies so the first configuration option lets you control whether GitHub co-pilot can generate code suggestions that match code found in public repositories on GitHub so private repositories on GitHub or any other repositories outside of GitHub will not be considered here and if you allow this when copilot gives that code snippet that matches some code on the public repository on GitHub it will also provide you with details about the matching code like any license identified for the repository as well as the source for the code and so on in our case actually let's allow this one and the second option allows you to decide whether the prompts that you give GitHub copilot to generate code for you plus the context that GitHub copilot takes to reply to your question questions whether you want all of that to be collected and retained by GitHub and processed further as well as shared with Microsoft because as I mentioned the context is basically what gets sent up to GitHub server so that co-pilot can use that reach context to give you a better more precise repli and with this option you can decide whether GitHub can retain that context that was uploaded and process it to improve the co-pilot even more now this can be an important security consideration especially if you're developing an application which is private for your organization or maybe you have developers that are hardcoding some secret information or sensitive information in the code or you are basically testing out and adding the API keys or whatever sensitive information directly into the code maybe for local testing not even for pushing it to the remote repository so that may be send as part of the code Snippets So based on that you can decide whether you want to reject and make sure GitHub doesn't keep any of this data and doesn't process it or you can decide to allow it in my case I'm going to allow it because this is just a test project and all the codes inside can be sent and retained by GitHub save and complete setup and we have this confetti flowing down because we have successfully activated GitHub co-pilot how however we need to use GitHub co-pilot in our code editor because the co-pilot is going to help us write code right so we need it directly integrated in our IDE in this case Visual Studio code and we have these options that we can choose from so I'm going to choose this one and it jumps into the stepbystep guide of how to integrate it in Visual Studio code so all we need from this documentation is this section here so we have all the prerequisites everything is activated and in place so right now all it's left to do is to go to visual studio code find the GitHub compilot extension install it and we're going to be good to go so basically these six steps is all we need to activate GitHub copilot in Visual Studio code so I'm going to go to the extensions and look for GitHub co-pilot and there you go this is the official one from GitHub itself so let's install it we need to sign in GitHub co-pilot as is also mentioned in the documentation so let's do that we're going to authorize Visual Studio code to access our co-pilot and that's it our GitHub co-pilot extension is installed so now when we start writing our infrastructure as code for provisioning ad infrastructure we're going to have cop directly integrated into our editor so we can use it for our demos awesome so everything is set up for us to start the demo so as a first step let's create a new pumi project using pumi CLI so to initialize a new project in the co-pilot pumi folder we're going to execute pumi new and we're going to give pumi kind of a template or what we are trying to create here as we learned pumi supports multi multiple programming languages in our case we want to create a python project which is going to build infrastructure on AWS so the template name will be AWS Python and you can actually look up all the pumi templates when you're running pomi new Command right here in the GitHub repository so the one we're using is AWS python so let's go ahead and execute with this and this is actually a very convenient way to provision everything that pomi needs for the configuration instead of writing everything from scratch so the first thing we're going to do is an exess token from pumi so of course we need to authenticate that this is our palumi account that we're using and palumi also has a really nice UI where you can track all your projects and infrastructure configurations that you create that looks like this with the dashboard and project and STS and so on and in the personal exess tokens we're going to create a new token and we're going to call these also awf Python and we just provided here so now we're going to configure the project details let's call the project Co pilot palumi take the default project description stack name we can leave it at Dev and AWS region to deploy into I'm going to choose EU West 2 which is London region you can choose whatever is closest to you and as you see pumi is actually doing all the main configuration behind the scenes and let's see what we get as a result awesome so the project was initialized and because this is a python project it actually created a python virtual environment where all our libraries will be downloaded for the python project it also created this boilerplate python code for palumi it gave us the configuration for the dev stack or development environment basically and some other stuff plus because we specified that we want to provision infrastructure on the AWS Cloud platform it actually automatically included the plumi AWS Library which we're going to use to create different resources on AWS which we're using right here so all this boilerplate code basically that we need for the project was provisioned automatically which is pretty convenient and as I said the requirements. txt which is like a dependencies file for a python project so all of that was downloaded automatically cool so now we want to finally see GitHub copilot in action right so how do we use GitHub copilot well first of all if we create a new file in our project and let's call it test. python doesn't matter you see that immediately the first line is a prompt to ask GitHub compiler to help you write code and using this shortcut which is command shift I on Mech or whatever shortcut you get displayed in another operating system you can use this chat to ask GitHub copilot for something or you can use it inline using command I like this or if you don't like shortcuts you can just open GitHub copilot chat right here so you always have this on the left side to fix your code issues or explain the code that was written Maybe by other developer and so on so let's actually use GitHub copilot for some really simple examples let's remove this and this is a boiler plate that P generated which is just creating an est3 bucket so let's ask GitHub co-pilot to give us code that generates 10 buckets in a loop for example create 10 as3 buckets with unique names let's see what we get and it actually generated this code in a for Loop which is a python syntax to generate those buckets in in a loop and each one will get some unique name and let's say we want to replace this whole code block with this one you can just click here and here we have a new code block now let's say when you're coding and writing your infrastructure configuration file maybe you don't always want to go to the chat generate something copy that code paste it in a more convenient way to work in your code editor while you're writing code is actually to use inline GitHub copilot suggestions right and the most convenient option is actually code suggestions so if I clean all this up I can write a comment here just a simple comment that says create 10 S3 buckets with unique names and if I hit enter you see that it auto suggests the code and again which is pretty much same as here or similar and now let's say we want to keep writing configuration for other components of infrastructure let's say create five ec2 instances for my web application and we have the whole suggestion for that configuration sometimes you get multiple suggestions in this case both of them look pretty similar just a small difference and whichever one you like you click on accept right here or you can just hit on the tab and it fills in the entire configuration and let's say right here we want to use another instance type instead of T2 micro use T2 small instance type and gives a new one here we have an Ami which is usually an ID that we need to go to AWS and look it up to define a value here so here again let's ask get up co-pilot to actually use an auntu operating system instead of the Amazon Linux Ami and we actually have two suggestions and if I switch the second one it says use Ubuntu 18.
04 and if I delete that it will give me a suggestion for the Ubuntu Ami which would be very convenient because I don't have to look up Ami in the AWS console so as you see we can control the flow and what co-pilot suggests us using the comments let's do one more thing which is right here as you see it uses S3 from palumi AWS library but it didn't import ec2 so let's actually ask this to import S3 and ec2 modules from pulumi aw's package and if I delete that we get both and of course you can remove the comments that are kind of cluttering the code and only use them to get suggestions from GitHub copilot so as you see with the simple demonstration the main benefit or the use case of GitHub copilot just like any other AI code assistant is to let you program with a human language or in this case English language and let GTH up copilot give you the actual code for that logic now this may look really cool and it is in most cases however there is a huge drawback of GitHub copilot used in practice for writing infrastructure as code projects and that is that it's outdated so basically the latest state of giup co-pilot code for most of the languages most of the information that it knows about is from September 2021 and that is a pretty old date considering the speed of technological developments every infrastructure as code language or tool develops really fast so you have new libraries improvements every couple of month that are being released so 2021 is actually a very old State and we can even ask co-pilot itself what the last date of its information is as you see this is September 2021 and the information since then has obvious L changed and because of that we're getting this older version of auntu as well as the older Ami image from Amazon right so if I check to versions obviously we have new versions here Ubuntu has released two major versions since then so this Ami will be outdated and of course in our infrastructure we don't want to be provisioning servers with outdated operating system versions and images that will be a very bad practice and I also want to mention one very important distinction between using GitHub copilot for programming as a software developer and using GitHub co-pilot for infrastructure provisioning because programming languages like python Java JavaScript whatever these programming languages are way more established and the functions and libraries and so on they don't change as much and as frequently compared to a relatively new infrastructure as code tools like palumi terraform obviously improvements are made way faster and a lot more is going on in that area and because of that it's way more challenging to use GitHub copilot for infrastructure as code projects because the chances of functions or attributes or libraries being outdated is way higher here so that is a very important distinction when analyzing GitHub co-pilot's value in each category and that means if you are devops engineer or Cloud engineer that is automating stuff you want to use GitHub cor pilot it definitely helps with spitting up the code however overall you will probably have to double check and reference the upto-date documentation for each configuration option so that is a huge disadvantage of GitHub copilot right now which is that it's outdated so now considering this limitation of GitHub co-pilot let's still see in what other use cases we can use GitHub compil to actually get some value out of it so I'm going to clean this up and let's do one more thing here so what I'm going to do is I'm going to create a temporary file here which is going to include terraform code which creates a VPC and eks cluster and let's say in our scenario we want to migrate a project from terraform to pumi we already have a project that Provisions configures infrastructure that is written in terraform but we want to migrate it to pumi so we can ask GitHub copilot to actually help us do the translation from terraform to pumi code so this is a simple script that basically creates a VPC for the cluster by defining the private subnet public subnet and so on and then it creates an eks module in that VPC with some simple configuration such as one node group with this instance type and so on so let's ask our GitHub co-pilot first of all to explain whatever is written right here so let's say we don't know terraform as well we are trying to migrate a project that somebody else wrote in the team to palomi so we want to ask GitHub copilot to explain that code first so we know exactly what it's doing and GitHub pilot we can use de and one of the commands basically that references the workspace that means it takes our entire workspace as a context for giving us the answer so we're going to ask it to explain the highlighted terraform code in detail and it gave us a breakdown of what we're provisioning we need to set this varable value somewhere they were retrieving the as's from the current region that in the eks configuration we're enabling public access to the cluster which is right here as well as disabling of cluster and node security groups so it will be using the default ones and again you can ask it to go into details and explain each and every attribute so you can understand the code which I believe is one of the really good use cases of co-pilot because very often you're going to be working with a code that looks very complex that has become complex over time but because you are seeing it for the first time and you didn't write it yourself it may be a huge help if you let G UPC pilot basically analyze the whole thing and tell you what's happening there so now we're going to ask co-pilot to translate this entire terraform code into pumi python code with the exact same configuration for creating VPC and eks cluster and let's see what we get and another very important note is that if you are following this demo and you ask co-pilot the same thing you will most probably get something completely different you will even get different answers every time you ask the same question so it basically comes up with a new answer on each question means you can't expect that kind of consistency which has both advantages and disadvantages obviously but let's see our pumi code and then we also have some explanation here that says it does the same thing as terraform code we provided we have the same instance type the capacities of the node somehow it took the cus version 1. 21 because that's the last version that co-pilot actually knows of it doesn't know that kis cluster version is I believe at this point at 1. 29 already so now we can actually move this code into the python file and also notice the follow-up question that it suggests itself so we notice that we're not creating security groups for VPC and E cluster and it suggests us to addit configuration so if I click on this it will actually give us the configuration for that as well that we can and add additionally in our configuration so let's see what it gave us it's creating a new Security Group in the VPC which is always a good practice to create new components because when you tear down your infrastructure using your infrastructure as code project you can cleanly delete everything basically and then it adds a rule to the security group it basically opens a incoming Port 80 and it references that Security Group in the cluster and it gives us uses another followup which is for configuring load balancers in the E cluster so basically it will give you more and more suggestions to try to be helpful but as I said you have to be aware that every configuration that you get here generated by co-pilot you will have to validate because as I said the huge drawback is that it's outdated and we obviously don't want to use outdated configuration plus when we execute it it may not actually work work because some attributes some libraries will be outdated Etc and one obvious one is the kubernetes version that it's using right here kubernetes 1.