Welcome back to Backspace Academy. In this lab we are going to be creating a 2
fictitious ordering and fulfillment process and what we're going to do is, 3
we're going to create a number of different Lambda functions to receive 4
the order, to process the order, to check in their stock and we're going to 5
coordinate all of the handling of the interaction between those Lambda 6
functions as a state machine and we're going to use aws step functions to 7
make that happen. So let's get into it. So starting off at 8
the Lambda console. We're going to create our Lambda functions first. So again 9
create function and we're going to be authoring this from scratch. We'll give 10
this function a name. The first one is going to be receiving our order. So it's 11
got to cut it and paste this from the lab notes. Nodejs it will be. We'll 12
choose our execution role and create that function, and we've got to update 13
the code for that, and we'll scroll down 14
and make sure that we save that now. So I'm just going to do a quick test on it 15
and see what it returns 16
and run that test so that's succeeded okay and it's just going sending ordered 17
order and that we haven't sent it an order number or anything like that but the 18
message is right. So that seems to be behaving as it should. So order and then 19
undefined because we didn't send a order number there to it. That seems okay. 20
We'll create our next one. So we go to functions and create 21
function, and we're going to call this one, I'll just have a look, this one's 22
going to be check stock, and the same as before nodejs and use that existing role 23
and the basic execution and create that function, and I'll copy this code here 24
from the lab notes again and I'll paste it in. Okay so let's have a look and see 25
what's going on here. So we're going to be having our handler here and it's 26
going to receive that event. First thing we're going to do is we're going to 27
console log out that the function is loaded successfully. So we've got a 28
temporary storage here of our string so our order ID is going to be event order. 29
So we need to pass a field called order in our object then 30
when we send it to this function. It's going to console out that we're 31
checking the stock, and so what we're doing here is we're creating a random 32
number so it's going to be either 0 or 1 and depending on what that random number 33
comes up with will depend on or will decide exactly whether there's stock or 34
not. So again this is a fictitious process so we need to somehow make it 35
realistic with a random number. So if it returns 1 then we're going to say that 36
the products are in stock. If it returns 0 then the products are out of stock. 37
Then we have our response the order ID and then the status which is stock 38
status. So that will be either 1 or 0, and then we'll return that response. Provided 39
there's no errors there and so that will be sent back to the requester and 40
our requestor is going to be the 41
AWS step functions process or step function service and so that will decide, make a 42
decision on where to send that request further. So we just save that and we'll 43
just do a quick test to make sure that's behaving reasonably how we expect it, and 44
test that, and that's done. Okay so just sending a status of one and products are 45
in stock. So if we run that again or run it a few times when I get a status of 46
zero. So we just run it again it's still one. We'll try it again. There we go so 47
the status is zero. So our random number generator is changing the status from 48
zero to one depending on on what it produces so that's saved we will get out of 49
that one and we'll create another function and this one will be called 50
order stock. This one is so again just copying from the lab notes here their 51
execution role again 52
and create that function so again just copying and pasting from the lab notes 53
you for our order stock. Okay so here again we've got a handle on receiving 54
event and all we're doing is just sending back the order IDs just 55
returning the response which will be the order ID and the message and that 56
message will be order order the ID has stock ordered so nothing too spectacular 57
going on there. We'll just save that and we'll test it to make sure it's behaving 58
reasonably how we expect it. Okay so we'll just run that test have a look at 59
that order and we haven't passed a order there so that's going to be undefined 60
that's fine so order and has stock ordered so that's seems to be fine. We'll 61
create our next function which is going to be called deliver order. So create the 62
function and our execution role again 63
soul and the basic execution and create that function and again I'll just cut 64
and paste from the or copy and paste from the lab notes and again the same 65
thing it's just receiving that event in that it's just going to send back order 66
and then the order ID is being delivered so nothing spectacular there going as 67
well we'll save that we'll test that 68
so let's run that test okay so that seems fine so order and the undefined 69
because of again we didn't send an order ID it's being delivered so that's 70
working fine now we'll do the next one and the next one there which i think is 71
the last one it's called build customer so i'll create a function and build 72
customer our execution role again and create that function and again copying 73
and pasting from the lab notes okay so the same thing again it's just receiving 74
that event and we're just going to send back to the request of the order and 75
then the order ID is being billed to the customer so we'll save that and we'll 76
test that again and create that test and run that test that looks fine so order 77
and then undefined we didn't send the order ID so it's going to be undefined 78
that's okay so order is being billed to customer that's all working fine and so 79
let's have a look so we should have five functions in. So we're receiving the 80
order we're going to check it this stock if there's no stock then we're going to 81
order stock if there is stock then we're going to deliver that order and then 82
we're going to build the customer so we need to somehow define this process and 83
we're going to do that using step functions so let's go to services 84
actually before we do that, we need to create an IAM role for this step 85
function so we'll go into services and then IAM. So step functions needs to 86
have an IAM role to be able to invoke these Lambda functions. So we go into 87
roles and create role 88
and so our AWS service is going to be step functions and next permissions. So it's 89
already created this for us our AWS Lambda role so it's been a bit proactive for 90
us there so that's fine next tags and next to review and we'll 91
give this role a name and again I'll just copy this from the lab notes so 92
step functions basic execution and we'll create that role so that's done so now 93
we can use that role with our step function coded services and then step 94
functions and what we're going to do is just going to open up this toolbar on 95
the left hand side here and we're going to go to state machines and we're going 96
to create a state machine so we're going to author with code snippets who are 97
going to provide our own snippets it's going to be a standard type so we get 98
the code from our github repository 99
okay so there is our step functions repository them on our BackSpace 100
Academy github and so what we need to get here is our step function JSON so we 101
just open that up and I'm just gonna click on raw here select all that and 102
I'm going to scroll down here to our definition and so that's what they're 103
supplied to us and it's really not very good for us at all so we just delete 104
that and paste in from our from our github repository and so if we click on 105
this refresh button here we can see that what is going on here so we we start we 106
receive our order so receive order is a Lambda function and then check stock is 107
a Lambda function and that will send either a 1 or a 0 to step functions and 108
based on that we can see whether there is stock available so if it's a 1 it's 109
going to go through and deliver that order and then build a customer if it's 110
a 0 then it's going to do the order stock Lambda function and then it will 111
fail because it cannot fulfill that order so you can see here we've got some 112
red crosses here which are errors in our code so what's going on there it's 113
looking for you because hover over the top of that the field value is not a 114
valid Lambda function they are n so we need to get a valid Lambda function 115
arn so obviously we need to go back to our Lambda console so the first one we 116
need there is receive order so go into here we'll click on receive order and 117
we'll select our ARN for that that's copied and we'll paste that in 118
there and there we go so there all of a sudden that Red Cross is gone so the 119
next one we need to do is the check stock one so again we go into our 120
functions click on check stock copy the ARN for that 121
and paste that one in there as well and again our Red Cross has disappeared and 122
we've got another three to do there so the next one is order stock so click on 123
order stock get our ARN and paste it in there next one is deliver order and we 124
copy how they are in that work yep and paste that in there so there's our 125
deliver order paste it in there and last one is our build customer 126
copied 127
okay so that looks fine everything is done there so what have we got so we've 128
got our author with code snippets it's a standard type and we can format the JSON 129
there if it's any problems with it looks fine I'll update this here and make sure 130
there's no problems here and so that's exactly what we should be expecting 131
there and there's no errors you know in our code whatsoever we'll click on next 132
okay so we'll choose an existing role for IAM role so just scroll up here so 133
we give it a name I'm just going to leave it as my state machine that's fine 134
with me we're not going to create a new role we're going to use our existing 135
role so the role that we used was his step functions basic at your new 136
execution that we created before and we're not going to have logging there 137
and no tags let's create that state machine okay so that seems to have 138
worked perfectly okay so what we can do now is we can start that execution but 139
before we do that let's have a look and see what we've got a pass to our state 140
machine for it to work so just go on doing click on edit and have a look here 141
so the first thing here is we're going to invoke the receive order Lambda 142
function so I'm just going to get out of that now and go into a Lambda and get 143
that receiver order so let's have a look at the code and see what we've got a 144
pass to it so scroll down here so it's going to be looking for event and event 145
order and nothing else so the only thing we need to do is we need to send a JSON 146
object with an order field in there and that should be fine so just get back out 147
of that now and did we go back into our step function 148
we can start our execution and what we need to do is change that I'm just gonna 149
cut and cut or copy and paste from the lab notes again 150
okay so we just need to pass that order and just going to give it order zero 151
zero one that will be fine and let's start that execution and see what 152
happens so here we go so it's gone through its its work perfectly its 153
receive the order it's check the stock and our random number generator it's 154
generated either a zero or one this time it's generated a zero so it's sense that 155
there's no stock so if ordered stock and it's failed so we can't fulfill the order 156
cause there is no stock if we run it again we might get it to run on this other 157
side depending on our random number generator so let's have a look another go. 158
So I do a new execution and start execution let's see what happens this 159
time and this time I went to the other side so we've got receive order check 160
stock is stopped available and then bill customer okay so that's the input 161
message auto zero and then that's that's all finished that's fine so let's have a 162
look at all these so we can click on receive order and receive to order zero 163
zero one which is what we sent to it and it outputted order zero zero one and 164
then has been received we'll click on check stock order has been received it 165
we received and then it did that random number generator that generated status 1 166
and the message products are in stock and so because it checked one he goes to 167
deliver order we can go into is stock available and have a look at that 168
and so that received a 1 and because it received a 1 it forwarded the request 169
over to deliver order we receive that and then Bill customer which is the 170
final one there that went through let's run it again and see what happens again 171
okay so this time it went on the zero so a check is step available or a do a 172
check stock maybe so the input and then the output is that this one is the 173
status of zero products are out of stock because they're out of stock we've gone 174
to this side here and order the stock so it's received a stat at a zero products 175
are out of stock and then it's all of that stock and then because it's 176
received this then the process is stopped we can't fulfill that order so 177
if we scroll down here we can see exactly what went on with our event 178
execution history and so it's got a link to our receive order resource there the 179
Lambda if we want to have a look at that but it's also got the Cloudwatch log so 180
if there was any problem there we can always have a look at those Cloudwatch 181
logs for that receive order so we click on that it should open up the 182
Cloudwatch logs for receive order so here we go so it's e receive order and our log 183
group for that and so we can see here we click on that we should have our log 184
stream so there it is there so function loaded successively returning response 185
order blah blah blah so that's all working fine okay before we finish up 186
with the lab what I want to do is just run through very quickly how the step 187
functions code works and how we format this JSON object so it's understandable 188
by the step function service so what I've done here is I've copied the code 189
the JSON code or the step function is JSON file the contents of that and I've 190
put it here into a JSON editor now you can get to this editor just go to JSON 191
editor online org or else just Google JSON editor online and you'll find it 192
there easy enough so I pasted it in here on the left hand side and I've just 193
clicked copy to copy it over on to the right hand side and display that as a 194
tree so you can select up here is a code or tree and that allows us to visually 195
see what's going on so the first line here is we've got comments so that's 196
obviously just a comment describing what the state machine is doing 197
and that's optional so if we leave it out it will still run fine 198
the next one there is start at that is not optional that you must have a 199
starting state so it starts at the state of receive order so obviously we need to 200
define what's receive order is so these states which is next is also compulsory 201
you need to have that in there so obviously we need to have our receive 202
order state so just going on here on to the right hand side here so we can see 203
we've got started receive order and then our different states that we've got them 204
I'll just expand that out so there is our receive order state and it's got a 205
type of task now type is also compulsory so you need to have that in it that is 206
required and if you don't have type in a task definition then that will not work 207
also so you need to have started you need to have these states and within 208
that stage you need to have a type so the type is a task and we need to define 209
what the resource is and so the resource is our Lambda function and then what is 210
the next state that we're going to and that is going to be check stock so it's 211
pretty self-explanatory and in check stock we've got type tasks and then the 212
check stock lambda AR n and the next one is is stock available and so again 213
that's type task and in is stock available this is of type choice so it's 214
not a task it's a choice and then we have an array of choices so let's expand 215
that array and that will give us a list of choices so the first choice said is 216
if they are variable status so what happens here is that our check stock 217
will send back a JSON object which will have the order ID but it will also have 218
a field status and that will be either 0 or 1 depending on what our random number 219
generator generated so if that status comes back numeric equals 1 so it comes 220
back as 1 our next state will be deliver order so let's go down to 221
deliver order and that's a task this time so we define the task the lambda AR 222
n for it deliver order the next one there is going to be bill customer so 223
let's have a look at Bill customer so again that's a tight that's a task and 224
the resource the land AR n but there is no next we don't have a next year we got 225
an end and the end is true so it's going to come to bill customer and then it's 226
going to stop so the statement state machine it will finish at that point and 227
it won't continue to do any more functions so that is if numeric equals 228
one if the status equals one so let's just minimize these again so let's say 229
look at the other choice here so if the status is zero we go to order stock so 230
we'll have a look at order stock so again this is a task we have our order 231
stock AR n but the next one is fail so let's go to fail now this is of type 232
fail so it's not a choice it's not a task this is a fail so this will come up 233
in red on our flow chart when it's when it's invoked or when it when it gets to 234
this state and then we need to define a cause so the cause is it's a description 235
of what happened and so that is how it all works so quite self-explanatory if 236
you wanted to see a little bit more on this there's a tiny bit of documentation 237
but it's a very very simple language to use or simple format to use it's not 238
really much going on you just need to know really task choice and and fail and 239
they're the basic types of the states pretty straightforward so I hope you've 240
got a lot out of it so let's move on now and start to clean 241
up everything and that's a pretty good introduction to AWS step functions so I 242
hope you got a lot out of it so what we can do now is we can clean all this up 243
so we need to get rid of this state function or this state machine go to our 244
state machines select it and delete it 245
so that's near deleting and we'll just go to our Lambda functions and delete 246
each one of those as well 247
and there we go so they're deleted and our state machinery refreshes screen 248
should be deleted as well okay that brings us to the end hope you've got a 249
lot out of it and I look forward to seeing you in the next one