Resources



Sample Code and Libraries

Sample application to get started with Amazon SQS and Amazon EC2

Click for a printer friendly version of this document Printer Friendly Save to del.icio.us
 

To help you get started using Amazon EC2 and Amazon SQS together, we’ve put together a simple sample application. It demonstrates the basic concepts for using Amazon SQS to quickly and easily build Amazon EC2 applications. It shows a simple but fairly common scenario, where Amazon SQS dispatches jobs to an Amazon EC2 instance. The source code for the sample application is included so you can understand how it’s actually implemented or jumpstart your own application by extending our application

Submitted By: Madhu@AWS  
AWS Products Used: Amazon SQS, Amazon EC2
Language(s): C#
License: Apache License 2.0

The figure and discussion below explain the major components of the sample application and the interactions between them.

Figure 1: Shows a user communicating asynchronously with an EC2 instance using Amazon SQS queues.
There are three major components to the sample application:

  • A server application implemented as an Amazon Machine Instance (AMI) that runs in an Amazon EC2 instance. The server application simply reverses an input string (i.e., “Foobar” becomes “rabooF”).
  • A client application that runs on your PC/desktop. It provides the user interface to submit the input strings and display the output strings. It also provides controls to start and stop the Amazon EC2 instance.
  • Amazon SQS queues, used to connect the client application to the running Amazon EC2 instance. It enables asynchronous, message-based communication between the client application and server application.

The client application posts jobs as messages to an Amazon SQS request queue. The server application picks up each message from the request queue and executes the job (as specified in the message). In the sample application, the job is simply to reverse the string.

The server then does two things:

  • It outputs the reversed string into the response queue
  • It deletes the message from the request queue so that it does not process the message again

The client picks up the message from the response queue and displays the result. If an error occurs while the server is processing the message, the server submits an error message to the error queue.

The turnaround time for the entire loop depends on how frequently the client application and the server application poll the queue for messages.

Prerequisites for Using the Sample Application

  • You must have an Amazon AWS account and be signed up for Amazon EC2, Amazon SQS, and Amazon S3. If you do not have an account or have not already subscribed to all three services, you can do so here.
  • The client application requires Microsoft .NET Framework version 2.0. If you don’t have it installed on your computer, you can download it here.
  • Have your AWS Access Key Id and Secret Access Key handy, as you need them to run the sample application. You can get them by going to http://amazon.com/aws, clicking Your Web Services Account, and clicking AWS Access Identifiers.

Usage Charges

Please note that when you use the sample application, you will be charged for the use of Amazon EC2 and Amazon SQS. The amount of the Amazon SQS charges depend on the number of strings you submit. The Amazon EC2 charges depend on how long you run the Amazon EC2 instance. Remember to stop the EC2 instance when you are done with the demo¾either by clicking the Stop button or by exiting the application.

Running the Sample Application

SQS_EC2_Client.exe: The client executable.
SQS_EC2_Client_source.zip: Source code for the client executable. It’s a Visual Studio Express Project .
SQS_EC2_Server_source.zip: Source code for the server application
Amazon.SQS.dll & EC2client.exe: Amazon C# Library for Amazon

  • Launch the client application (SQS_EC2_CLIENT.exe). You should see the following screen:

  • Enter your Access Key ID and Secret Access Key in the text boxes.
  • Click Start. The client application tries to do two things:
    • Connect to Amazon SQS to check for the existence of the queues. If the queues do not exist, it creates them.
    • Launch the SQS/EC2 AMI. It may take several seconds to start the SQS/EC2 AMI. During this time you will see “Instance starting” in the Instance Status box. Please wait until the status is “running”.
  • Type the string “Testing” into the Message Text box.
  • Click Submit Request.
  • You should see the reverse string “gnitseT” displayed in the Results Queue. The screen should appear as below.

  • You can have the client application automatically generate and submit strings when you click Submit Batch.
  • When you are done, click Stop to stop the EC2 instance. The EC2 instance is also automatically stopped if you exit the application.

Extending the Sample Server Application

Source code is provided for both the client and server applications. You can use the server application independent of the client application. You can also extend the server application to implement the server logic you want. For detailed instructions refer to the README document included in the SQS_EC2_Server_Source.zip download.



Related Documents
Type: AMIs SQS-EC2 Job Processor Sample

Discussion
Click to start a discussion on this document Create a New Discussion
No discussion has been created for this document.

Reviews
Create Review Write a Review
Be the first to review this.
Welcome, Guest Help
Login Login