How to do Test Driven Development with AWS Lambda Function in Python

How to do Test Driven Development with AWS Lambda Function in Python


How to do Test Driven Development with AWS Lambda Function in Python

This episode shows how to write an AWS Lambda Function in Python following the TDD (Test Driven Development) approach. This leverages on boto3 python module to allow calls to AWS API and moto to mock the calls to AWS Services.

Github Link: https://github.com/dyordsabuzo/pablos

#Python #TDD #AWSLambdaFunction


Content

0.4 -> hello welcome to public spot i'm george today i'll  be doing something different i will show you how  
6.72 -> i write a lambda function using tdd approach  or test driven development let's start coding
21.84 -> so what i will try to achieve today is  to write an aws lambda function that will  
27.04 -> generate new access keys  for a user i have installed  
30.48 -> python pip and virtual and on my machine to do  my development work and the two primary python  
37.28 -> modules that i will be using include photo 3 which  allows adbris calls in the background and moto  
43.6 -> which is essentially a way for me to mock calls to  aws so i'll start by creating a space for my code  
52.16 -> and then inside the lambda directory i will set up  my python virtual environment and then call it dev
60.32 -> i'll activate my virtual environment  
64.24 -> now inside the lambda directory i will create  two requirements the text file one for the main  
69.76 -> modules and then another one for my modules that  i need for testing and then in my requirements  
76.24 -> the text i'll add boto3 and then on my  requirements test the text i'll add moto  
82.4 -> pi test and pi yamu i'll go ahead and install  my modules inside my virtual environment
93.6 -> now that all my python modules are installed  i'll start creating a directory inside  
97.84 -> lambda called app and then inside my app  directory i'll start creating my test file
112.88 -> i will start with the simplest test case so this  assertion actually failed because we have a space  
132.32 -> in there so let's fix that save that and then run  our test again now we've got a first successful  
140.16 -> test let's go ahead and move on to the next test  case so what if i allow the parameter access and  
146.24 -> secret values to be provided by the caller  so i would create a new test case for this
162.56 -> save my changes and run my tests again  and then we have failed tests let's just  
168.16 -> have a look it says it takes three positional  arguments but five were given so let's fix that
181.68 -> save changes and then run our test again  so again all my test cases are successful  
188.48 -> so now i need to set a test case that will  test the actual creation of the access key
217.28 -> save our changes on our test.py  and then run our test again  
223.36 -> so mock iam is not defined so let's fix that
228.56 -> save our changes run our test again
232.72 -> we have another error boto3  is not defined let's fix that  
240.96 -> so as expected my test case failed it's looking  for the implementation of that new method and  
246.72 -> if we look at our test case it's actually  expecting values to be populated in my  
251.36 -> ssm so what i need to do is implement the part  where it populates my parameter store inside ssm
279.2 -> now that we've fixed the  implementation let's save our changes  
282.64 -> and then run our test again so far all my  test cases have passed so now i need to  
288.64 -> make sure that my parameter values are not  empty let me implement a test case for it
317.2 -> save my changes and run my tests again as expected  our test case failed because our assertions are  
324.48 -> not satisfied so let's go ahead and fix that test  case save our changes then run our tests again  
343.04 -> so now i need to create a new test case for a user  that doesn't exist before i start implementing the  
348.8 -> test case for handling users it doesn't exist i  need to import the module that i need to handle  
354.56 -> the exceptions and that module is both core so i  need to go all the way to the top of my test.py
364 -> save that and then i can  start creating my test case
374.48 -> save my changes and run my test again
378.96 -> my test case ran successfully which means we  do not have to implement anything to handle  
383.04 -> the exception what i've written so far  is capable of handling situations where  
387.84 -> user that does not exist so we are  good now on to the next test case  
392.24 -> aws imposes limit of two axis kits per  im so i will create a test case for this
424.88 -> save my changes and run our test
430.32 -> as expected the test case failed and the  assertions are not satisfied the way to fix this  
436 -> is that i have to implement a way of deleting  old access keys before creating a new one
462 -> save our changes open terminal clear this and then  run our test again my test case failed because of  
470.48 -> an incorrect assertion that i used at line 89  instead of a third equal this should be assert  
476.56 -> true save that and then run my test again and now  all my six test cases are successful and my final  
484.88 -> set of test cases are for the handler method  of my lambda function so let me write that up
506.56 -> save my tested py and then run my test again  
510.96 -> as expected the test case failed  so let's go ahead and fix it
532.72 -> all seven test cases that i have set up are run  successfully and so the last bit that i want to  
539.52 -> set up as a test case is exceptions when  calling handler so let me add that test case
555.36 -> run our test so all eight test cases that i've  written have ran and completed successfully so  
562.56 -> at this point i have completed writing my  lambda function code that will generate  
567.2 -> new access keys for users and if you've  been working with aws you'd know that one  
573.04 -> of the items in aws well architected framework is  ensuring that all access keys for iam users are  
580.64 -> rotated regularly so this lambda function  will come in very handy anyways let's edit  
587.2 -> in there and on the next episode i will  show you how i create a docker image and  
592.8 -> set up this lambda function infrastructure that  would use this docker image so that's it for now  
599.44 -> let me know what you think on the comments  below send me some likes or share this video  
603.44 -> if you find it useful and subscribe  to this channel until next time see ya

Source: https://www.youtube.com/watch?v=KBr7yKXI-G4