Invoke AWS Step function at S3 Event
Posted By : Shubham Jain | 31-Jan-2019
The step function is one of the best thing AWS provided to procedural call Lambda functions. What the main issue comes with it that it can not be trigger from the S3 bucket for the storage related work. Now to solve this problem we can introduce another lambda function which will be used as Proxy Lambda function to invoke and transfer event to the AWS Step function.
So Let us assume that we have a step function which will work on the S3 objects and obviously a S3 bucket. A lambda function will also be required which will work as invoking function for Step Function.
For this blog we are creating a lamda function with Python 3.7.
import json
import boto3
import datetime
from botocore.client import ClientError
status_code = 200
def invokeStepFunction_handler(event, context):
try:
client = boto3.client('stepfunctions')
status_code = client.start_execution(
stateMachineArn='<step function arn>',
name= str(datetime.datetime.now().timestamp())
input= json.dumps(event)
)
print(status_code)
except Exception as e:
print ('Exception: %s', % e)
status_code = 500
raise
finally:
return{
'status_code': status_code,
'headers': {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'}
}
We have to mention the ARN of the step function in this lambda function so that it will execute the preferred Step Function.
Also this lambda function will also have an IAM Role which have the policy to execute the given step function.
Now we have to just add a trigger in S3 bucket on your preferred event, and configured this lambda function in the event trigger of the S3 bucket.
The above python code will get the event on lambda function and by using boto3 we will pass this event to the step function in the input field. The event provide the data in JSON format and the Step function will except input only in JSON.
You Step function invoke is now created use it for the event trigger as per your Object call need.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shubham Jain
Shubham is a RedHat Certified System Administrator. He is having interest in learning new open-source technologies. He has a hobby of building PCs.