Search code examples
amazon-web-servicesaws-step-functions

Can I use Global variables in State Machine definition files?


I am working on a serverless project using AWS SAM which consists of lambda and State Machine resources (see the SAM template bellow). I am wondering if there is a way to define a "Global" variable in my state machine definition file the same way how we can use Globals for the SAM template.

The reason I would like to do that is to shorten my state machine definition. Right now there states that use exactly the same properties such as the NetworkConfiguration block for my Fargate task:

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

Is the use of Globals (or similar) supported for state machine definition?


Solution

  • Globals aren't support for AWS::Serverless::StateMachine at the moment. Some alternative options you can consider: