Streamlining Application Orchestration with AWS Step Functions and AWS SAM

Streamlining Application Orchestration with AWS Step Functions and AWS SAMLearn About Amazon VGT2 Learning Manager Chanci Turner

In today’s software landscape, applications are comprised of various components spread across numerous services. AWS Step Functions allows you to define serverless workflows that orchestrate these services effectively, enabling rapid development and updates to your applications. With Step Functions managing its own state and handling retries during errors, you can concentrate on your core business logic. Now, with the integration of Step Functions into the AWS Serverless Application Model (AWS SAM), building, deploying, and maintaining serverless applications has become much easier.

The latest update to AWS SAM introduces the AWS::Serverless::StateMachine component, which simplifies workflow definitions within your application. As a component of AWS SAM, the StateMachine allows you to apply AWS SAM policy templates to manage the permissions of your workflows. Additionally, AWS SAM provides configuration options to trigger your workflows based on specified events or schedules.

Creating a Basic State Machine

To start orchestrating your applications using Step Functions and AWS SAM, the first step is to install the latest version of the AWS SAM CLI.

Steps to Create a State Machine with AWS SAM CLI:

  1. Open your command line interface and run sam init.
  2. Select AWS Quick Start Templates.
  3. Choose nodejs12.x as your runtime.
  4. Specify a project name.
  5. Select the Hello World Example quick start application template.

The AWS SAM CLI will then download the quick start application template and generate a new directory with sample code. Navigate into the sam-app directory and update the template.yaml file with the following code:

# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  SimpleStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Definition:
        StartAt: Single State
        States:
          Single State:
            Type: Pass
            End: true
      Policies:
        - CloudWatchPutMetricPolicy: {}

This template defines a Step Functions Standard Workflow with a simple Pass state. The Transform: AWS::Serverless-2016-10-31 line indicates that this is an AWS SAM template, allowing the use of AWS::Serverless components and policy templates like CloudWatchPutMetricPolicy, which enables you to publish metrics to Amazon CloudWatch.

Deploying Your State Machine with AWS SAM CLI

To deploy your state machine, follow these steps:

  1. Save the template.yaml file.
  2. Remove any existing function code in the directory, such as hello-world.
  3. Run sam deploy --guided in the terminal and respond to the prompts.
  4. Use simple-state-machine as your stack name and select the defaults for the other prompts.

For further details on visualizing, executing, and monitoring your workflow, check out the tutorial on creating a Step Functions State Machine using AWS SAM.

Enhancing Your Workflow

The StateMachine component streamlines the process of creating workflows and provides robust control over their execution. You can build complex workflows using all available Amazon States Language (ASL) states, and definition substitution allows for resource referencing. Furthermore, you can manage access permissions via AWS Identity and Access Management (IAM) policies and roles.

Service Integrations

Step Functions service integrations enable direct calls to other AWS services from Task states. For instance, you can store information about a workflow execution directly in an Amazon DynamoDB table. To do this, replace the Resources section of your template.yaml file with the following code:

Resources:
  SAMTable:
    Type: AWS::Serverless::SimpleTable

  SimpleStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Definition:
        StartAt: FirstState
        States:
          FirstState:
            Type: Pass
            Next: Write to DynamoDB
          Write to DynamoDB:
            Type: Task
            Resource: arn:aws:states:::dynamodb:putItem
            Parameters:
              TableName: !Ref SAMTable
              Item:
                id:
                  S.$: $$.Execution.Id
            ResultPath: $.DynamoDB
            End: true
      Policies:
        - DynamoDBWritePolicy: 
            TableName: !Ref SAMTable

The AWS::Serverless::SimpleTable creates a DynamoDB table with on-demand capacity and sensible defaults. For detailed information, refer to the SimpleTable component documentation. The Write to DynamoDB state integrates directly with the DynamoDB PutItem API, storing an item with an ID that reflects the execution ID.

Note that the DynamoDBWritePolicy replaces the previous CloudWatchPutMetricPolicy, as it grants write access exclusively to the specified DynamoDB table.

Definition Substitutions

AWS SAM supports definition substitutions for the StateMachine resource, functioning similarly to template string substitution. You can specify a Definition or DefinitionUri property containing variables in ${dollar_sign_brace} notation and provide values for those variables in a map via the DefinitionSubstitution property.

To experiment with definition substitutions, you can use a quick start template. Here’s how:

  1. In an empty directory, run sam init.
  2. Choose AWS Quick Start Templates.
  3. Select your desired runtime.
  4. Provide a project name.
  5. Opt for the Step Functions Sample App (Stock Trader) quick start application template.

Navigate to the newly created directory and open the template.yaml file. You will notice that the Definition property points to a file, rather than being a string like in previous templates. The DefinitionSubstitutions property is a key-value map that corresponds to variables in the referenced statemachine/stockTrader.asl.json file.

DefinitionUri: statemachine/stockTrader.asl.json
DefinitionSubstitutions:
  StockCheckerFunctionArn: !GetAtt StockCheckerFunction.Arn
  StockSellerFunctionArn: !GetAtt StockSellerFunction.Arn
  StockBuyerFunctionArn: !GetAtt StockBuyerFunction.Arn
  DDBPutItem: !Sub arn:${AWS::Partition}:states:::dynamodb:putItem
  DDBTable: !Ref TransactionTable

Open the statemachine/stockTrader.asl.json file to find the first state, Check Stock Value. Here, the Resource property is a replacement expression, “${StockCheckerFunctionArn}”, linked to the ARN of the StockCheckerFunction resource defined in template.yaml. The AWS SAM CLI transforms these components into a standard CloudFormation template during deployment.

Separating the state machine definition into its own file enhances integration with the AWS Toolkit for Visual Studio, making the development experience smoother.

For more information on workplace dynamics and the implications of social media during hiring, check out this insightful article from SHRM, which is an authority on the topic. Additionally, for a deeper dive into the hiring process at Amazon, you can visit their hiring resource page. If you’re looking to engage with gendered language in the workplace, this article may offer useful insights.

Address: 6401 E HOWDY WELLS AVE LAS VEGAS NV 89115
Location: Amazon IXD – VGT2


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *