1 00:00:00,370 --> 00:00:03,870 ‫So let's do an introduction to Step Functions. 2 00:00:03,870 --> 00:00:05,960 ‫And we are going to go into the step function, 3 00:00:05,960 --> 00:00:09,090 ‫get started and there is a Hello World example right away 4 00:00:09,090 --> 00:00:10,140 ‫to get up and running, 5 00:00:10,140 --> 00:00:12,350 ‫so there is a very complex flow, 6 00:00:12,350 --> 00:00:14,863 ‫so pass Hello World example, this is a choice state. 7 00:00:14,863 --> 00:00:16,700 ‫Yes. Wait three seconds. 8 00:00:16,700 --> 00:00:18,570 ‫Hello and World, Hello World and the End. 9 00:00:18,570 --> 00:00:20,080 ‫So all these things really, 10 00:00:20,080 --> 00:00:22,000 ‫look at this json document 11 00:00:22,000 --> 00:00:24,899 ‫and generates a graph, a workflow from the right-hand side. 12 00:00:24,899 --> 00:00:29,340 ‫And so this is for example, IsHelloWorldExample, 13 00:00:29,340 --> 00:00:30,540 ‫false or true, 14 00:00:30,540 --> 00:00:32,350 ‫so I will show you how this works in a second, 15 00:00:32,350 --> 00:00:34,880 ‫so the idea is that's based on the input we have 16 00:00:34,880 --> 00:00:36,071 ‫we should go into one branch 17 00:00:36,071 --> 00:00:39,560 ‫on the left-hand side or run branch on the right-hand side. 18 00:00:39,560 --> 00:00:42,800 ‫So let's go ahead and creates this workflow. 19 00:00:42,800 --> 00:00:44,220 ‫This is the name, HelloWorld. 20 00:00:44,220 --> 00:00:46,420 ‫We're going to create a new role for this 21 00:00:46,420 --> 00:00:49,740 ‫and then we will creates the state machine. 22 00:00:49,740 --> 00:00:51,060 ‫So to run this workflow 23 00:00:51,060 --> 00:00:55,070 ‫we have to specify "IsHelloWorldExample": true or false. 24 00:00:55,070 --> 00:00:56,920 ‫So in this example we're going to say true. 25 00:00:56,920 --> 00:00:59,169 ‫We're going to open a new browser window, 26 00:00:59,169 --> 00:01:01,340 ‫and this is going to do an execution 27 00:01:01,340 --> 00:01:03,730 ‫of a workflow with this inputs. 28 00:01:03,730 --> 00:01:06,490 ‫And as you can see, the execution is happening right now. 29 00:01:06,490 --> 00:01:08,300 ‫We went into the Hello World example, 30 00:01:08,300 --> 00:01:09,133 ‫it detected it. 31 00:01:09,133 --> 00:01:10,280 ‫Yes it was Hello World example, 32 00:01:10,280 --> 00:01:12,467 ‫so we went to the yes, it waited three seconds, 33 00:01:12,467 --> 00:01:14,087 ‫it ran this parallel states, 34 00:01:14,087 --> 00:01:16,341 ‫give this the Hello World outputs, 35 00:01:16,341 --> 00:01:18,130 ‫and if we look at the step outputs, 36 00:01:18,130 --> 00:01:19,580 ‫we get this operated 37 00:01:19,580 --> 00:01:21,708 ‫which is not a very interesting outputs 38 00:01:21,708 --> 00:01:24,580 ‫but still shows us that's the whole execution 39 00:01:24,580 --> 00:01:26,510 ‫happened like this. 40 00:01:26,510 --> 00:01:29,053 ‫And all the steps of the Execution history 41 00:01:29,053 --> 00:01:30,930 ‫happened in here, 42 00:01:30,930 --> 00:01:32,770 ‫and we can debug them one by one 43 00:01:32,770 --> 00:01:34,071 ‫by looking at what was the inputs 44 00:01:34,071 --> 00:01:35,750 ‫and what was the outputs 45 00:01:35,750 --> 00:01:37,350 ‫which is really, really handy. 46 00:01:37,350 --> 00:01:39,420 ‫So this shows you already like a very good, 47 00:01:39,420 --> 00:01:40,947 ‫powerful way of doing step functions 48 00:01:40,947 --> 00:01:43,310 ‫using some very simple steps, 49 00:01:43,310 --> 00:01:45,430 ‫but these steps could be Lambda functions. 50 00:01:45,430 --> 00:01:47,490 ‫So if you want to do a new execution 51 00:01:47,490 --> 00:01:48,323 ‫and this time we say 52 00:01:48,323 --> 00:01:50,450 ‫"IsHelloWorldExample": false 53 00:01:50,450 --> 00:01:52,768 ‫and we open it in a new tab again. 54 00:01:52,768 --> 00:01:55,559 ‫This time we're going to go into a failed states 55 00:01:55,559 --> 00:01:58,110 ‫because well, IsHelloWorldExample was false 56 00:01:58,110 --> 00:02:00,800 ‫and so we went into the No states and there was an End. 57 00:02:00,800 --> 00:02:02,870 ‫And again, we can debug this directly 58 00:02:02,870 --> 00:02:04,840 ‫from the Execution event history 59 00:02:04,840 --> 00:02:06,479 ‫which is really, really handy. 60 00:02:06,479 --> 00:02:07,790 ‫So we're going to go ahead 61 00:02:07,790 --> 00:02:09,630 ‫and create our own state machines. 62 00:02:09,630 --> 00:02:10,790 ‫So we have a state machine here. 63 00:02:10,790 --> 00:02:12,210 ‫This is the HelloWorld one 64 00:02:12,210 --> 00:02:15,128 ‫but I'm going to author our own state machine, okay? 65 00:02:15,128 --> 00:02:17,223 ‫Using a standard workflow 66 00:02:17,223 --> 00:02:20,730 ‫and we have to pass on some data. 67 00:02:20,730 --> 00:02:24,128 ‫So to do so we're going to first create a Lambda function 68 00:02:24,128 --> 00:02:27,140 ‫that will be invoked by our step functions. 69 00:02:27,140 --> 00:02:29,453 ‫So I'm gonna go into Lambda, 70 00:02:30,327 --> 00:02:32,012 ‫open this in a new tab 71 00:02:32,012 --> 00:02:33,667 ‫and create our first functions. 72 00:02:33,667 --> 00:02:38,667 ‫So I'm going to create a function in here. 73 00:02:39,260 --> 00:02:41,460 ‫We will call it this function. 74 00:02:41,460 --> 00:02:43,020 ‫We'll call it HelloFunction. 75 00:02:43,020 --> 00:02:45,918 ‫It's going to be node.js 14.x 76 00:02:45,918 --> 00:02:48,663 ‫and then it will creates this function. 77 00:02:52,051 --> 00:02:52,890 ‫(computer mouse clicks) 78 00:02:52,890 --> 00:02:54,210 ‫Now in terms of the code that we're going to use 79 00:02:54,210 --> 00:02:56,120 ‫for this function, we have to go into step functions, 80 00:02:56,120 --> 00:02:57,660 ‫the code directory, hello-world, 81 00:02:57,660 --> 00:03:02,330 ‫and then function.js and copy and paste this. 82 00:03:02,330 --> 00:03:05,675 ‫So if we go into the code itself in here for index.js, 83 00:03:05,675 --> 00:03:09,130 ‫just paste it and this is a very simple function 84 00:03:09,130 --> 00:03:10,930 ‫is going to say "Hello" 85 00:03:10,930 --> 00:03:13,070 ‫plus the content of the who key 86 00:03:13,070 --> 00:03:14,720 ‫in the event documents. 87 00:03:14,720 --> 00:03:16,130 ‫So we can test it, 88 00:03:16,130 --> 00:03:17,880 ‫by passing in a new test event. 89 00:03:17,880 --> 00:03:19,790 ‫I'll call it Hello 90 00:03:19,790 --> 00:03:21,466 ‫and the key has to be "who": 91 00:03:21,466 --> 00:03:24,380 ‫and the value is going to be "John". 92 00:03:24,380 --> 00:03:25,760 ‫So if we go in here 93 00:03:25,760 --> 00:03:29,700 ‫and pass this test events to our Lambda function 94 00:03:29,700 --> 00:03:33,100 ‫and now test it is going to say "\"Hello from Lambda!\"" 95 00:03:33,100 --> 00:03:35,790 ‫and somehow it was not deployed 96 00:03:35,790 --> 00:03:38,400 ‫so I need to save my function 97 00:03:38,400 --> 00:03:41,464 ‫by doing ctrl+S and then test it again. 98 00:03:41,464 --> 00:03:45,370 ‫(computer mouse clicks) 99 00:03:45,370 --> 00:03:46,640 ‫So I forgot to click on deploy 100 00:03:46,640 --> 00:03:47,790 ‫so I just clicked on deploy 101 00:03:47,790 --> 00:03:49,045 ‫and we're going to test it again 102 00:03:49,045 --> 00:03:51,210 ‫and this time it says, "Hello, John!". 103 00:03:51,210 --> 00:03:52,043 ‫So very simple. 104 00:03:52,043 --> 00:03:53,610 ‫Based on the input we have, 105 00:03:53,610 --> 00:03:54,840 ‫we have the response "Hello", 106 00:03:54,840 --> 00:03:57,060 ‫and the who we just passed. 107 00:03:57,060 --> 00:03:57,893 ‫Okay, great. 108 00:03:57,893 --> 00:03:58,790 ‫So this Lambda function 109 00:03:58,790 --> 00:04:02,060 ‫we'll be using it for our step functions. 110 00:04:02,060 --> 00:04:04,440 ‫So we need to find the Lambda function ARN 111 00:04:04,440 --> 00:04:07,780 ‫which is right here, and we'll be using it later on. 112 00:04:07,780 --> 00:04:08,806 ‫Okay. So next we go, 113 00:04:08,806 --> 00:04:10,913 ‫we have our states machine right here 114 00:04:10,913 --> 00:04:12,410 ‫and so I'm going through my code. 115 00:04:12,410 --> 00:04:14,580 ‫I opened the state-machine.json, 116 00:04:14,580 --> 00:04:18,010 ‫copy this entirely and paste it here. 117 00:04:18,010 --> 00:04:19,270 ‫So what we're trying to do 118 00:04:19,270 --> 00:04:21,180 ‫and I'm going to refresh this to display it. 119 00:04:21,180 --> 00:04:22,850 ‫So what we're going to do is to first 120 00:04:22,850 --> 00:04:24,550 ‫Invoke Lambda function 121 00:04:24,550 --> 00:04:27,212 ‫and then evaluate the result of this Lambda function 122 00:04:27,212 --> 00:04:30,246 ‫and determine if I am a teacher or I'm not a teacher 123 00:04:30,246 --> 00:04:32,150 ‫and then go to the End results. 124 00:04:32,150 --> 00:04:34,480 ‫Okay. Now we have to specify a few things. 125 00:04:34,480 --> 00:04:36,650 ‫Number one, for the "Invoke Lambda function" 126 00:04:36,650 --> 00:04:38,950 ‫which is the "StartAt": states, 127 00:04:38,950 --> 00:04:41,250 ‫we're going to specify the name of the resource. 128 00:04:41,250 --> 00:04:43,290 ‫So we go into the Lambda function, 129 00:04:43,290 --> 00:04:47,256 ‫we find the ARN right here, and we paste it. 130 00:04:47,256 --> 00:04:48,089 ‫(computer mouse clicks) 131 00:04:48,089 --> 00:04:52,250 ‫Okay. Next we have a thing called "InputPath":, 132 00:04:52,250 --> 00:04:54,760 ‫so this is what gets passed to this Lambda function. 133 00:04:54,760 --> 00:04:56,820 ‫And by default at the input of my workflow 134 00:04:56,820 --> 00:04:57,950 ‫will be passing by them the function, 135 00:04:57,950 --> 00:04:59,900 ‫because this is the first Task. 136 00:04:59,900 --> 00:05:01,770 ‫But anyway, I'm still having it here 137 00:05:01,770 --> 00:05:03,540 ‫just to make it very obvious. 138 00:05:03,540 --> 00:05:06,480 ‫So once this is done, the "Next": state is a "Choice State", 139 00:05:06,480 --> 00:05:08,990 ‫so we're going to go into this "Choice State": right here 140 00:05:08,990 --> 00:05:10,150 ‫which is a "Choice" 141 00:05:10,150 --> 00:05:11,580 ‫and it's going to evaluate one choice. 142 00:05:11,580 --> 00:05:14,300 ‫It's going to look at the outputs of the previous states 143 00:05:14,300 --> 00:05:16,880 ‫so dollar sign is the output of the previous states. 144 00:05:16,880 --> 00:05:18,926 ‫And it's going to look at if there is "Stephane" in it, 145 00:05:18,926 --> 00:05:20,582 ‫and if there is "Stephane" in it, 146 00:05:20,582 --> 00:05:23,770 ‫then you should go to "Is Teacher". 147 00:05:23,770 --> 00:05:25,750 ‫And "Is Teacher": is of "Type": "Pass" 148 00:05:25,750 --> 00:05:26,990 ‫and the "Result": is "Woohoo!" 149 00:05:26,990 --> 00:05:28,590 ‫and the "End": is true. 150 00:05:28,590 --> 00:05:29,730 ‫But if it's not, 151 00:05:29,730 --> 00:05:32,097 ‫then the "Default": is to go to "Not Teacher". 152 00:05:32,097 --> 00:05:34,870 ‫So were gonna go to the "Not Teacher" states 153 00:05:34,870 --> 00:05:35,950 ‫and this is a failure. 154 00:05:35,950 --> 00:05:36,783 ‫Here's the "ErrorCode" 155 00:05:36,783 --> 00:05:39,820 ‫and here is the error "Stephane the teacher wasn't found 156 00:05:39,820 --> 00:05:42,290 ‫in the output of the Lambda function". 157 00:05:42,290 --> 00:05:46,452 ‫So let's save this and call it MyStateMachine, 158 00:05:46,452 --> 00:05:48,300 ‫we're going to MyStateMachineDemo, 159 00:05:48,300 --> 00:05:49,844 ‫we're going to create a new role 160 00:05:49,844 --> 00:05:52,303 ‫and creates the state machine. 161 00:05:53,480 --> 00:05:55,450 ‫Okay. So it has been created. 162 00:05:55,450 --> 00:05:57,690 ‫Now we need to start an execution. 163 00:05:57,690 --> 00:05:59,690 ‫So to do so I'm going to do "who": 164 00:05:59,690 --> 00:06:02,010 ‫and then we're going to enter "Stephane" 165 00:06:02,010 --> 00:06:04,453 ‫and open this in a new browser tab. 166 00:06:06,090 --> 00:06:06,923 ‫Now this is already done 167 00:06:06,923 --> 00:06:08,960 ‫so this was very quick and it went 168 00:06:08,960 --> 00:06:10,460 ‫it Invoke the Lambda function. 169 00:06:10,460 --> 00:06:12,490 ‫So the step inputs was "who": "Stephane" 170 00:06:12,490 --> 00:06:14,790 ‫and the step output was "Hello, Stephane!" 171 00:06:14,790 --> 00:06:17,230 ‫which is the output of the Lambda function. 172 00:06:17,230 --> 00:06:18,550 ‫It went into a choice state. 173 00:06:18,550 --> 00:06:20,020 ‫The input was "Hello, Stephane!". 174 00:06:20,020 --> 00:06:22,231 ‫It's so that's Stephane, wasn't in the inputs 175 00:06:22,231 --> 00:06:25,030 ‫so therefore he went to the is Teacher branch 176 00:06:25,030 --> 00:06:26,610 ‫and then he went to the End. 177 00:06:26,610 --> 00:06:27,443 ‫So this is great. 178 00:06:27,443 --> 00:06:29,600 ‫And we can, again, look at the event history 179 00:06:29,600 --> 00:06:31,110 ‫to look at the CloudWatch Logs, 180 00:06:31,110 --> 00:06:32,210 ‫the Lambda function itself 181 00:06:32,210 --> 00:06:36,040 ‫and why things went the way it worked, which is great. 182 00:06:36,040 --> 00:06:40,460 ‫Okay. The second execution is to say John 183 00:06:40,460 --> 00:06:41,600 ‫and John is not a teacher. 184 00:06:41,600 --> 00:06:43,340 ‫I'm sorry if you are watching this video 185 00:06:43,340 --> 00:06:44,500 ‫and named John. 186 00:06:44,500 --> 00:06:47,094 ‫So (laughs) let's open this in a new tab 187 00:06:47,094 --> 00:06:48,714 ‫and start the execution, 188 00:06:48,714 --> 00:06:50,100 ‫and it failed. 189 00:06:50,100 --> 00:06:52,630 ‫So why? So the Lambda function was invoked 190 00:06:52,630 --> 00:06:54,612 ‫The step input was "who": "John" 191 00:06:54,612 --> 00:06:57,870 ‫and the step output was "Hello, John!". 192 00:06:57,870 --> 00:07:00,470 ‫And then directly we went into a Choice States 193 00:07:00,470 --> 00:07:02,220 ‫and it figured out that John is not Stephane, 194 00:07:02,220 --> 00:07:03,986 ‫so it went into Not Teacher. 195 00:07:03,986 --> 00:07:07,200 ‫And then it threw an exception saying Errorcode 196 00:07:07,200 --> 00:07:09,360 ‫and the causes that "Stephane the teacher wasn't found 197 00:07:09,360 --> 00:07:12,330 ‫in the outputs of the Lambda function." 198 00:07:12,330 --> 00:07:13,610 ‫So it's pretty cool, right? 199 00:07:13,610 --> 00:07:15,442 ‫And if we wanted to do a new execution 200 00:07:15,442 --> 00:07:19,128 ‫and actually this time, I'm going to say "not a key": 201 00:07:19,128 --> 00:07:20,660 ‫and then "blabla", 202 00:07:20,660 --> 00:07:23,070 ‫so we're going to pass some bad arguments 203 00:07:23,070 --> 00:07:26,200 ‫to our Lambda function and start the execution. 204 00:07:26,200 --> 00:07:29,027 ‫Again, it has failed so in this example, 205 00:07:29,027 --> 00:07:30,250 ‫it says, "Hello, undefined!" 206 00:07:30,250 --> 00:07:31,910 ‫so the Lambda function did not fail actually, 207 00:07:31,910 --> 00:07:33,010 ‫it just returned undefined, 208 00:07:33,010 --> 00:07:35,062 ‫but if the Lambda function had failed, 209 00:07:35,062 --> 00:07:37,890 ‫we would see a failure right here in our graph, 210 00:07:37,890 --> 00:07:39,070 ‫which would be great. 211 00:07:39,070 --> 00:07:40,820 ‫So really it is quite interesting 212 00:07:40,820 --> 00:07:42,621 ‫because step functions encapsulate the whole logic 213 00:07:42,621 --> 00:07:44,222 ‫of our decision tree 214 00:07:44,222 --> 00:07:46,250 ‫and allow us to define workflows. 215 00:07:46,250 --> 00:07:48,850 ‫And this is what I want to show you with this hands-on. 216 00:07:48,850 --> 00:07:49,690 ‫So hope you liked it, 217 00:07:49,690 --> 00:07:51,640 ‫and I will see you in the next lecture.