AWS Step Function Parameters

AWS Step Function Parameters

To solve a particular business problem I needed to find a solution to pass input parameters into a step function and pass a value from one lambda function to another, via that same step function.

The article image shows the process and will assist in the understanding of the solution.

To put into context, a trigger on an s3 bucket initiates a call to a lambda function (lam1), which then executes the step function (step1) The step function invokes a second lambda function (lam2). On successful completion of lam2, the step function then invokes a third lambda (lam3).

From the s3 event context received at lam1, I extracted just the key, also referred to as the prefix. I then populated a dictionary with this value.

source_key = event['Records'][0]['s3']['object']['key']

input_dict = {
    input_dict['source_key'] = source_key
}        

In otherwords, the source_key variable contains the prefix (or path) of the file that was just uploaded to the s3 bucket.

Then, I initiated the execution of step1 from lam1 by using boto3

sfn_client = boto3.client('stepfunctions')
       
response = sfn_client.start_execution(
      stateMachineArn = step_function_arn,
      input = str(input_dict).replace("'","\"")
)        

To utilise this information within step1 I needed to add

"InputPath": "$",
"ResultPath":"$"        

to each state within step1, the data being assigned to InputPath.

Then, as part of the lam2 state definition within step1, I specified the following payload to pass to lam2

"Parameters":{
    "FunctionName": "arn:aws:lambda:eu-central-1:account:function:lam2", 
    "Payload":{         
          "source_key.$":"$.source_key"  
     }   
}        

In order for this to work, we need to place .$ after the parameter name (source_key) and then specify $. to extract the source_key value from InputPath

Then, within lam2, I captured the input parameter value from the event context

source_key = event['source_key']        

At the end of lam2 I added the following

return {'curated_key_suffix': result}        

This returned a value back to step1 which was stored in

 "ResultPath":"$",        

Then, provided that lam2 succeeded, I could invoke lam3 with the following payload

"Parameters":{
    "FunctionName": "arn:aws:lambda:eu-central-1:account:function:lam3", 
    "Payload":{         
          "curated_key_suffix.$":"$.Payload.curated_key_suffix"  
     }   
}         

lam3 received the parameter data as an event context

curated_key_suffix = json.dumps(event['curated_key_suffix']).strip('"')        


To view or add a comment, sign in

More articles by Gary Juleff

  • Extract Excel Data with AWS Lambda

    A requirement existed where I needed to extract data from Excel from within an AWS Lambda Function. Having read several…

  • An Introduction to PowerBI - #10 of 10

    This final article follows ipon from slicers and briefly touches on DAX Expressions DAX Expressions DAX Expressions…

  • An Introduction to PowerBI - #9 of 10

    This article follows on from filtering Slicers Slicers allow users interacting with the Power BI report the ability to…

  • An Introduction to PowerBI - #8 of 10

    This article follows on from maps. Filtering can be applied at various levels within a Power BI report as follows: 1.

  • An Introduction to PowerBI - #7 of 10

    Previously I demonstrated how to create bar charts using the Total Sales measure that we created in an earlier article.…

  • An Introduction to PowerBI - #6 of 10

    After looking at measures in the previous article we now take a look at charts. Charts With charting we can gain…

  • An Introduction to PowerBI - #5 of 10

    In the last article we looked at calculated columns. This time around it's the turn of measures.

  • An Introduction to PowerBI - #4 of 10

    In the last article we discussed Shaping and Cleansing. Today, we move onto Calculated Columns.

  • An Introduction to PowerBI - #3 of 10

    In the second article of this series I discussed merging tables and relationships. In this article I will touch on…

  • An Introduction to PowerBI - #2 of 10

    In the first article I discussed data preparation and load. In this article I will briefly touch on Relationships and…

Others also viewed

Explore content categories