[PYTHON] [AWS] Play with Step Functions (SAM + Lambda) Part.1 (Basic)

What is Step Functions?

AWS Step Functions is a service for workflowizing microservice components.

Status

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

Services available in Task

The following services can be specified for the Task of Step Functions.

This time's target

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)

Premise

SAM For SAM, see below.

-[AWS] Serverless Application Model (SAM) Basic Summary -[AWS] Create API Gateway + Lambda + DynamoDB sample with Serverless Application Model (SAM)

Prepare a Lambda function

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.

Configure a state machine with Step Functions

Preparation

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

Add state machine definition

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
    }
  }
}

Add state machine definition to SAM template

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.

I will check

First, let's check the current status. In the management console, select Step Functions. You can see that the state machine is deployed.

sfn1.png

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.

sfn2.png

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.

Try to run

Now let's run the state machine. Press the Start Execution button on the Run tab to run the state machine.

sfn3.png

This time, since the parameters etc. are not received on the Lambda side, it is okay to execute the input as it is.

sfn5.png

Execution is completed immediately and you can check the event history.

sfn6.png

If you check the linked Cloud Watch, you can be sure that the Lambda function is being called.

Summary

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)

Sample code repository

https://github.com/hito-psv/sam-demo-004

Recommended Posts

[AWS] Play with Step Functions (SAM + Lambda) Part.1 (Basic)
[AWS] Play with Step Functions (SAM + Lambda) Part.2 (Parameter)
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
Regular serverless scraping with AWS lambda + scrapy Part 1.8
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
AWS Step Functions to learn with a sample
Delete DynamoDB data after 5 minutes with AWS Step Functions
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Create API with Python, lambda, API Gateway quickly using AWS SAM
Play with a turtle with turtle graphics (Part 1)
[AWS] Create API with API Gateway + Lambda
Using Lambda with AWS Amplify with Go
Serverless scraping on a regular basis with AWS lambda + scrapy Part 1
Notify HipChat with AWS Lambda (Python)
[AWS] Using ini files with Lambda [Python]
Play handwritten numbers with python Part 2 (identify)
I want to play with aws with python
[AWS] Link Lambda and S3 with boto3
Connect to s3 with AWS Lambda Python
[AWS] Do SSI-like things with S3 / Lambda
Python + Selenium + Headless Chromium with aws lambda
I just did FizzBuzz with AWS Lambda
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Play with Lambda layer (python) for about 5 minutes
LINE BOT with Python + AWS Lambda + API Gateway
[AWS] Try tracing API Gateway + Lambda with X-Ray
I tried connecting AWS Lambda with other services
Infrastructure construction automation with CloudFromation + troposphere + AWS Lambda
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]