如何修改AWS Step功能?

时间:2017-03-30 22:31:59

标签: amazon-web-services aws-step-functions

从AWS控制台看,AWS Step功能似乎是不可变的。有没有办法修改它?如果没有,版本控制如何工作?每次必须对状态机进行增量更改时,是否必须创建新的状态机?

3 个答案:

答案 0 :(得分:1)

根据this论坛条目,尚未修改现有状态机。你需要每次都创建一个新的。

答案 1 :(得分:0)

目前你可以编辑状态mashine。 Buttton"编辑状态机"在右上角

答案 2 :(得分:0)

这些天,我一直在使用带有 boto3 的 CloudFormation。就写到这里吧,因为过去我对 CloudFormation 有点害怕,但如果有一个端到端的例子,也许它更平易近人。

step_function_stack.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
  A description of the State Machine goes here. 
Resources:
  MyStateMachineName:
    Type: AWS::StepFunctions::StateMachine
    Properties:
      RoleArn: "arn:aws:iam::{{aws_account_id}}:role/service-role/StepFunctions-MyStepFunctionRole"

      StateMachineName: "MyStateMachineName"
      StateMachineType: "EXPRESS"

      DefinitionString:
        Fn::Sub: |
          {{full_json_definition}}

manage_step_functions.py

import boto3
import os
import time
from jinja2 import Environment


def do_render(full_json_definition):
    with open('step_function_stack.yaml') as fd:
        template = fd.read()
    yaml = Environment().from_string(template).render(
            full_json_definition=full_json_definition,
            aws_account_id=os.getenv('AWS_ACCOUNT_ID')) 
    return yaml


def update_step_function(stack_name, full_json_definition,):
    yaml = do_render(full_json_definition)
    client = boto3.client('cloudformation')
    response = client.update_stack(
        StackName=stack_name,
        TemplateBody=yaml,
        Capabilities=[
            'CAPABILITY_AUTO_EXPAND',
        ])
    return response


def create_step_function(stack_name, full_json_definition,):
    yaml = do_render(full_json_definition)
    client = boto3.client('cloudformation')
    response = client.update_stack(
        StackName=stack_name,
        TemplateBody=yaml,
        Capabilities=[
            'CAPABILITY_AUTO_EXPAND',
        ])
    return response
    

  def get_lambdas_stack_latest_events(stack_name):
      # Get the first 100 most recent events.
      client = boto3.client('cloudformation')
      return client.describe_stack_events(
          StackName=stack_name)


  def wait_on_update(stack_name):
      events = None
      while events is None or events['StackEvents'][0]['ResourceStatus'] not in ['UPDATE_COMPLETE',
              'UPDATE_ROLLBACK_COMPLETE', 'DELETE_COMPLETE', 'CREATE_COMPLETE']:

          print(events['StackEvents'][0]['ResourceStatus'] if events else ...)
          events = get_lambdas_stack_latest_events(stack_name)
          time.sleep(1)

      return events 

step_function_definition.json

{
  "Comment": "This is a Hello World State Machine from https://docs.aws.amazon.com/step-functions/latest/dg/getting-started.html#create-state-machine",
  "StartAt": "Hello",
  "States": {
    "Hello": {
      "Type": "Pass",
      "Result": "Hello",
      "Next": "World"
    },
    "World": {
      "Type": "Pass",
      "Result": "World",
      "End": true
    }
  }
}

创建阶跃函数

# From a python shell for example
# First just set any privileged variables through environmental variables so they are not checked into code
# export AWS_ACCOUNT_ID=999999999

# edit step_function_definition.json then read it
with open('step_function_definition.json') as fd:
    step_function_definition = fd.read()
    
import manage_step_functions as msf
stack_name = 'MyGloriousStepFuncStack'
msf.create_step_function(stack_name, step_function_definition)

如果您准备好更新您的状态机,您可以编辑 step_function_definition.json,或者您可以创建一个新文件以供参考,step_function_definition-2021-01-29.json。 (因为在撰写本文时,Step Functions 没有像 Lambda 这样的版本)。

import manage_step_functions as msf
stack_name = 'MyGloriousStepFuncStack'
with open('step_function_definition-2021-01-29.json') as fd:
    step_function_definition = fd.read()

msf.update_step_function(stack_name, step_function_definition)