Skip to content

How to inclue cloudformation parameter into step function ?

0

Hi, I want to use cloudformation parameters with StepFunction, But it thorws error like this.

Does anyone know how to solve this?

##error message	
Resource handler returned message: "Invalid State Machine Definition: 'INVALID_JSON_DESCRIPTION: Unrecognized token 'TestBucket': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at
 [Source: (String)"{ "StartAt": "Init", "TimeoutSeconds": 4800, "States": { "Init": { "Type": "Pass", "Assign": { "bucket":TestBucket "LogGroups": [], "NextToken": null, "YesterdayStartTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T00:00:00.000Z') %}", "YesterdayEndTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T23:59:59.999Z') %}", "date_prefix": "{% $fromMillis($toMil"[truncated 127 chars]; line: 8, column: 28] at [Source: (String)"{ "StartAt": "Init", "TimeoutSeconds": 4800, "States": { "Init": { "Type": "Pass", "Assign": { "bucket":TestBucket "LogGroups": [], "NextToken": null, 
"YesterdayStartTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T00:00:00.000Z') %}", "YesterdayEndTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T23:59:59.999Z') %}", 
"date_prefix": "{% $fromMillis($toMil"[truncated 127 chars]; line: 8, column: 28]' (Service: Sfn, Status Code: 400, Request ID: 819574eb-645c-4870-bb22-d65f1b6d8f16) (SDK Attempt Count: 1)" (RequestToken: 4390ccb5-d15d-c4a5-7cdd-83504eff6edf, HandlerErrorCode: InvalidRequest)

Here is the template I used .

Parameters:
  DestinationBucket:
    Type: String
    Description: CloudWatch Logs export destination bucket
    Default: "TestBucket"
  StepFunctionName:
    Type: String
    Default: "test_function"
Resources:
  SfnRole:
    Type: "AWS::IAM::Role"
    Properties:
      RoleName: !Sub ${StepFunctionName}SFNRole
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: "Allow"
            Principal:
                Service: states.amazonaws.com
            Action: "sts:AssumeRole"
      Policies:
        - PolicyName: "SfnExportLogsPolicy"
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: "Allow"
                Action:
                  - "logs:CreateExportTask"
                  - "logs:CancelExportTask"
                  - "logs:DescribeExportTasks"
                  - "logs:DescribeLogStreams"
                  - "logs:DescribeLogGroups"
                Resource: "*"
  MyStateMachine: 
    Type: AWS::StepFunctions::StateMachine
    Properties:
      RoleArn: !GetAtt SfnRole.Arn
      StateMachineName: !Ref StepFunctionName
      DefinitionSubstitutions:
        DestinationBucket: !Ref DestinationBucket   
      DefinitionString:  |-
        {
          "StartAt": "Init",
          "TimeoutSeconds": 4800,
          "States": {
            "Init": {
              "Type": "Pass",
              "Assign": {
                "bucket":${DestinationBucket}
                "LogGroups": [],
                "NextToken": null,
                "YesterdayStartTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T00:00:00.000Z') %}",
                "YesterdayEndTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T23:59:59.999Z') %}",
                "date_prefix": "{% $fromMillis($toMillis($now()) - 86400000, \"[Y0001]/[M01]/[D01]\", \"Z\") %}"
              },
              "End":true
            }
          },
          "QueryLanguage": "JSONata"
        }

asked 10 months ago153 views

1 Answer
0
Accepted Answer

The error you're encountering is due to incorrect JSON syntax in your Step Functions definition. When using CloudFormation parameters in a Step Functions definition, you need to properly format them as JSON strings.

In your case, there are two issues:

  1. The bucket parameter is missing quotes around the parameter substitution
  2. There's a missing comma after the bucket parameter

Here's the corrected version of your DefinitionString section:

DefinitionString:  |-
{
"StartAt": "Init",
"TimeoutSeconds": 4800,
"States": {
"Init": {
"Type": "Pass",
"Assign": {
"bucket": "${DestinationBucket}",
"LogGroups": [],
"NextToken": null,
"YesterdayStartTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T00:00:00.000Z') %}",
"YesterdayEndTime": "{% $toMillis($substring($fromMillis($toMillis($now()) - 86400000), 0, 10) & 'T23:59:59.999Z') %}",
"date_prefix": "{% $fromMillis($toMillis($now()) - 86400000, \"[Y0001]/[M01]/[D01]\", \"Z\") %}"
},
"End":true
}
},
"QueryLanguage": "JSONata"
}

The key changes are:

  • Added quotes around ${DestinationBucket}
  • Added a comma after the bucket line

When using CloudFormation parameters in Step Functions definitions, you need to ensure that the substituted values maintain valid JSON syntax. The DefinitionSubstitutions property you're using correctly maps the parameter, but the syntax in the definition string itself needs to be valid JSON after substitution.
Sources
Troubleshooting issues in Step Functions - AWS Step Functions

answered 10 months ago

AWS
EXPERT

reviewed 10 months ago

  • Thank you for your swift response.! It worked.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.