AWS Step Functions is a service for workflowizing microservice components.
With Step Functions, you can control various actions depending on the state. The states are as follows.
Status | control |
---|---|
Task | Do some work on the state machine |
Choice | Choose between execution options |
Fail or Succeed | Stop execution on failure or success |
Pass | Simply pass the input to the output, or output some fixed data |
Wait | Fixed time or specified time/Provide delay until date |
Parallel | Start a parallel branch of execution |
Map | Dynamically iterate steps |
The following services can be specified for the Task of Step Functions.
Step Functions has a very wide range, and even if you look at Step Functions + Lambda alone, it is quite wide. Since the content of the post can be huge, we will target the following content for this post. I would like to cover the parts such as passing parameters, changing tasks depending on conditions, executing other services, etc. next time.
--Create a Lambda function (SAM) --Deploy Lambda functions (SAM) --Configure a state machine --Call a Lambda function (only) --Deploy a state machine (SAM) --Run the state machine (Management Console)
SAM For SAM, see below.
-[AWS] Serverless Application Model (SAM) Basic Summary -[AWS] Create API Gateway + Lambda + DynamoDB sample with Serverless Application Model (SAM)
First, create a Hello World project with Runtime in Python (3.8).
$ sam init --runtime=python3.8
Which template source would you like to use?
1 - AWS Quick Start Templates
2 - Custom Template Location
Choice: 1
Project name [sam-app]:
Cloning app templates from https://github.com/awslabs/aws-sam-cli-app-templates.git
AWS quick start application templates:
1 - Hello World Example
2 - EventBridge Hello World
3 - EventBridge App from scratch (100+ Event Schemas)
4 - Step Functions Sample App (Stock Trader)
5 - Elastic File System Sample App
Template selection: 1
-----------------------
Generating application:
-----------------------
Name: sam-app
Runtime: python3.8
Dependency Manager: pip
Application Template: hello-world
Output Directory: .
Next steps can be found in the README file at ./sam-app/README.md
This time, we don't need API Gateway, so edit template.yml
as follows.
ʻAWS :: Serverless :: Function does not need to define ʻEvents
.
Also, ʻOutputs` itself is unnecessary.
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Once saved, run sam build
and sam deploy --guided
to deploy.
First, let's make the necessary preparations to configure the state machine.
It adds a role definition so that it can be called from the Step Function as a setting for the Lambda function.
Templates created in Hello World assign a default role to your Lambda function, so you need to explicitly create and assign it.
Now let's play with template.yml
.
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Role: !GetAtt HelloWorldFunctionRole.Arn
HelloWorldFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSStepFunctionsReadOnlyAccess
Next, create the state machine definition in Json format. First, create a directory to store the files and create a new file there.
$ mkdir step_functions
$ touch step_functions/state_machine.json
Next, define the state machine. The definition below is a very simple definition that just calls the Lambda function of hello_world and terminates it, with no conditions or parameters.
step_functions/state_machine.json
{
"StartAt": "hello world",
"States": {
"hello world": {
"Type": "Task",
"Resource": "${HelloWorldFunction}",
"End": true
}
}
}
Finally, let's add a state machine definition to template.yml
.
template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
sam-app
Sample SAM Template for sam-app
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.8
Role: !GetAtt HelloWorldFunctionRole.Arn
HelloWorldFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AWSStepFunctionsReadOnlyAccess
StateMachine:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: step_functions/state_machine.json
DefinitionSubstitutions:
HelloWorldFunction: !GetAtt HelloWorldFunction.Arn
Role: !GetAtt StateMachineRole.Arn
StateMachineRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- states.amazonaws.com
Action:
- sts:AssumeRole
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
Then run to sam build
, sam deploy --guided
.
First, let's check the current status.
In the management console, select Step Functions
.
You can see that the state machine is deployed.
Let's take a look at the contents of the state machine here. Looking at the contents of the "definition", I think it looks like the following.
You can see that hello_world
is called.
In the contents of Json of "Definition", you can see that the Resource
part has been replaced with the ARN of the Lambda function.
Now let's run the state machine. Press the Start Execution button on the Run tab to run the state machine.
This time, since the parameters etc. are not received on the Lambda side, it is okay to execute the input as it is.
Execution is completed immediately and you can check the event history.
If you check the linked Cloud Watch, you can be sure that the Lambda function is being called.
This time, since it is the basic operation in the basics, I think it is important to firmly suppress this point first. Next time, I would like to talk about parameter settings, how to call other than Lambda from Task, and conditional branching.
-[AWS] Play with Step Functions (SAM + Lambda) Part.2 (Parameter Edition)
https://github.com/hito-psv/sam-demo-004
Recommended Posts