CI/CD from GitHub to AWS Lambda (i.e., automatically update lambda function code) with CodeBuild

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up everyone welcome back to another episode of aws tutorial and today i'll show you how to automatically update your lambda function code with libraries using github and code builds and in this video i'm going to use python as an example but the approach works for all other languages as well so without further ado let's get to it alright so right now i'm on the home page of the aws console so step one is to create a lambda function that we want to update the function code with so let's go to lambda click on it create function and then give it a name i would just call it github to land the demo we're going to choose python 3.8 and then i just leave everything as default create function all right so it's done and now let's move on to step number two which is to create a github repo to host our project so just go to github click on new i will just call it github to land the demo keep it as public and read me at the git ignore and then we're going to choose python create alright so it's done we're going to copy this url open the terminal and navigate to the directory where you want to save the project and then i'll do git clone and then paste the url hit enter and then i'll cd into the project and then i do code dot and that just opens up vs code you can also open the folder manually within vs code if you want to all right so what we're going to do here is we're going to first create a lambda function script if we go back there scroll down we can see that we specify the file name to be lambda function and then the function within the file is called empty handler so let's do that create a file call enter function the pi and then we're going to define a function called lambda handler which is going to take in an event and the context and in our demo function we're going to use a library called pandas so we're going to import pandas spd and then we will define some dummy data and then we define a data frame and we just print it out and then print out done times one and since we use an external library called pandas we have to install it first and then include that into our package so we're gonna create a file called requirements txt and then specify pandas and we're gonna use version 1.2.1 and the next we're going to create a buildback file for code build to use and i already have this written down before so i'm just going to copy and paste it here but i include a link down below so you can have it as well so one thing we need to change here is the lambda function name it should be whatever name you gave your lambda function when you created it so i'm just going to go back here and then copy this so what this is going to tell cobia to do is it divides the whole process into three phases the first one is installed and then we're going to use the python runtime to do it what we're going to do here is we print out what it's going to do and then we do it pip installed whatever library that we specify in the requirement file and then we're going to save all the libraries into the lib folder and then the next phase is built what we do is we cd into the lib directory and then we zip everything and save it into the root directory so one level up and then we're gonna cd out of the lib library into the root directory and then we add the lambda function script into the zip folder that's a typo here and lastly in the post build phase we're going to use the aws command to update the function code with whatever we have inside our deployment package.zip and this is how the file system works we have to add the file b in front of it and that's all we need and right now let's commit this and push it to github for later use and now we're ready to create a code build to connect everything so just type in code builds open that into a new tab create build project i give it a project name i just call it github to lander and then scroll down source i'm going to choose github and if this is the first time you connect your aws account to github you have to hit this button to authenticate confirm i choose repo from my account i believe we call it github is that one and then it's domain because it's in the main branch and then scroll down web hook and then event type i do push because i want it to build every time i push a new commit to it and then scroll down operating system i'll choose ubuntu runtime standard image i'll choose 5.0 and then i just let it create a new service row for me use a buildback file i don't have to enable cloudwatch and then create your project alright so it's done and that's one more thing we need to do before we can build the project is that we need to give code build the permission to update the lambda function and we're going to do that in the item row and item row can be found under build details it's right here so right click on it open it in a new tab click on this policy add the policy json we're going to add a statement to it i already have this written down before so i'm just going to copy and paste it here but i'll include that in the link down below so you can have it as well but one thing you need to change it here is you need to change this change this on to be the on of your lambda function so i'm just going to go back here copy this on go back to the row paste it here hit reveal and make sure you hit save all right so it's done and now we're ready to push a new commit to github and let it build a new version of our lambda and right now let's go back to our lander function and see what it looks like right now first so it just looks like this so if we go back to our code the push it should trigger our code build so let's go back to the aws console go to code build build projects it's in progress so click on it we can look at the face details all right so it's done everything's successful and right now if we go back to lambda refresh the function code is updated and because we have an external library the package is too big we cannot view that in the editor but if we hit test we don't need this it should print out what we told it to print out in the code is successful and there you go this is our data frame and this is the version that we specify it to be and that is it everyone i hope you have learned something if you like this video i hope you can give it a thumbs up and i'll see you in the next video
Info
Channel: Felix Yu
Views: 37,253
Rating: undefined out of 5
Keywords: aws, lambda, ci/cd, codebuild, deployment, automatically update, lambda function, lambda function code, python, python lambda, github action, github hook
Id: AmHZxULclLQ
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Fri Feb 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.