0

I have a very long but repetitive State Machine definition in json format and I am trying to extract some of the common blocks into the AWS SAM template using the DefinitionSubstitutions property of the State Machine block. For example, I have a Retry policy like so:

{
    "StartAt": "Generate Config",
    "States": {
        "Generate Config": {
            "Type": "Task",
            "Resource": "${GenerateConfigFunctionArn}",
            "Retry": [
                {
                    "ErrorEquals": [
                        "States.TaskFailed"
                    ],
                    "IntervalSeconds": 15,
                    "MaxAttempts": 5,
                    "BackoffRate": 1.5
                }
            ],
            "Next": "Process basins"
        },
...

I would like to extract the retry block into the AWS SAM template like so:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Resources:
  PredictiveAnalyticsPipelineOrchestration:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: stateMachine/definition.asl.json
      DefinitionSubstitutions:
        GenerateConfigFunctionArn: !GetAtt GenerateConfigFunction.Arn
        RetryPolicy: |
          [
            {
                "ErrorEquals": [
                    "States.ALL"
                ],
                "IntervalSeconds": 10,
                "MaxAttempts": 2,
                "BackoffRate": 1.5
            }
          ]
...

So the above becomes:

{
    "StartAt": "Generate Config",
    "States": {
        "Generate Config": {
            "Type": "Task",
            "Resource": "${GenerateConfigFunctionArn}",
            "Retry": "${RetryPolicy}",
            "Next": "Process basins"
        },
...

However, when I run aws deploy I get an error:

Resource handler returned message: "Invalid State Machine Definition:
'INVALID_JSON_DESCRIPTION: Illegal unquoted character ((CTRL-CHAR, code 10)):
 has to be escaped using backslash to be included in string value

I don't understand what I am doing wrong.

Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • should `"Retry": "${RetryPolicy}",` be `"Retry": \`${RetryPolicy}\`,`? – petey Apr 21 '21 at 03:15
  • Thank you for your comment @petey. However, I don't think that is the correct syntax of using tokens. There is actually a working example above the `"Retry": "${RetryPolicy}"` line. For example, `"Resource": "${GenerateConfigFunctionArn}",` is using a token with double quotation marks just fine. When I use "Retry": `${RetryPolicy}`, I get that error: `Invalid StateMachine definition` – Georgi Koemdzhiev Apr 21 '21 at 08:28

0 Answers0