我可以在状态机定义文件中使用全局变量吗?

Can I use Global variables in State Machine definition files?

我正在使用 AWS SAM 开发一个无服务器项目,它由 lambda 和状态机资源组成(请参阅下面的 SAM 模板)。我想知道是否有一种方法可以在我的状态机定义文件中定义一个“全局”变量,就像我们如何为 SAM 模板使用 Globals 一样。

我想这样做的原因是为了缩短我的状态机定义。现在有状态使用完全相同的属性,例如我的 Fargate 任务的 NetworkConfiguration 块:

definition.asl.json

{
    "StartAt": "Generate Config",
    "States": {
        "Generate Config": {
            "Type": "Task",
            "Resource": "${GenerateConfigFunctionArn}",
            "Next": "ETL"
        },
        "ETL": {
            "Type": "Task",
            "Resource": "arn:aws:states:::ecs:runTask.sync",
            "Next": "ETL",
            "Parameters": {
                "LaunchType": "FARGATE",
                "Cluster": "arn:aws:ecs:region:11111111:cluster/clustername",
                "TaskDefinition": "arn:aws:ecs:region:11111111:task-definition/taskdef",
                "NetworkConfiguration": {
                    "AwsvpcConfiguration": {
                        "Subnets": [
                            "subnet-00d610225d369486e"
                        ],
                        "SecurityGroups": [
                            "sg-016650ff9ecd28a9a"
                        ],
                        "AssignPublicIp": "ENABLED"
                    }
                },
                "Overrides": {
                    "ContainerOverrides": [
                        {
                            "Name": "Phase-1-start",
                            "Environment": [
                                {
                                    "Name": "COMMAND",
                                    "Value": "etl"
                                }
                            ]
                        }
                    ]
                }
            }
        },
        "Well_Calc": {
            "Type": "Task",
            "Resource": "arn:aws:states:::ecs:runTask.sync",
            "End": true,
            "Parameters": {
                "LaunchType": "FARGATE",
                "Cluster": "arn:aws:ecs:region:11111111:cluster/clustername",
                "TaskDefinition": "arn:aws:ecs:region:11111111:task-definition/taskdef",
                "NetworkConfiguration": {
                    "AwsvpcConfiguration": {
                        "Subnets": [
                            "subnet-00d610225d369486e"
                        ],
                        "SecurityGroups": [
                            "sg-016650ff9ecd28a9a"
                        ],
                        "AssignPublicIp": "ENABLED"
                    }
                },
                "Overrides": {
                    "ContainerOverrides": [
                        {
                            "Name": "Phase-1-start",
                            "Environment": [
                                {
                                    "Name": "COMMAND",
                                    "Value": "calculation"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
}

SAM template

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Resources:
  MySAMPOC:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: stateMachine/definition.asl.json
      DefinitionSubstitutions:
        GenerateConfigFunctionArn: !GetAtt GenerateConfigFunction.Arn
  GenerateConfigFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/generate_config
      Handler: app.lambda_handler
      Runtime: python3.8

状态机定义是否支持使用全局变量(或类似变量)?

AWS::Serverless::StateMachine 目前不支持全局变量。您可以考虑的一些备选方案: