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