Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

amazon web services - Best way to make Step Functions wait for the SQS to be empty

What would be the best way to make a step function workflow wait for an sqs topic to be empty and then continue the workflow. Is there a way to check a topics length so i can react to in within a step function workflow? And would this really be the proper way to tackle this problem ? Or would i just create a lambda that would do exactly that and trigger it in my step function workflow

Thanks in advance :)

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can build Lambda functions that uses the Lambda runtime API. Then within that Lambda function use the SQS Java API. Look at retrieving the queue attributes using this method.


Look at the following attribute.

ApproximateNumberOfMessages - Returns the approximate number of visible messages in a queue

Here is a code example that uses the SQS Java V2 API to obtain number of messages.

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GetQueueAttributes {

    public static void main(String[] args) {
        final String USAGE = "
" +
                "Usage: AddQueueTags <queueName>

" +
" +
                "  queueName - the name of the queue to which tags are applied.


        if (args.length != 1) {

        String queueName = args[0];
        SqsClient sqsClient = SqsClient.builder()

        try {
        GetQueueUrlResponse getQueueUrlResponse =

        String queueUrl = getQueueUrlResponse.queueUrl();

        // Specify the attributes to retrieve.
        List<QueueAttributeName> atts = new ArrayList();

        GetQueueAttributesRequest attributesRequest= GetQueueAttributesRequest.builder()

        GetQueueAttributesResponse response = sqsClient.getQueueAttributes(attributesRequest);

        Map<String,String> queueAtts = response.attributesAsStrings();
        for (Map.Entry<String,String> queueAtt : queueAtts.entrySet())
                System.out.println("Key = " + queueAtt.getKey() +
                        ", Value = " + queueAtt.getValue());

    } catch (SqsException e) {

What is nice is you can create different Lambda functions and then hook each Lambda function as a workflow step by using Step Functions. This is a good way to build workflows and have each step in that workflow use the AWS Java API to perform a specific AWS Service operation.

To learn how to hook in Lambda functions into a workflow, see this tutorial.

Create AWS serverless workflows by using the AWS SDK for Java

Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share