1 00:00:11,509 --> 00:00:16,410 Welcome back to BackSpace Academy. In this lab we're going to be creating a 2 00:00:16,410 --> 00:00:21,060 fictitious ordering and fulfillment process and we're going to do that by 3 00:00:21,060 --> 00:00:25,109 creating a number of different Lambda functions that are going to receive 4 00:00:25,109 --> 00:00:28,859 orders, are going to check for stock, they're going to bill a customer and 5 00:00:28,859 --> 00:00:33,420 all of these sorts of individual processes and we're going to coordinate 6 00:00:33,420 --> 00:00:39,030 all of the interaction between these many Lambda functions as a state machine 7 00:00:39,030 --> 00:00:45,809 and we're going to achieve that using AWS step functions. So without any 8 00:00:45,809 --> 00:00:52,020 further ado let's get into it. So starting off here in the Lambda console 9 00:00:52,020 --> 00:00:56,460 we're going to create a function and we're going to call this first function 10 00:00:56,460 --> 00:01:06,000 receive order so authoring from scratch and this is going to be for Python so up 11 00:01:06,000 --> 00:01:12,210 the top here you'll select Python and our execution role we'll use that same 12 00:01:12,210 --> 00:01:16,710 execution role that we used before lambda basic execution and create that 13 00:01:16,710 --> 00:01:23,780 function okay so that's created successfully now what I'm going to do is 14 00:01:23,780 --> 00:01:31,950 copy from the lab notes our code and paste that in here okay so what are we 15 00:01:31,950 --> 00:01:35,909 doing here so we're receiving our event is going to have a field called 16 00:01:35,909 --> 00:01:40,680 order so when we pass information to our request to this Lambda function it 17 00:01:40,680 --> 00:01:45,540 will be looking for order and that's all that we need for that so if we create a 18 00:01:45,540 --> 00:01:50,579 test for that that should be fine so first off we need to save it and create 19 00:01:50,579 --> 00:01:56,909 our test and again I'm just going to copy and paste from the lab notes and 20 00:01:56,909 --> 00:02:00,990 put that test in there just call it tests that will be fine 21 00:02:00,990 --> 00:02:11,310 and create that and let's do that test so that worked out okay order 001 has 22 00:02:11,310 --> 00:02:14,160 been received so that that function is working 23 00:02:14,160 --> 00:02:18,630 perfectly so we just go back into functions down create our next one so 24 00:02:18,630 --> 00:02:23,100 this next one is going to be called check stock 25 00:02:23,100 --> 00:02:31,830 so again copying from the lab notes and selecting Python and our execution role 26 00:02:31,830 --> 00:02:44,160 so use our existing role here and create that function and again I'm just going 27 00:02:44,160 --> 00:02:54,690 to copy from the lab notes and paste that in there ok so what we're doing 28 00:02:54,690 --> 00:02:58,770 here is a little bit more complicated. So we're again we're getting there order ID 29 00:02:58,770 --> 00:03:04,200 which is going to be the order inside that object that's sent, and then we're 30 00:03:04,200 --> 00:03:09,180 going to generate a random number which is going to give us either a 0 or a 1, 31 00:03:09,180 --> 00:03:13,410 the reason we're doing this is it's a fictitious process so we need to put 32 00:03:13,410 --> 00:03:16,320 some randomization in there otherwise it will just keep going in the same 33 00:03:16,320 --> 00:03:21,180 direction and so we're going to every now and again it might have stock status 34 00:03:21,180 --> 00:03:26,010 0 or might have stock status one. If its stock status 0 then the products are out 35 00:03:26,010 --> 00:03:30,240 of stock. If its stock status 1 then the products are in stock, and so we're just 36 00:03:30,240 --> 00:03:33,300 going to create this response to send back which is going to have the order ID 37 00:03:33,300 --> 00:03:38,730 the status which will be either 0 or 1 depending on whether the product is in 38 00:03:38,730 --> 00:03:42,360 stock or out of stock and then the message that's going to be sent back and 39 00:03:42,360 --> 00:03:46,200 so these step functions will receive that and we'll make a decision on what 40 00:03:46,200 --> 00:03:54,750 to do after receiving that information so we just save that and again we'll do 41 00:03:54,750 --> 00:04:00,600 a test on that and again I'm just going to copy and paste from the lab notes 42 00:04:00,600 --> 00:04:08,030 again and we'll just give this one 002 this time doesn't matter and create 43 00:04:08,030 --> 00:04:11,030 oops 44 00:04:11,130 --> 00:04:19,570 and create that and we'll test and that was fine, so products are in stock and in 45 00:04:19,570 --> 00:04:22,480 status 1 if we keep running this we'll do another test and might come up with 46 00:04:22,480 --> 00:04:26,620 status 0 so let's do it again there we go so set as 0 products we're 47 00:04:26,620 --> 00:04:30,490 at a stop we'll run it again might give us a different result products are in 48 00:04:30,490 --> 00:04:42,370 stock products are in stock products are in stock in stock and add a stock so 49 00:04:42,370 --> 00:04:46,510 just generating a random number this but why the 0 or once that's working fine 50 00:04:46,510 --> 00:05:03,870 we'll go back and do our next one which is going to be order stock and again - 51 00:05:03,930 --> 00:05:18,610 and our execution wrong create function again copying from the lab notes ok and 52 00:05:18,610 --> 00:05:23,530 save that so this one is just simply sending back the order ID and saying hot 53 00:05:23,530 --> 00:05:29,140 has stock ordered so we do a very simple test for that and we're just put in a 54 00:05:29,140 --> 00:05:37,900 number there whatever it can be doesn't met him and give it a name and we'll 55 00:05:37,900 --> 00:05:40,410 test that 56 00:05:41,559 --> 00:05:47,979 so that worked fine order 001 has stock ordered so we'll do a lot 57 00:05:47,979 --> 00:05:55,689 our next one which will be deliver order create function and deliver order again 58 00:05:55,689 --> 00:06:12,759 copied from the lab notes - land a basic execution roll create that function okay 59 00:06:12,759 --> 00:06:23,349 so again copying from the lab notes okay so this one again is just sending back 60 00:06:23,349 --> 00:06:27,309 the order ID it's saying is being delivered so we should be able to test 61 00:06:27,309 --> 00:06:37,569 that one easily just save that create another test and an order number will 62 00:06:37,569 --> 00:06:42,669 just be double zero one again or one two that'll that'll be fine and create that 63 00:06:42,669 --> 00:06:51,839 and test that okay order double zero one two is being delivered which is perfect 64 00:06:51,839 --> 00:06:59,279 okay the last one will be bill a customer so create function 65 00:07:03,759 --> 00:07:19,339 and again bill customer - and our role and create that function and again copy 66 00:07:19,339 --> 00:07:29,479 the code from the lab notes okay so this again is just simply sending back the 67 00:07:29,479 --> 00:07:43,279 order ID is being built to the customer will save that do a quick test on it and 68 00:07:43,279 --> 00:07:51,559 create that on that test and there we go order double zero one is being built to 69 00:07:51,559 --> 00:07:56,479 customer so all of those functions are working exactly as they should and we 70 00:07:56,479 --> 00:08:01,249 should have five functions here we're going to receive the order we're going 71 00:08:01,249 --> 00:08:06,289 to check for stock if there's no stock we're going to order stock if there is 72 00:08:06,289 --> 00:08:11,360 stock we're going to deliver the order and then build a customer so we need to 73 00:08:11,360 --> 00:08:15,800 create a state machine that's going to coordinate all of these Lambda functions 74 00:08:15,800 --> 00:08:22,189 so let's go into step functions and create that state, or no, before we create 75 00:08:22,189 --> 00:08:27,289 that state machine, we need to create an IAM role that is going to allow our step 76 00:08:27,289 --> 00:08:30,889 function to invoke these Lambda functions so we need to go and create a 77 00:08:30,889 --> 00:08:37,569 role so go to services and IAM 78 00:08:42,789 --> 00:08:49,700 so we go to roles and let's create this role it's going to be an AWS services 79 00:08:49,700 --> 00:09:00,860 step functions we'll select that and next permissions okay so it's already 80 00:09:00,860 --> 00:09:04,910 selected the AWS lamda role for us so it's been a bit smart there for us 81 00:09:04,910 --> 00:09:10,430 that's fine we're going to next tags and next review we'll give it a name we're 82 00:09:10,430 --> 00:09:15,170 going to call this one step functions basic execution you can see here it's 83 00:09:15,170 --> 00:09:23,060 got our Lambdaa role policy there and create that role there we go so that's 84 00:09:23,060 --> 00:09:27,890 been created and we can use that for our step function now so we create our step 85 00:09:27,890 --> 00:09:33,410 function or create a state machine we're going to author with code snippets it's 86 00:09:33,410 --> 00:09:37,610 going to be a standard one and we're going to put our own code in there so 87 00:09:37,610 --> 00:09:47,000 what we need to do is go to the Backspace Academy GitHub and so here we have 88 00:09:47,000 --> 00:09:52,760 our step AWS step functions lambda Python so this will have all of those 89 00:09:52,760 --> 00:09:57,110 Python code that we cut and paste out of the lab notes and the one that we need 90 00:09:57,110 --> 00:10:02,870 here is step function JSON so there we can see it's quite big I'm just going to 91 00:10:02,870 --> 00:10:14,000 click on raw and copy all that and paste that in into here so delete all of that 92 00:10:14,000 --> 00:10:22,970 and paste that in so that is our JSON that defines what our state machine is 93 00:10:22,970 --> 00:10:26,149 going to look like so what we do here is we'll click on this little refresh 94 00:10:26,149 --> 00:10:31,040 button on the diagram so there is our state machine so it's going to start 95 00:10:31,040 --> 00:10:35,690 it's going to invoke the receive order Lambda function it's going to then 96 00:10:35,690 --> 00:10:40,040 invoke the check stock Lambda function and then we're going to have a decision 97 00:10:40,040 --> 00:10:44,600 being made is stock available and so that depends on what we receive from 98 00:10:44,600 --> 00:10:48,950 check stock so it's if it's a one it's going to deliver the order and then bill 99 00:10:48,950 --> 00:10:52,970 customer if it's a zero it's going to do order stock and then stop from there 100 00:10:52,970 --> 00:10:55,980 it'll fail because we can't fulfill the order because there's 101 00:10:55,980 --> 00:11:01,350 no stock available so we look on the on the side here we've got these red 102 00:11:01,350 --> 00:11:06,210 crosses meaning that there's some errors so what's going on here is that we need 103 00:11:06,210 --> 00:11:10,680 to have our ir end for our lambda function so that one is of the receive 104 00:11:10,680 --> 00:11:17,190 order Lambda function so let's go into here click on our receive order copy the 105 00:11:17,190 --> 00:11:26,040 ARN for that and paste it in there and the next one is our chick stock so let's 106 00:11:26,040 --> 00:11:33,300 go back to our functions get our check stock Lambda function and copy that and 107 00:11:33,300 --> 00:11:40,290 paste that one in and then there should be five of these so there's the other 108 00:11:40,290 --> 00:11:49,730 three next one is going to be order stock order stock 109 00:11:49,730 --> 00:11:56,160 we'll get the ARN for that one and put that in there next one's going to be 110 00:11:56,160 --> 00:12:03,570 deliver order so you can go to the Lambda console and deliver order and 111 00:12:03,570 --> 00:12:10,260 we're going to copy that ARN, put that one in there and the last one is 112 00:12:10,260 --> 00:12:15,080 going to be our bill customer Lambda function 113 00:12:17,750 --> 00:12:25,790 and copy the ARN and paste that in so now all of those red arrow crosses have 114 00:12:25,790 --> 00:12:32,329 disappeared and we'll just refresh that and if we scroll down here we can click 115 00:12:32,329 --> 00:12:39,079 on next okay scrolling back up we'll leave the name as my state machine 116 00:12:39,079 --> 00:12:45,310 that's fine we'll choose an existing wrong so we check our we collect our 117 00:12:45,310 --> 00:12:50,089 step functions basic execution so the one that we created previously 118 00:12:50,089 --> 00:12:55,639 we'll use that leave logging as it is in tags as I am and create that state 119 00:12:55,639 --> 00:13:03,589 machine okay so the first step in the process is to receive order so we need 120 00:13:03,589 --> 00:13:09,649 to go and have a look at our receive order Lambda function and see what it's 121 00:13:09,649 --> 00:13:18,470 looking to receive so go back to our functions and our receive order and if 122 00:13:18,470 --> 00:13:25,730 we scroll down it's going to be looking for a field called order with a number 123 00:13:25,730 --> 00:13:30,290 in there so that's all that's looking for so that's what we need to pass to to 124 00:13:30,290 --> 00:13:34,970 the start of our process because receive order is a start of our process so go 125 00:13:34,970 --> 00:13:39,680 back to our step function and we can start the execution so we'll start 126 00:13:39,680 --> 00:13:44,029 execution and we're going to have to input something in here so that's going 127 00:13:44,029 --> 00:13:52,100 to be order and then a order number and then we'll just use double zero one 128 00:13:52,100 --> 00:13:55,279 that's fine 001 so that is all that we 129 00:13:55,279 --> 00:13:58,639 need to pass to get the whole thing started so we'll start that execution 130 00:13:58,639 --> 00:14:05,750 and see what happens and so there we go so it's received the order it's check 131 00:14:05,750 --> 00:14:12,949 the stock is the stock available so a random number generator has generated a 132 00:14:12,949 --> 00:14:17,600 1 and because of that one it has actually gone and delivered that order 133 00:14:17,600 --> 00:14:23,839 and then we're going to build that customer so let's run that again and see 134 00:14:23,839 --> 00:14:27,350 what we get so we'll do another execution and see what the random 135 00:14:27,350 --> 00:14:32,020 number comes up with this time so start execution 136 00:14:32,020 --> 00:14:36,620 and it's gonna be the same spot again we'll try it again see if we can get get 137 00:14:36,620 --> 00:14:47,350 it to produce a zero it's still producing ones we'll try it again still 138 00:14:47,350 --> 00:14:56,230 and much right once more still doing ones try it again 139 00:14:56,230 --> 00:15:03,260 and finally our random number generator has produced a zero so we've got a 140 00:15:03,260 --> 00:15:07,910 fail here so receive order the input that we receive is what we sent to it 141 00:15:07,910 --> 00:15:14,260 was order double zero one the output was water has been received we check stock 142 00:15:14,260 --> 00:15:18,980 it's received that and it's done a status at zero products are out of stock 143 00:15:18,980 --> 00:15:24,320 because it's given us a status of zero it's going into here we've now gone to 144 00:15:24,320 --> 00:15:29,900 the order stock so order stock has been ordered and then we go to fail because 145 00:15:29,900 --> 00:15:38,060 it's received that okay so if we scroll down here we can see the execution event 146 00:15:38,060 --> 00:15:41,780 history and so we've got a link here to the resource so that's going to be our 147 00:15:41,780 --> 00:15:46,460 receive or tool and a function there this one will be the check stock one but 148 00:15:46,460 --> 00:15:51,380 we also can link to our cloudwatch log so we click on that'll open the 149 00:15:51,380 --> 00:15:54,710 cloudwatch logs for our receive order so we just click on that and see what's 150 00:15:54,710 --> 00:16:03,860 happening so there we can see we've got our receive order a log group for that 151 00:16:03,860 --> 00:16:09,380 and here's our log stream for that so there we can see order has been received 152 00:16:09,380 --> 00:16:14,630 order has been received so that's all working perfectly okay before we finish 153 00:16:14,630 --> 00:16:19,340 up with the lab what I want to do is just run through very quickly how the 154 00:16:19,340 --> 00:16:24,680 step functions code works and how we format this JSON object so that's 155 00:16:24,680 --> 00:16:29,030 understandable by the step function service so what I've done here is I've 156 00:16:29,030 --> 00:16:34,790 copied the code the JSON code or the step function is JSON file the contents 157 00:16:34,790 --> 00:16:39,680 of that and I've put it here into a JSON editor now you can get to this editor 158 00:16:39,680 --> 00:16:44,800 just go to JSON editor on line org or else just Google JSON editor 159 00:16:44,800 --> 00:16:48,730 line and you'll find it there easy enough so I pasted it in here on the 160 00:16:48,730 --> 00:16:52,660 left hand side and I've just clicked copy to copy it over onto the right hand 161 00:16:52,660 --> 00:16:58,089 side and displayed that as a tree so you can select up here as a code or tree and 162 00:16:58,089 --> 00:17:03,970 that allows us to visually see what's going on so the first line here is we've 163 00:17:03,970 --> 00:17:07,839 got comments so that's obviously just a comment describing what this state 164 00:17:07,839 --> 00:17:13,540 machine is doing and that's optional so if we leave it out it will still run 165 00:17:13,540 --> 00:17:19,360 fine the next one there is start at that is not optional that you must have a 166 00:17:19,360 --> 00:17:26,829 starting state so it starts at the state of receive order so obviously we need to 167 00:17:26,829 --> 00:17:33,309 define what's receive order is so these states which is next is also compulsory 168 00:17:33,309 --> 00:17:37,600 you need to have that in there so obviously we need to have our receive 169 00:17:37,600 --> 00:17:41,530 order state so just going on here onto the right hand side here so we can see 170 00:17:41,530 --> 00:17:46,600 we've got started receive order and then our different states that we've got 171 00:17:46,600 --> 00:17:55,270 there I'll just expand that out so there is our receive order state and it's got 172 00:17:55,270 --> 00:18:01,270 a type of task now type is also compulsory so you need to have that in 173 00:18:01,270 --> 00:18:06,970 it that is required and if you don't have type in a task definition then that 174 00:18:06,970 --> 00:18:11,200 will not work also so you need to have started you need to have these states 175 00:18:11,200 --> 00:18:17,350 and within that stage you need to have a type so the type is a task and we need 176 00:18:17,350 --> 00:18:21,090 to define what the resource is and so the resource is our Lambda function and 177 00:18:21,090 --> 00:18:24,790 then what is the next state that we're going to and that is going to be check 178 00:18:24,790 --> 00:18:29,470 stock so it's pretty self-explanatory and in check stock we've got type tasks 179 00:18:29,470 --> 00:18:35,440 and then the check stock lambda AR n and then the next one is is stock available 180 00:18:35,440 --> 00:18:42,370 and so again that's type task and in is stock available this is of type choice 181 00:18:42,370 --> 00:18:48,460 so it's not a task it's a choice and then we have an array of choices so 182 00:18:48,460 --> 00:18:52,690 let's expand data rate and that will give us a list of choices so the first 183 00:18:52,690 --> 00:18:58,510 choice here is if they are variable status so what happens here 184 00:18:58,510 --> 00:19:04,300 is that our check stock we'll send back a JSON object which will have the order 185 00:19:04,300 --> 00:19:10,140 ID but it will also have a field status and that will be either 0 or 1 depending 186 00:19:10,140 --> 00:19:16,860 on what our random number generator generated so if that status comes back 187 00:19:16,860 --> 00:19:23,680 numeric equals 1 so it comes back as one our next state will be to deliver order 188 00:19:23,680 --> 00:19:29,860 so let's go down to deliver order and that's the task this time so we define 189 00:19:29,860 --> 00:19:35,200 the task the lambda AR n for deliver order the next one there is going to be 190 00:19:35,200 --> 00:19:40,450 bill customer so let's have a look at Bill customer so again that's a tight 191 00:19:40,450 --> 00:19:46,960 that's a task and the resource the land AR n but there is no next we don't have 192 00:19:46,960 --> 00:19:51,460 a next year we got an end and the end is true so it's going to come to bill 193 00:19:51,460 --> 00:19:55,510 customer and then it's going to stop so the statement state machine it will 194 00:19:55,510 --> 00:20:01,240 finish at that point and it won't continue to do any more functions so 195 00:20:01,240 --> 00:20:07,870 that is if numeric equals 1 if the status equals 1 so let's just minimize 196 00:20:07,870 --> 00:20:15,280 these again so let's have a look at the other choice m so if the status is 0 we 197 00:20:15,280 --> 00:20:21,490 go to order stock so we'll have a look at order stock so again this is a task 198 00:20:21,490 --> 00:20:30,430 we have our order stock AR n but the next one is fail so let's go to fail now 199 00:20:30,430 --> 00:20:36,520 this is of type fail so it's not a choice it's not a task this is a fail so 200 00:20:36,520 --> 00:20:40,900 this will come up in red on our flow chart when it's when it's invoked or 201 00:20:40,900 --> 00:20:45,370 when it when it gets to this state and then we need to define a cause so the 202 00:20:45,370 --> 00:20:49,930 cause is it's a description of what happened and so that is how it all works 203 00:20:49,930 --> 00:20:56,170 so quite self-explanatory if you wanted to see a little bit more on this there's 204 00:20:56,170 --> 00:21:01,600 a tiny bit of documentation but it's a very very simple language to use or 205 00:21:01,600 --> 00:21:05,080 simple format to use it's not really much going on you just need to know 206 00:21:05,080 --> 00:21:11,230 really task choice and and fail and they're the basic types of the states 207 00:21:11,230 --> 00:21:13,630 pretty feet forward so I hope you've got a lot 208 00:21:13,630 --> 00:21:20,080 out of that so let's move on now and start to clean up everything so we'll 209 00:21:20,080 --> 00:21:26,650 just close that out and we're going to delete this step function now and clean 210 00:21:26,650 --> 00:21:33,220 it all up so we'll go into state machines and delete and delete that 211 00:21:33,220 --> 00:21:37,150 state machine so that'll be chugging away we just go through and delete all 212 00:21:37,150 --> 00:21:42,900 these Lambda functions now delete 213 00:22:05,800 --> 00:22:12,220 and they're all gone if we refresh this that's been deleted as well so that 214 00:22:12,220 --> 00:22:14,980 brings us to the end of the lab I hope you've got a lot out of it 215 00:22:14,980 --> 00:22:19,410 and I look forward to seeing you in the next one