Given this input (which is output from a previous map task):
[
{
"result": {
"validated": true,
"order": "1"
}
},
{
"result": {
"validated": true,
"order": "2"
}
}
]
how do I access the validated flag in a choice task. I would have thought it is:
"Choice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.result[0].validated",
"BooleanEquals": true,
"Next": "Success"
}
],
but that doesn't work - I get "Invalid path '$.result[0].validated': The choice state's condition path references an invalid value".
Also I will never know how many 'results' will be in that array (produced by the map task). How can I process the 'validated' flag to check that they are all true? Any suggestions welcome!
hi with the given input it is evident that '$.result[0].validated' will be invalid as '$' represents the root of the input and you are accessing 'result' on the root which does not exist. '$[0].result.validated' should work instead.
Related
I have this dynamodb:Query in my step function:
{
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:dynamodb:query",
"Next": "If nothing returned by query Or Study not yet Zipped",
"Parameters": {
"TableName": "TEST-StudyProcessingTable",
"ScanIndexForward": false,
"Limit": 1,
"KeyConditionExpression": "OrderID = :OrderID",
"FilterExpression": "StudyID = :StudyID",
"ExpressionAttributeValues": {
":OrderID": {
"S.$": "$.body.order_id"
},
":StudyID": {
"S.$": "$.body.study_id"
}
}
},
"ResultPath": "$.processed_files"
}
The results comes in as an array called Items which is nested under my ResultPath
processed_files.Items:
{
"body": {
"order_id": "1001",
"study_id": "1"
},
"processed_files": {
"Count": 1,
"Items": [
{
"Status": {
"S": "unzipped"
},
"StudyID": {
"S": "1"
},
"ZipFileS3Key": {
"S": "path/to/the/file"
},
"UploadSet": {
"S": "4"
},
"OrderID": {
"S": "1001"
},
"UploadSet#StudyID": {
"S": "4#1"
}
}
],
"LastEvaluatedKey": {
"OrderID": {
"S": "1001"
},
"UploadSet#StudyID": {
"S": "4#1"
}
},
"ScannedCount": 1
}
}
My question is how do i access the items inside this array from a choice state in a step function?
I need to query then decide something based on the results by checking the item in a condition in a choice state.
The problem is that since this is an array I can't access it using regular JsonPath (like with Items.item), and in my next step the choice condition does NOT accept an index like processed_files.Items['0'].Status
Ok so the answer was so simple all you need to do is use a number instead of string for the array index like this.
processed_files.Items[0].Status
I was originally mislead by an error I received which said that it expected a ' or '[' after the first '['. I mistakenly thought this meant it only accepts strings.
I was wrong, it works like any other array.
I hope this helps somebody one day.
I have a step where I want to update a object on a DynamoDB table.
Everything works except its creating a new object with the ID value of "$.id", instead of updating where the ID I pass in.
This is my first state machine attempt so what have I done wrong here?
"update-table-processing": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:updateItem",
"ResultPath": "$.updateResult",
"Parameters": {
"TableName": "Projects",
"Key": {
"id": {
"S": "$.id"
}
},
"UpdateExpression": "SET step = :updateRef",
"ExpressionAttributeValues": {
":updateRef": {
"S": "processing"
}
},
"ReturnValues": "ALL_NEW"
},
"Next": "create-project"
},
Do I somehow need to tell DynamoDB to evaluate "$.id" rather than treating it as a "S", or is this happening because I've not mapped the input correctly that the "$.id" value is empty?
My input looks like:
{
"id": "f8185735-c90d-4d4e-8689-cec68a48b1bc"
}
In order to specify data from your input you have to use a Key-Value pair, with the key value ending in a ".$". So to fix this you need to change it to:
"Key": {
"id": {
"S.$": "$.id"
}
},
Using the above it should correctly resolve to the value from your input instead of the string value "$.id".
References - https://docs.aws.amazon.com/step-functions/latest/dg/input-output-inputpath-params.html#input-output-parameters
In an AWS Step Function, in a Choice step, we want to compare a result from an AWS Lambda function to a threshold given as a parameter using "NumericGreaterThan".
In our example, we compare a calculated from a lambda with a threshold given by the event.
I tried defining my step function in the following way:
{
"StartAt": "Check Enough Data",
"States": {
"Check Enough Data": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ID:function:FUNCTION:$LATEST",
"Next": "Validate Count",
"ResultPath": "$.count"
},
"Validate Count": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.count",
"NumericGreaterThan": "$.threshold",
"Next": "Succeed State"
}
],
"Default": "Wait 24 Hours"
},
"Wait 24 Hours": {
"Type": "Wait",
"Seconds": 86400,
"Next": "Check Enough Data"
},
"Succeed State": {
"Type": "Succeed"
}
}
}
but got an error Expected value of type: Integer, Float insted of String.
If I replace "$.threshold" with a hard-coded value (like 20), it works, but the value is not dynamic as I want.
The following input should cause the lambda to get to the Succeed State:
{
"country": "japan",
"threshold": 40
}
I know we can replace the Choice step with another Lambda function, but we do not want to do that from cost-effective issues.
Does anyone have an idea on how to solve the problem?
you can use 'NumericGreaterThanPath' operator as per the docs https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-choice-state.html
Within a Choice Rule, the value of Variable can be compared with another value from the state input by appending 'Path' to name of supported comparison operators.
NumericEqualsPath, NumericGreaterThanPath, NumericGreaterThanEqualsPath, etc.
The comparison operators need to have an integer after ":". It can't be an string.
A workaround is that "Variable": "$.count" change to "Variable": "$.count/$.threshold" so that you can have "NumericGreaterThan": 1.
In that case, you have count and threshold that define the Choice action.
Let me know if that fixes your problem
Precision: "Variable": "$.count" becomes "Variable": "$.ratio"
where ratio = count/threshold
I want to branch the flow of an AWS State Machine based on how many items are in an array. If the array has 0 items, I want to end the flow. If it has more than 0 items, I want to do some stuff.
For example, I want to do something like the following:
{
"StartAt": "IsBig",
"States": {
"IsBig": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.things.length",
"NumericGreaterThan": 0,
"Next": "Big"
}
],
"Default": "Small"
},
"Big": {
"Type": "Pass",
"Result": "1",
"End": true
},
"Small": {
"Type": "Pass",
"Result": "0",
"End": true
}
}
}
I'd then pass in the following on execution:
{ "things": [1, 2, 3] }
I'd want IsBig to then call Big and end.
Is there a way to do that in AWS states language?
If I can't, I'll just create a Lambda that gets the array's length. I am just curious.
The answer is "no". You can't run a function from the "Variable": "$.things.length" property.
The value in the Variable field is a.... variable. It's not an expression.
The docs don't show any expression evaluation syntax. So, long story short, you can't do what I was looking to do.
I have a parallel task in the step function that contains two branches.
The input was:
{
"database": "test",
"userName": "tester",
"userID": "test123",
"adGroup": "testADGroup",
"dbGroup": "ReadGroup"
}
Each branch return a json result like the following
Branch 1 (I used "OutputPath": "$"):
{
"requestType": "GrantAccess",
"DBUser": "exists",
"ADUser": "exists"
}
Branch 2 (I used "ResultPath": "$.approvalStatus"):
{
"database": "test",
"userName": "tester",
"userID": "test123",
"adGroup": "testADGroup",
"dbGroup": "ReadGroup"
"approvalStatus": "Approved"
}
When both the branches complete, the output of the parallel task return:
[
{
"requestType": "GrantAccess",
"DBUser": "exists",
"ADUser": "exists"
},
{
"database": "test",
"userName": "tester",
"userID": "test123",
"adGroup": "testADGroup",
"dbGroup": "ReadGroup"
"approvalStatus": "Approved"
}
]
The next task is a choices,
"Choices": [
{
"Variable": "$.input[1].approvalStatus",
"StringEquals": "Approved",
"Next": "ProcessRequest"
},
{
"Variable": "$.input[1].approvalStatus",
"StringEquals": "Declined",
"Next": "SendDeclineNotification"
}
]
and it is keep giving me the following error:
"cause": "An error occurred while executing the state 'CheckApprovalStatus' (entered at the event id #16). Invalid path '$.input[1].approvalStatus': The choice state's condition path references an invalid value."
So here are my questions,
1) How should I reference it in the choice task to get the approvalStatus value?
2) Is there are anyway I can make the parallel task return in json format instead of an array?
Thanks in advance
I think you should use something like "$[1].approvalStatus" if you don't want to change the ResultPath.