I have a problem. I am knew in AWS services ecosystem. I actually want to create a step function with choice attributes. But, I would like to iterate on a python dictionary based on the value I get. Here is the code:
"getAssociatedAction":{
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"InputPath": "$.preprodResult",
"ResultPath": "$.dwResult",
"Next": "Insert update or delete"
},
"Insert update or delete":{
"Type": "Choice",
"Choices": [
{
"Variable": "$.dwResult.Payload",
"StringEquals": "insert",
"Next": "insert customer"
},
{
"Variable": "$.dwResult.Payload",
"StringEquals": "update",
"Next": "update customer"
},
{
"Variable": "$.dwResult.Payload",
"StringEquals": "delete",
"Next": "Delete customer"
}
]
},
"insert customer":{
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"InputPath": "",
"OutputPath": "",
"ResultPath": "$",
"End": true
},
def lambda_handler(event, context):
objects_list = getActionData(db_connection_dw)
for key, value in objects_list.items():
if value == 'insert':
action = "action"
return loads(json.dumps(action), object_hook=as_python_object)
elif value == 'update':
action = "update"
return loads(json.dumps(action), object_hook=as_python_object)
elif value == 'delete':
action = "delete"
return loads(json.dumps(action), object_hook=as_python_object)
Is this possible with AWS Lambda ?
CodePudding user response:
You should remove the "InputPath" property from your lambda Task
Doing so will cause the event that passed into the Choice State (with the key of $.dwResult
and a sub key of Payload
) as part of the incoming event to your lambda. You can then do whatever you want with that key. event['dwResult']['Payload]
You have over engineered your step function here. There is no reason to do the choice state if they are all going to end up at the same lambda and that again is going to decide what you do with it inside the code. The entire point of having a Choice Task is to lead to different paths with different Tasks associated with them. You could have 3 different lambdas, one for each operation you are checking for and they only have the code for that given operation. This would be useful in a scenario where there are lots of tasks to do for a given operation first, and a keep the structure far more clean. If the tasks are just simple one or two line tasks for each operation, then its far more engineering than you need for your scenario.
I recommend looking into https://docs.aws.amazon.com/step-functions/latest/dg/input-output-example.html on how the InputPath, ResultPath, and OutputPath manipulate the event as it passes through the various State Machine tasks. Remember, its called a State Machine because it 'maintains the state of data between tasks' - but you can manipulate that state with the three Path variables