(bright music)
- Hi, I'm Derrick.
A Cloud Support Engineer
here at the AWS office in Bangalore, India.
Sometimes, customers ask me
how to invoke AWS Lambda
whenever execution status changes
in AWS Step Functions.
I tell them the steps are similar.
If you want to trigger Lambda
from event changes of different services,
such as RDS, EC2, and so on.
Let me walk you through the process.
Here are some things to keep in mind for this process.
You will need an IAM role
which grants a Step Function state machine
permission to perform any required actions.
The complete setup is divided
into four different parts.
Create a state machine,
create a sample target Lambda,
configure a CloudWatch rule
which would trigger the function
based on execution status change
of the state machine, test it.
First, we'll create a state machine in Step Functions.
Login to the AWS Management Console.
Navigate to the Step Functions Console.
Choose Create state machine
on the right hand side of the console.
Under define state machine section,
select the Templates tab.
Choose a template.
For the purposes of this video,
I'll use Hello world.
In the detail section,
enter the name of the state machine.
I'll leave it as default for now.
Select Next button on the bottom right of the console.
Under Configure settings section,
you will see two tabs for IAM role for executions.
Here, you can either choose
to create a new IAM role
by selecting Create an IAM role for me tab
or use an existing role
by selecting Choose an existing IAM role tab.
For this example, I'll use an existing role
which grants permissions
for the state machine to invoke Lambda.
Finally, choose Create a state machine
on the bottom right corner
and your state machine is ready.
Now let's create a sample Lambda function.
Navigate to the Lambda console page
and choose Create function.
Choose a blueprint.
For the purposes of this video,
I'll choose the hello-world-python blueprint.
Note that you can also alter the function from scratch.
Select Configure at the bottom right.
Under Basic information section,
enter the function's name.
Select or create an appropriate IAM role
that gives the Lambda necessary permissions
to execute the function.
For this example, I'll create a new role
with basic Lambda permissions.
The above role grants the permissions
to publish logs to CloudWatch only.
Choose Create function also at the bottom right.
This creates your Lambda function.
When the function is created,
edit the generated code in line
under Lambda's code so that,
when it's executed,
the function just prints the event it receives.
Review and save your function.
With this your Lambda function is ready.
And finally let's create the CloudWatch rule,
which links the function and the state machine.
Navigate to the CloudWatch console page,
choose Rules under Events tab,
and then go for Create rule.
In the Event Pattern, choose Service Name
as Step Functions and then follow up
by choosing the Event Type
as Step Functions Execution Status Change.
You can customize a trigger for specific statuses,
state machines, or execution errands.
For this example, I'll select Any for all options.
Note, we can also choose AWS API call
using CloudTrail option as seen
from the drop down in the Event Types.
You can customize this according to your use case.
For example, you might wanna trigger Lambda
only for a specific event,
such as start execution.
I will skip this step.
Now, let's add the trigger.
Select Add Target on the top right.
From the drop down, select Lambda function.
From the second drop down,
find and choose your function.
Select Configure details on the bottom right.
Type a name and description.
I'm gonna type Test_rule.
And in description
I'll mention This is my test rule.
Check State Enabled to enable your rule.
Then choose Create rule.
And with this, your setup is complete.
Now let's test it.
Go back to your previously created state machine,
choose the machine,
and then start an execution
by choosing the Start execution button
at the bottom right.
Under the new execution section,
you can enter a name for the execution
and provide an input to the state machine.
I'll be leaving this as it is.
Select Start execution button
at the bottom right of the page.
Now let's go to a Lambda function log
to see whether it was invoked or not.
Navigate to CloudWatch console,
then choose Logs.
Select and open the log stream
generated by the Lambda function.
You will see the event details as shown.
If your log stream is not yet generated,
just wait for one or two minutes.
The above mentioned setup
can also be used for services
other than Step Functions.
Just change the service in Event Pattern
when creating the rule.
Thanks for watching and happy Cloud computing.
From all of us here at AWS.
(bright music)