Executing Parallel Tasks In Step Functions

In this post, we’ll see how we can execute parallel tasks in Step Function.

Our scenario will be to execute a step function when an instance gets into running state, which will first create AMI of the instance and then as parallel tasks, will attach an IAM role and security group to the instance.

Step 1: Create an IAM role for our Lambda functions with AmazonEC2FullAccess and AWSLambdaBasicExecutionRole permission. Also, add following inline policy

Step 2: Create 3 functions,EC2_AMI,EC2_SG,EC2_IAM, with Python3.7 runtime and 2 minutes timeout.Use the role which we created in Step 1.

Code for EC2_AMI function:

Code for EC2_IAM function: Specify Instance profile ARN and name in the code.

Code for EC2_SG function: Replace sg-xyz with your security group.

Step 3: Create a Step Function with following definition. Replace <account_id> with your account id.

Step 4: Create a CloudWatch event rule with the following Event Pattern.

Specify your Step Function as Target and make sure the IAM role you are using for Step Function has the permission to invoke our 3 lambda functions.

That’s all from a setup point of view.

Now try launching an instance and you’ll see AMI gets created for that instance and designated IAM role and security group is also attached to this instance.

This gives us a fair bit of idea about using the Step function and you can use a similar setup for tasks like the text to speech conversion etc.

👋 Join us today !!

️Follow us on LinkedIn, Twitter, Facebook, and Instagram

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store