I tried setting up AWS Step Function with SQS

I tried setting up an AWS Step Function that sends a message to SQS . This is using AWS direct SQS integration as part of a Step Function Task. Step functions helps you model your workflows as state machines, written in json.

Let's get started!

First thing we have to do is to make a SQS queue with all the default settings.

After creating the queue, make sure to copy the URL and save it in your notepad. We will be needing it in future steps.

Let's head over to the step function and create a step machine.

Next, select 'Write your workflow in code' and give your state machine a name. Click next.

Now, let's write the code under code section.

{
  "Comment": "Transaction Processor State Machine using SQS",
  "StartAt": "ProcessTransaction",
  "States": {
    "ProcessTransaction": {
      "Type": "Pass",
      "Next": "BroadcastToSQS"
    },
    "BroadcastToSQS": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sqs:sendMessage",
      "Parameters":{
        "QueueUrl":"https://sqs.ap-northeast-1.amazonaws.com/974737090971/SQSFromStepFunction",
        "MessageBody":{
          "TransactionId.$":"$.TransactionId",
          "Type.$":"$.Type"
        }
      },
      "End": true
    }
  }
}

Let's take a quick look into the code. So we have a transaction processor step; this is just a pass but it is the pointer to the next step which is BroadcastToSQS. BroadcastToSQS is a task and it's resource is SQS sendMessage API. The parameters which we are passing into the resource has to be the queueURL. This URL is the one we saved from SQS in the start. At the end there is a message body which we are actually sending to SQS. So our key is the transaction ID and the value is type.

So let's paste this code in the left hand side.

Click next and click on 'Create new role'. Give it a name.

We can leave the other tabs as it is and click on 'Create state machine'.

Our state machine is getting created now. Let's do a test run now to see if it runs correctly. For that, click on 'Start Execution'.

Next, give a sample input and paste it in the input section.

After clicking on start execution, it will be succeeded. You can see the confirmation message as 200 in the output.

Next, let's verify if the message is actually in the queue. For that, we have to go back to SQS and poll for messages.

And that's it. We got exactly what we wanted. We can see our message in SQS.

Thank you so much for reading.

Happy learning!