How to use variables defined in your samconfig.toml file in your Step Function definition

To Nha Notes | Oct. 30, 2023, 12:06 a.m.

You can use variable substitution in AWS SAM to substitute ARNs into your state machine at the time of deployment1. This allows you to use variables defined in your samconfig.toml file in your Step Function definition.

Here’s an example of how you can do this:

  1. Define your variables in the samconfig.toml file. For example:
[default.deploy.parameters]
BucketName = "my-bucket"
KeyName = "my-key"
  1. In your AWS SAM template, you can reference these variables. For example:
Parameters:

  Parameter1:

    Type: String

    Default: ''

    Description: MWAA DAGs trigger function arn.

Parameter2:

    Type: String

    Default: ''

    Description: MWAA DAGs trigger function execution role arn.

Parameter3:

    Type: String

    Default: ''

    Description: MWAA environment name.
Resources:
  MyStateMachine:
    Type: 'AWS::Serverless::StateMachine'
    Properties:
      DefinitionSubstitutions:
        BucketName: !Ref BucketName
        KeyName: !Ref KeyName
        Parameter1: !Ref Parameter1
       Parameter2: !Ref Parameter2
       Parameter3: !Ref Parameter3
      ...
  1. In your state machine definition, you can use these variables. For example:
{
  "StartAt": "MyState",
  "States": {
    "MyState": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "Parameters": {
        "BucketName.$": "$$.Execution.Input.BucketName",
        "KeyName.$": "$$.Execution.Input.KeyName"
        "Parameter1": "${Parameter1}",
        "Parameter2": "${Parameter2}",
        "Parameter3": "${Parameter3}"
        ...
      },
      ...
    }
  }
}

In this example, BucketName and KeyName are substituted into the state machine definition at the time of deployment1. The values for these variables are taken from the samconfig.toml file.

Please note that when using AWS SAM local, you can emulate Lambda and API Gateway locally. However, you can’t emulate Step Functions locally using AWS SAM1.