admin管理员组

文章数量:1287554

I am working with AWS Step Functions and trying to send an event to an EventBridge event bus using the PutEvents API. However, I am encountering an issue with the following Step Function configuration in my "SendRequestToEventBus" step:

"Entries": [
  {
    "Source": "us-east-1-service-dev-scheduler-execute-schedule-sfn",
    "DetailType": "EnqueueJobRequest",
    "EventBusName": "us-east-1-service-dev-job-engine-event-bus",
    "Detail": {
      "instanceId.$": "States.StringToJson($.Item.instanceId.N)",
      "jobCorrelationId.$": "$.Item.jobCorrelationId.S",
      "jobType.$": "$.Item.jobType.S",
      "parameters.$": "States.StringToJson($.Item.parameters.S)",
      "priority.$": "$.Item.priority.S"
    }
  }
]

Problem:

The parameters field sometimes contains a Map (with the structure "M": {}), and sometimes it contains a string that I need to convert to JSON. When the parameters field is stored as a Map in DynamoDB, I get the following error:

The function 'States.StringToJson($.Item.parameters.S)' had the following error: The JsonPath argument for the field '$.Item.parameters.S' could not be found in the input.

Solution I'm Looking For:

I want to ensure that:

  1. If the parameters field is already a Map ("M": {}), No need to convert it to JSON and can be used as it is.

  2. If the parameters field is a string, I want to apply States.StringToJson.

In future, the parameters field will be stored as a string in DynamoDB, so I need a solution that works for both cases.

As I am not expert in JsonPath, I am not sure how to use the evaluation expression in JsonPath to achieve this.

Your help here is highly appreciated...

I am working with AWS Step Functions and trying to send an event to an EventBridge event bus using the PutEvents API. However, I am encountering an issue with the following Step Function configuration in my "SendRequestToEventBus" step:

"Entries": [
  {
    "Source": "us-east-1-service-dev-scheduler-execute-schedule-sfn",
    "DetailType": "EnqueueJobRequest",
    "EventBusName": "us-east-1-service-dev-job-engine-event-bus",
    "Detail": {
      "instanceId.$": "States.StringToJson($.Item.instanceId.N)",
      "jobCorrelationId.$": "$.Item.jobCorrelationId.S",
      "jobType.$": "$.Item.jobType.S",
      "parameters.$": "States.StringToJson($.Item.parameters.S)",
      "priority.$": "$.Item.priority.S"
    }
  }
]

Problem:

The parameters field sometimes contains a Map (with the structure "M": {}), and sometimes it contains a string that I need to convert to JSON. When the parameters field is stored as a Map in DynamoDB, I get the following error:

The function 'States.StringToJson($.Item.parameters.S)' had the following error: The JsonPath argument for the field '$.Item.parameters.S' could not be found in the input.

Solution I'm Looking For:

I want to ensure that:

  1. If the parameters field is already a Map ("M": {}), No need to convert it to JSON and can be used as it is.

  2. If the parameters field is a string, I want to apply States.StringToJson.

In future, the parameters field will be stored as a string in DynamoDB, so I need a solution that works for both cases.

As I am not expert in JsonPath, I am not sure how to use the evaluation expression in JsonPath to achieve this.

Your help here is highly appreciated...

Share Improve this question edited Feb 24 at 11:21 fa44 5021 gold badge3 silver badges11 bronze badges asked Feb 24 at 10:18 Ranjith KumarRanjith Kumar 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I believe you can solve this with the use of Choice workflow sate to check the param type before using it:

{
  "StartAt": "CheckParametersType",
  "States": {
    "CheckParametersType": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.Item.parameters.M",
          "IsPresent": true,
          "Next": "SendRequestToEventBusMap"
        }
      ],
      "Default": "SendRequestToEventBusString"
    },
    "SendRequestToEventBusMap": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:eventbridge:putEvents",
      "Parameters": {
        "Entries": [
          {
            "Source": "us-east-1-service-dev-scheduler-execute-schedule-sfn",
            "DetailType": "EnqueueJobRequest",
            "EventBusName": "us-east-1-service-dev-job-engine-event-bus",
            "Detail": {
              "instanceId.$": "States.StringToJson($.Item.instanceId.N)",
              "jobCorrelationId.$": "$.Item.jobCorrelationId.S",
              "jobType.$": "$.Item.jobType.S",
              "parameters.$": "$.Item.parameters.M",
              "priority.$": "$.Item.priority.S"
            }
          }
        ]
      },
      "End": true
    },
    "SendRequestToEventBusString": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:eventbridge:putEvents",
      "Parameters": {
        "Entries": [
          {
            "Source": "us-east-1-service-dev-scheduler-execute-schedule-sfn",
            "DetailType": "EnqueueJobRequest",
            "EventBusName": "us-east-1-service-dev-job-engine-event-bus",
            "Detail": {
              "instanceId.$": "States.StringToJson($.Item.instanceId.N)",
              "jobCorrelationId.$": "$.Item.jobCorrelationId.S",
              "jobType.$": "$.Item.jobType.S",
              "parameters.$": "States.StringToJson($.Item.parameters.S)",
              "priority.$": "$.Item.priority.S"
            }
          }
        ]
      },
      "End": true
    }
  }
}

本文标签: amazon web servicesConditional Expression in JSONPath for AWS Step FunctionStack Overflow