‫So let's do an introduction to Step Functions.
‫And we are going to go into the step function,
‫get started and there is a Hello World example right away
‫to get up and running,
‫so there is a very complex flow,
‫so pass Hello World example, this is a choice state.
‫Yes. Wait three seconds.
‫Hello and World, Hello World and the End.
‫So all these things really,
‫look at this json document
‫and generates a graph, a workflow from the right-hand side.
‫And so this is for example, IsHelloWorldExample,
‫false or true,
‫so I will show you how this works in a second,
‫so the idea is that's based on the input we have
‫we should go into one branch
‫on the left-hand side or run branch on the right-hand side.
‫So let's go ahead and creates this workflow.
‫This is the name, HelloWorld.
‫We're going to create a new role for this
‫and then we will creates the state machine.
‫So to run this workflow
‫we have to specify "IsHelloWorldExample": true or false.
‫So in this example we're going to say true.
‫We're going to open a new browser window,
‫and this is going to do an execution
‫of a workflow with this inputs.
‫And as you can see, the execution is happening right now.
‫We went into the Hello World example,
‫it detected it.
‫Yes it was Hello World example,
‫so we went to the yes, it waited three seconds,
‫it ran this parallel states,
‫give this the Hello World outputs,
‫and if we look at the step outputs,
‫we get this operated
‫which is not a very interesting outputs
‫but still shows us that's the whole execution
‫happened like this.
‫And all the steps of the Execution history
‫happened in here,
‫and we can debug them one by one
‫by looking at what was the inputs
‫and what was the outputs
‫which is really, really handy.
‫So this shows you already like a very good,
‫powerful way of doing step functions
‫using some very simple steps,
‫but these steps could be Lambda functions.
‫So if you want to do a new execution
‫and this time we say
‫"IsHelloWorldExample": false
‫and we open it in a new tab again.
‫This time we're going to go into a failed states
‫because well, IsHelloWorldExample was false
‫and so we went into the No states and there was an End.
‫And again, we can debug this directly
‫from the Execution event history
‫which is really, really handy.
‫So we're going to go ahead
‫and create our own state machines.
‫So we have a state machine here.
‫This is the HelloWorld one
‫but I'm going to author our own state machine, okay?
‫Using a standard workflow
‫and we have to pass on some data.
‫So to do so we're going to first create a Lambda function
‫that will be invoked by our step functions.
‫So I'm gonna go into Lambda,
‫open this in a new tab
‫and create our first functions.
‫So I'm going to create a function in here.
‫We will call it this function.
‫We'll call it HelloFunction.
‫It's going to be node.js 14.x
‫and then it will creates this function.
‫(computer mouse clicks)
‫Now in terms of the code that we're going to use
‫for this function, we have to go into step functions,
‫the code directory, hello-world,
‫and then function.js and copy and paste this.
‫So if we go into the code itself in here for index.js,
‫just paste it and this is a very simple function
‫is going to say "Hello"
‫plus the content of the who key
‫in the event documents.
‫So we can test it,
‫by passing in a new test event.
‫I'll call it Hello
‫and the key has to be "who":
‫and the value is going to be "John".
‫So if we go in here
‫and pass this test events to our Lambda function
‫and now test it is going to say "\"Hello from Lambda!\""
‫and somehow it was not deployed
‫so I need to save my function
‫by doing ctrl+S and then test it again.
‫(computer mouse clicks)
‫So I forgot to click on deploy
‫so I just clicked on deploy
‫and we're going to test it again
‫and this time it says, "Hello, John!".
‫So very simple.
‫Based on the input we have,
‫we have the response "Hello",
‫and the who we just passed.
‫Okay, great.
‫So this Lambda function
‫we'll be using it for our step functions.
‫So we need to find the Lambda function ARN
‫which is right here, and we'll be using it later on.
‫Okay. So next we go,
‫we have our states machine right here
‫and so I'm going through my code.
‫I opened the state-machine.json,
‫copy this entirely and paste it here.
‫So what we're trying to do
‫and I'm going to refresh this to display it.
‫So what we're going to do is to first
‫Invoke Lambda function
‫and then evaluate the result of this Lambda function
‫and determine if I am a teacher or I'm not a teacher
‫and then go to the End results.
‫Okay. Now we have to specify a few things.
‫Number one, for the "Invoke Lambda function"
‫which is the "StartAt": states,
‫we're going to specify the name of the resource.
‫So we go into the Lambda function,
‫we find the ARN right here, and we paste it.
‫(computer mouse clicks)
‫Okay. Next we have a thing called "InputPath":,
‫so this is what gets passed to this Lambda function.
‫And by default at the input of my workflow
‫will be passing by them the function,
‫because this is the first Task.
‫But anyway, I'm still having it here
‫just to make it very obvious.
‫So once this is done, the "Next": state is a "Choice State",
‫so we're going to go into this "Choice State": right here
‫which is a "Choice"
‫and it's going to evaluate one choice.
‫It's going to look at the outputs of the previous states
‫so dollar sign is the output of the previous states.
‫And it's going to look at if there is "Stephane" in it,
‫and if there is "Stephane" in it,
‫then you should go to "Is Teacher".
‫And "Is Teacher": is of "Type": "Pass"
‫and the "Result": is "Woohoo!"
‫and the "End": is true.
‫But if it's not,
‫then the "Default": is to go to "Not Teacher".
‫So were gonna go to the "Not Teacher" states
‫and this is a failure.
‫Here's the "ErrorCode"
‫and here is the error "Stephane the teacher wasn't found
‫in the output of the Lambda function".
‫So let's save this and call it MyStateMachine,
‫we're going to MyStateMachineDemo,
‫we're going to create a new role
‫and creates the state machine.
‫Okay. So it has been created.
‫Now we need to start an execution.
‫So to do so I'm going to do "who":
‫and then we're going to enter "Stephane"
‫and open this in a new browser tab.
‫Now this is already done
‫so this was very quick and it went
‫it Invoke the Lambda function.
‫So the step inputs was "who": "Stephane"
‫and the step output was "Hello, Stephane!"
‫which is the output of the Lambda function.
‫It went into a choice state.
‫The input was "Hello, Stephane!".
‫It's so that's Stephane, wasn't in the inputs
‫so therefore he went to the is Teacher branch
‫and then he went to the End.
‫So this is great.
‫And we can, again, look at the event history
‫to look at the CloudWatch Logs,
‫the Lambda function itself
‫and why things went the way it worked, which is great.
‫Okay. The second execution is to say John
‫and John is not a teacher.
‫I'm sorry if you are watching this video
‫and named John.
‫So (laughs) let's open this in a new tab
‫and start the execution,
‫and it failed.
‫So why? So the Lambda function was invoked
‫The step input was "who": "John"
‫and the step output was "Hello, John!".
‫And then directly we went into a Choice States
‫and it figured out that John is not Stephane,
‫so it went into Not Teacher.
‫And then it threw an exception saying Errorcode
‫and the causes that "Stephane the teacher wasn't found
‫in the outputs of the Lambda function."
‫So it's pretty cool, right?
‫And if we wanted to do a new execution
‫and actually this time, I'm going to say "not a key":
‫and then "blabla",
‫so we're going to pass some bad arguments
‫to our Lambda function and start the execution.
‫Again, it has failed so in this example,
‫it says, "Hello, undefined!"
‫so the Lambda function did not fail actually,
‫it just returned undefined,
‫but if the Lambda function had failed,
‫we would see a failure right here in our graph,
‫which would be great.
‫So really it is quite interesting
‫because step functions encapsulate the whole logic
‫of our decision tree
‫and allow us to define workflows.
‫And this is what I want to show you with this hands-on.
‫So hope you liked it,
‫and I will see you in the next lecture.