How to turn on Long Polling on an AWS SQS Queue

The following code snippet will allow you to configure an AWS SQS queue for long polling using the ReceiveMessageWaitTimeSeconds attribute. For more information about Amazon long polling, see here:

http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html

// 5. Set Queue attributes
// The API Version 2012-11-05 of Amazon SQS provides support for long polling. (.net sdk v2)
// In the AWS console, you can verify in "Receive Message Wait Time" value of the queue
SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest();
List<Amazon.SQS.Model.Attribute> attributes = new List<Amazon.SQS.Model.Attribute>();
Amazon.SQS.Model.Attribute attribute = new Amazon.SQS.Model.Attribute();
attribute.Name = "ReceiveMessageWaitTimeSeconds";
attribute.Value = "20"; // 0 to 20 Seconds, default is 0
attributes.Add(attribute);
setQueueAttributesRequest.QueueUrl = queueUrl;
setQueueAttributesRequest.Attribute = attributes;
_sqs.SetQueueAttributes(setQueueAttributesRequest);
Advertisements

How to create an Amazon AWS SNS Topic and an SQS Queue that subscribes to it

Creating powerful cloud-based systems in C# and the .Net framework are possible for a long time now. Architecting a messages based cloud-computing solution that can handle millions of requests per day is not an easy undertaking but much easier when you use Amazon AWS services.

I recently needed to create an Amazon AWS SQS queue programmatically that subscribes to an SNS Topic that was also created programmatically. I could not find any examples in C# so I decided to post this code from one of my cloud-computing systems that I had designed and hope others find it useful. This particular code was used to test the dynamic nature of a messages based backend that can expand and contract the number of SQS queues based on how many active EC2 instances (nodes) in the cluster are available.

This code also shows how to create a policy for the SQS queue so that it will receive the messages from the SNS service. If you do not set a correct policy, the queue won’t receive any messages nor any exceptions are thrown.

Declaration code:

        private AmazonSimpleNotificationServiceClient _sns;
        private const string SNS_TOPIC = "YourTopicHere";
        private string _AWSSNSArn;

Core C# code:

            // 1. Create an Amazon SNS topic
            AmazonSimpleNotificationService sns = new AmazonSimpleNotificationServiceClient(RegionEndpoint.USWest1);
            _AWSSNSArn = sns.CreateTopic(new CreateTopicRequest
            {
                Name = SNS_TOPIC
            }).CreateTopicResult.TopicArn;

            // 2. Create the Amazon SQS In-Queue, will ignore it if it already exists
            AmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient(RegionEndpoint.USWest1);
            CreateQueueRequest sqsRequest = new CreateQueueRequest();
            sqsRequest.QueueName = General.IpAddressAWSFriendly;
            string queueUrl = sqs.CreateQueue(sqsRequest).CreateQueueResult.QueueUrl;
            GetQueueAttributesRequest getQueueAttributesRequest = new GetQueueAttributesRequest();
            List list = new List();
            list.Add("QueueArn");
            getQueueAttributesRequest.AttributeName = list;
            getQueueAttributesRequest.QueueUrl = queueUrl;
            GetQueueAttributesResponse response = sqs.GetQueueAttributes(getQueueAttributesRequest);
            string queueArn = response.GetQueueAttributesResult.QueueARN;

            // 3. Configure the Amazon SNS topic to publish to the SQS queue
            sns.Subscribe(new SubscribeRequest
            {
                TopicArn = _AWSSNSArn,
                Protocol = "sqs",
                Endpoint = queueArn
            });

            // 4. Set the queue policy to allow SNS to publish messages
            ActionIdentifier[] actions = new ActionIdentifier[2];
            actions[0] = SQSActionIdentifiers.SendMessage;
            actions[1] = SQSActionIdentifiers.ReceiveMessage;
            Policy sqsPolicy = new Policy()
                .WithStatements(new Statement(Statement.StatementEffect.Allow)
                                    .WithPrincipals(Principal.AllUsers)
                                    .WithResources(new Resource(queueArn))
                                    .WithConditions(ConditionFactory.NewSourceArnCondition(_AWSSNSArn))
                                    .WithActionIdentifiers(actions));
            SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest();
            List attributes = new List();
            Amazon.SQS.Model.Attribute attribute = new Amazon.SQS.Model.Attribute();
            attribute.Name = "Policy";
            attribute.Value = sqsPolicy.ToJson();
            attributes.Add(attribute);
            setQueueAttributesRequest.QueueUrl = queueUrl;
            setQueueAttributesRequest.Attribute = attributes;
            sqs.SetQueueAttributes(setQueueAttributesRequest);

Amazon AWS wins contract for CIA to host on AWS’ Cloud – Yeah!

I have been working with Amazon AWS Cloud web services since its beta introduction back in 2006 and still totally love it and use it today. When I heard about the news that Amazon won the contract to host CIA’s operations in the cloud, I was super excited and another confirmation that NO ONE knows cloud computing better than Amazon, not even IBM, the sore looser in the bidding process.

When it comes to service offerings and the rich capabilities and great SDK’s offered by Amazon AWS, Microsoft’s Azure, Google, IBM, and others do not come even close. Period!!!

http://techcrunch.com/2013/10/07/amazon-web-services-wins-again-in-battle-to-build-the-cia-and-nsa-cloud/

To get started, go to http://aws.amazon.com