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

问题描述

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

我想这样做的原因是为了缩短我的状态机定义。现在有状态使用完全相同的属性,例如我的 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","End": true,"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。您可以考虑的一些替代选项: