1
00:00:00,270 --> 00:00:06,030
In this video, we are going to create step function state machines using a lambda.

2
00:00:06,060 --> 00:00:12,330
We will create a workflow using step functions in order to invoke a lambda functions.

3
00:00:12,720 --> 00:00:19,320
As you remember, that step functions and V called step function workflow is a state machine which including

4
00:00:19,350 --> 00:00:21,660
the series of event driven steps.

5
00:00:21,690 --> 00:00:27,960
Each step in workflow is called state, a task state as represent a single work of unit.

6
00:00:27,990 --> 00:00:32,430
That could be an services like a lambda and so on.

7
00:00:32,550 --> 00:00:39,360
So this lambda is well-suited for task states because lambda functions are serverless and easy to write.

8
00:00:39,390 --> 00:00:46,530
We can write code in the mission console or favorite editor a place, handle the details or providing

9
00:00:46,530 --> 00:00:49,310
competing for our function and running it.

10
00:00:49,320 --> 00:00:55,160
So for that purpose I'm going to use ADA is lambda into step functions for that purpose.

11
00:00:55,170 --> 00:01:01,200
If you open the pseudocode I have create a separate section and lecture folder and we have a command

12
00:01:01,230 --> 00:01:04,170
text file that we are going to follow one by one.

13
00:01:04,170 --> 00:01:07,890
We will basically create step function state machine using a lambda.

14
00:01:07,980 --> 00:01:15,600
Here you can see the our state machine, we will have start step and after that we will choice state.

15
00:01:15,600 --> 00:01:17,790
We choice state will be the order type.

16
00:01:17,790 --> 00:01:23,790
And if the order type is a purchase, we will run, execute, purchase, function, lambda function.

17
00:01:24,090 --> 00:01:26,130
As if the order type is a refund.

18
00:01:26,130 --> 00:01:31,140
We will run the refund lambda function and we will enter our state mission.

19
00:01:31,140 --> 00:01:36,810
So this is the our state machine step function workflow that we are going to build on step function

20
00:01:36,810 --> 00:01:38,910
workflow workflow designer.

21
00:01:38,940 --> 00:01:46,020
And after that you can see the expected payloads when we execute start and execution, these workflow

22
00:01:46,050 --> 00:01:47,760
with the order type is a purchase.

23
00:01:47,760 --> 00:01:54,140
We expect that our purchase function lambda function will be invoked, but if we send order type is

24
00:01:54,140 --> 00:01:58,290
a different this time, we will expect that different function will be invocation.

25
00:01:58,290 --> 00:01:59,760
Have an on lambda functions.

26
00:01:59,910 --> 00:02:00,810
Okay, very good.

27
00:02:00,840 --> 00:02:03,030
Here you can see the implementation of the code.

28
00:02:03,060 --> 00:02:05,280
Now we have first step.

29
00:02:05,280 --> 00:02:10,220
Basically we will create a lambda function test, a lambda function and create a state machine with

30
00:02:10,230 --> 00:02:11,490
including these steps.

31
00:02:11,490 --> 00:02:17,520
And after that we will start a need to execution to see how invokes step function to our lambda function.

32
00:02:17,520 --> 00:02:18,660
Let's get started.

33
00:02:18,660 --> 00:02:25,230
Please open the machine console and see that we will start with creating lambda functions.

34
00:02:25,260 --> 00:02:30,480
Go to lambda functions and I'm going to create a purchase lambda function.

35
00:02:30,600 --> 00:02:32,150
Let me create a new function guys.

36
00:02:32,160 --> 00:02:36,930
Name is a purchase function as it is for all complications create functions.

37
00:02:37,110 --> 00:02:38,700
Now I'm going to.

38
00:02:39,550 --> 00:02:43,340
Create purchase function and after that I will develop this function.

39
00:02:43,360 --> 00:02:45,130
You can copy codes from here.

40
00:02:45,130 --> 00:02:50,290
Basically, we will log the incoming event and after that we will log the order type.

41
00:02:50,290 --> 00:02:56,980
And after that we basically get back to this post the success code and also give the Helloworld purchase

42
00:02:56,980 --> 00:02:59,050
function and specify the order type.

43
00:02:59,080 --> 00:03:01,290
This is very basic implementation.

44
00:03:01,300 --> 00:03:08,380
If you come to purchase function and go to code window, you can see in here basically I will copy and

45
00:03:08,380 --> 00:03:13,390
paste from our common text to our will Islam the code window click to deploy button.

46
00:03:13,750 --> 00:03:19,120
So here you can see the implementation and it's the same way if you go back to functions and now create

47
00:03:19,120 --> 00:03:25,090
a new function, which name is different function and leave as it is for all complications.

48
00:03:25,090 --> 00:03:27,520
Create new function which is the different function.

49
00:03:28,170 --> 00:03:31,260
So again if you go back to.

50
00:03:32,630 --> 00:03:37,460
Our common text, you can copy paste from the exact same codes for the default function.

51
00:03:37,460 --> 00:03:41,090
I have only changed the text of the body of the response.

52
00:03:41,720 --> 00:03:45,230
If you come to code window and come to index choose.

53
00:03:46,150 --> 00:03:48,280
The code and deploy this function.

54
00:03:48,290 --> 00:03:54,890
Now we are ready for the create our state machine, but before that we can start with the test or one

55
00:03:54,890 --> 00:03:55,520
of the functions.

56
00:03:55,520 --> 00:04:00,670
For example, the different function come to test section, create a new test event so you can give

57
00:04:00,680 --> 00:04:01,460
random name.

58
00:04:01,460 --> 00:04:02,270
If you scroll down.

59
00:04:02,270 --> 00:04:03,530
I'm going to edit the event.

60
00:04:03,530 --> 00:04:04,370
Jason File.

61
00:04:04,400 --> 00:04:11,270
If you provide the event JSON file as a older type, equal the default and click this save and test

62
00:04:11,270 --> 00:04:12,490
this execution.

63
00:04:12,500 --> 00:04:17,900
You can see that we basically return back to equals 200 and the button information include help from

64
00:04:17,900 --> 00:04:20,510
refund function and all the type is refund.

65
00:04:20,510 --> 00:04:23,510
So this is the basic execution of our lambda function.

66
00:04:23,510 --> 00:04:27,530
I'm not going to go deeply, so it is very basic implementation.

67
00:04:27,530 --> 00:04:30,290
So now it is time to create our state functions.

68
00:04:30,290 --> 00:04:36,290
If you go back to state functions and open the state machines, we can create a new state machine and

69
00:04:36,290 --> 00:04:41,870
these will be the our standard state machine and I'm going to click the next button.

70
00:04:41,870 --> 00:04:46,640
So now we can design our workflow using the design workflow studio.

71
00:04:46,670 --> 00:04:50,960
You can go back to command text and see and remember our set mission.

72
00:04:50,960 --> 00:04:55,700
This will be start and after that we will chose that and locate the order type.

73
00:04:55,700 --> 00:04:59,540
So if you come back here and open the flow and come to choice statement.

74
00:04:59,540 --> 00:05:04,280
So in the choice statement we will require the add two rules.

75
00:05:04,280 --> 00:05:07,370
The first rule is edit to rule and add the condition.

76
00:05:07,370 --> 00:05:10,160
So basically we will expect the order type.

77
00:05:10,160 --> 00:05:17,480
So that's why I'm going to add a dollar sign and that an order type is equal to now we can select the

78
00:05:17,480 --> 00:05:27,620
string constant and basically you can come here if there's a purchase we can redirect to purchase statement.

79
00:05:27,650 --> 00:05:33,230
Now I'm going to add a new choice, which is the rule to add the condition.

80
00:05:33,230 --> 00:05:37,550
And this time again, I'm going to put a dollar sign and order type.

81
00:05:37,550 --> 00:05:47,210
Let me copy the order type and give the operator is equal to and string constant and you can say that

82
00:05:47,780 --> 00:05:48,680
you want.

83
00:05:49,320 --> 00:05:52,500
So if you remove the default rule.

84
00:05:53,530 --> 00:05:55,990
You can see that we have two choices.

85
00:05:56,140 --> 00:05:57,490
One choice is purchase.

86
00:05:57,520 --> 00:06:00,610
Another choice is the refund or according to order type.

87
00:06:00,640 --> 00:06:06,490
We can add a lambda function and set invoke lambda function for that choice.

88
00:06:06,490 --> 00:06:13,690
So if you come back to actions, so come to a lambda, you can drag and drop the lambda under the order

89
00:06:13,690 --> 00:06:15,100
type equal purchase.

90
00:06:15,100 --> 00:06:23,290
And if you come to this window you can basically give the name state name is a purchase lambda invoke

91
00:06:23,290 --> 00:06:30,850
and this will be purchased lambda invoke and the other one a cool the if you drag and drop here and

92
00:06:31,120 --> 00:06:34,500
the other one should be the default lambda invoke.

93
00:06:34,510 --> 00:06:41,620
So basically we will attach the lambda functions according to order type choice statement, purchase

94
00:06:41,620 --> 00:06:42,370
and default.

95
00:06:42,370 --> 00:06:45,340
So now let's configure that lambda functions.

96
00:06:45,370 --> 00:06:50,500
Please select the purchase lambda function and if you scroll down, make sure that integration type

97
00:06:50,500 --> 00:06:51,700
should be optimized.

98
00:06:51,700 --> 00:06:54,400
And if you scroll down, you can see the payload.

99
00:06:54,430 --> 00:06:57,940
Payload should be the use state input as payload.

100
00:06:57,940 --> 00:07:01,450
So these are the default configuration that I am not going to change.

101
00:07:01,450 --> 00:07:05,050
So we will also make sure that the configuration is the same.

102
00:07:05,080 --> 00:07:09,640
And if you look at the default lambda policy, you find lambda in work state.

103
00:07:09,640 --> 00:07:15,850
If you scroll down, you can see the integration type optimized and we can use state input as a payload.

104
00:07:15,850 --> 00:07:22,780
By this way, the incoming input of the our state, this will be invoke lambda function and this will

105
00:07:22,780 --> 00:07:27,370
be one of the lambda functions and return back to us a lambda execution.

106
00:07:27,610 --> 00:07:28,930
Okay, very good.

107
00:07:28,930 --> 00:07:33,760
So now this function is invoke and after that goes to the end of the workflow.

108
00:07:33,790 --> 00:07:39,790
So if you see the latest architecture, we have only one choice statement and we will invoke two different

109
00:07:39,790 --> 00:07:42,310
lambda functions according to incoming order type.

110
00:07:42,550 --> 00:07:43,210
Okay.

111
00:07:43,240 --> 00:07:48,910
If you click the next button, you can see that we have two error found in our workflow.

112
00:07:48,940 --> 00:07:54,790
When we configure our states as the lambda function, we forgot the very important configuration, which

113
00:07:54,790 --> 00:07:56,020
is the lambda function name.

114
00:07:56,020 --> 00:08:00,130
Please select the purchase Lambda invoke state and go to configuration.

115
00:08:00,130 --> 00:08:00,820
Scroll down.

116
00:08:00,820 --> 00:08:02,830
You can see the function name is in here.

117
00:08:02,830 --> 00:08:07,930
We should provide the function name and the function name should be the our purchase function lambda

118
00:08:07,930 --> 00:08:08,590
function.

119
00:08:09,280 --> 00:08:16,660
Okay after that, please select the refund lambda invoke state and select the function name is a different

120
00:08:16,660 --> 00:08:17,320
function.

121
00:08:17,500 --> 00:08:23,260
Okay, now we can click the next button and see that we will see the generated code from the Amazon

122
00:08:23,260 --> 00:08:24,280
State language.

123
00:08:24,280 --> 00:08:29,860
And this is the our workflow and this is the codes that generated from the Amazon State language and

124
00:08:29,860 --> 00:08:31,420
is adjacent format.

125
00:08:31,450 --> 00:08:35,790
You can review and see the all lambda invocation and configuration in here.

126
00:08:35,890 --> 00:08:41,590
After that, if you scroll down and click the next button, we basically create a state machine, which

127
00:08:41,590 --> 00:08:44,320
will be the or other type.

128
00:08:45,910 --> 00:08:46,720
Tape machine.

129
00:08:48,220 --> 00:08:53,760
If you scroll down this is the create any rule and I don't change any settings in here and click the

130
00:08:53,760 --> 00:08:55,080
create state machine.

131
00:08:55,800 --> 00:09:00,870
So this is going to create our new state mission, which is the older type state machine, which using

132
00:09:00,900 --> 00:09:02,920
the lambda as a state.

133
00:09:02,940 --> 00:09:07,070
So now we can start executions and see what's happened.

134
00:09:07,080 --> 00:09:13,350
So click the start execution button and now I'm going to set a order type as a purchase and see what's

135
00:09:13,350 --> 00:09:13,950
happened.

136
00:09:13,980 --> 00:09:19,910
If you copy paste this JSON, give the order type as a purchase and start execution.

137
00:09:19,920 --> 00:09:21,510
We will see what's going on.

138
00:09:21,540 --> 00:09:26,940
As you can see that this comes from the choice step in the choice step input is order type purchase

139
00:09:26,940 --> 00:09:34,670
and according to choice step it is invoke to our purchase lambda lock step and this step run the our

140
00:09:34,740 --> 00:09:35,460
lambda function.

141
00:09:35,460 --> 00:09:41,280
And if you come to step input and output, you can see that this is the execution response from the

142
00:09:41,280 --> 00:09:42,280
lambda function.

143
00:09:42,300 --> 00:09:48,570
You can see this go to 100 success response and the body is from purchase function and order type is

144
00:09:48,570 --> 00:09:49,430
purchase.

145
00:09:49,440 --> 00:09:54,210
So we have successful trigger or lambda function from the step functions.

146
00:09:54,210 --> 00:09:57,690
So you can come here and see the execution event history.

147
00:09:58,590 --> 00:10:04,050
Basically, we will start from a choice state and according to Choice State, we will redirect to our

148
00:10:04,050 --> 00:10:09,240
workflow or purchase lambda IMO State and this state invoked to our lambda functions.

149
00:10:09,240 --> 00:10:11,100
You can see the task scheduled.

150
00:10:11,130 --> 00:10:14,300
This is invoked the lambda function and you can see the cloud logs.

151
00:10:14,310 --> 00:10:20,370
If you click the cloud stocks, this will be open the cloud watch of to purchase function and you can

152
00:10:20,370 --> 00:10:27,690
see the exact same log stream and here you can see the our incoming order type and these will be invoke

153
00:10:27,690 --> 00:10:32,400
our lambda function and this is response back to our step functions and set functions.

154
00:10:32,400 --> 00:10:37,920
So in here the output of the state is the response from the lambda function.

155
00:10:37,920 --> 00:10:38,550
Very good.

156
00:10:38,550 --> 00:10:44,790
We have successfully executed our purchase function and please start an execution.

157
00:10:44,790 --> 00:10:51,300
This time I'm going to set a order type and refund and start execution and you can scroll down and see

158
00:10:51,300 --> 00:10:55,350
that everything is goes work for the lambda function.

159
00:10:55,350 --> 00:10:56,700
We have a choice statement.

160
00:10:56,730 --> 00:11:00,510
We can see the input and output as order type is in font.

161
00:11:00,540 --> 00:11:06,150
If you click the our default lambda invoke statement, the input parameters is here and step outward.

162
00:11:06,150 --> 00:11:12,030
It comes from the Lambda Invocation response, which is the halo from the default message comes to our

163
00:11:12,030 --> 00:11:13,050
lambda function.

164
00:11:13,050 --> 00:11:19,770
If you scroll down, you can see the exact same execution event history that we execute our lambda function,

165
00:11:19,770 --> 00:11:21,480
which is the default lambda function.

166
00:11:21,480 --> 00:11:23,270
You can check from the cloud watch logs.

167
00:11:23,290 --> 00:11:25,410
This time it is the font function.

168
00:11:25,440 --> 00:11:28,050
Open the latest log stream and see that.

169
00:11:28,050 --> 00:11:33,920
Verify that to order type invoke from the order type from the is equal default.

170
00:11:34,020 --> 00:11:35,250
Okay, very good.

171
00:11:35,250 --> 00:11:43,560
We have verified that we are created or state mission which including lambda function and we solve this

172
00:11:43,560 --> 00:11:45,650
lambda invocation from the cloud logs.

173
00:11:45,690 --> 00:11:48,150
Now we are going to create our resources.

174
00:11:48,150 --> 00:11:49,110
It is very important.

175
00:11:49,110 --> 00:11:52,660
Please don't forget to clear your resources for that source.

176
00:11:52,710 --> 00:11:54,450
Please open the lambda functions.

177
00:11:54,450 --> 00:11:59,100
Select all function action delete and confirm with the delete message.

178
00:12:00,650 --> 00:12:03,440
And make sure that you have the date, all of the functions.

179
00:12:03,440 --> 00:12:11,330
And if you come to state machines or to state functions, please select 1.1 and delete all state missions

180
00:12:11,330 --> 00:12:13,910
into your state functions.

181
00:12:14,570 --> 00:12:15,710
Click to Delete Button.

182
00:12:15,740 --> 00:12:16,940
Delete State Mission.

183
00:12:18,830 --> 00:12:19,310
It is.

184
00:12:19,310 --> 00:12:23,450
So this is dilating and now I am going to delete one by one.

185
00:12:24,820 --> 00:12:25,990
It is also deleting.

186
00:12:25,990 --> 00:12:26,800
And now.

187
00:12:27,770 --> 00:12:32,860
I had delete all step functions state machines into set function.

188
00:12:32,870 --> 00:12:38,540
If you refresh this page, make sure that you have successfully delete all state machines on the state

189
00:12:38,540 --> 00:12:39,110
functions.
