AWS Auto Scaling Groups

Today I want to show you how to start with AWS Auto Scaling Groups. When we need Auto Scaling Groups? It can be a question for a whole, another post. 😀

I have prepared an app on Spring Boot which pull arguments from SQS, calculates prime number bigger than that argument and finally puts the result to the S3 (Simple Storage Service). Everything works quickly on small numbers. When it comes to bigger input (e.g. 10 000 000) it takes really long time. Server is calculating and we are waiting, next number is waiting…

And then… our Scaling Group says – it is a good moment to add some new resources and creates a next server. After all, when there is nothing to calculate, Scaling Group kills them.


First of all, when we want to use AWS in our Maven project we have to add some dependencies.

        <!-- AWS SQS CORE -->

        <!-- AWS SDK CORE -->

        <!-- AWS MESSAGE SQS -->

        <!-- JMS API -->

And plugins to build an apps from maven. They help us later.


Now we are going to write our code. There are many ways to provide credentials. Below is one of them (not the best I sure).

BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);

Then we can connect to our created queue on SQS.

SQSConnectionFactory connectionFactory = 
new SQSConnectionFactory(new ProviderConfiguration(),
		      .withCredentials(new AWSStaticCredentialsProvider(awsCreds))

SQSConnection connection = connectionFactory.createConnection();

I said that we will use S3 to hold the results so we need to define it too.

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
	.withCredentials(new bAWSStaticCredentialsProvider(awsCreds))

Finally, it is a good time to create a session to queue and start to receiving the messages.

		Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		String QUEUE_NAME = "my-example-queue";
		MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE_NAME));


		receiveMessages(s3Client, consumer);

		System.out.println("Connection closed");

Below the receiveMessages method with a basic method which calculates prime number.

private static void receiveMessages(AmazonS3 s3Client, MessageConsumer consumer) {
try {
	while (true) {
		System.out.println("Waiting for messages");
		// Wait 1 minute for a message
		Message message = consumer.receive(TimeUnit.DAYS.toMillis(1));
		if (message == null) {
			System.out.println("Shutting down after 1 minute of silence");

		if (message.propertyExists("n")) {
			int prime = calculatePrimeNumber(2, 0, message.getIntProperty("n"));
			System.out.println("Wyliczone: " + prime);
			s3Client.putObject("lista-10-odpowiedzi", "primeNumber_" + message.getIntProperty("n"), "For n: " + message.getIntProperty("n") + " prime number is " + prime);
		System.out.println("Acknowledged message " + message.getJMSMessageID());
} catch (JMSException e) {
	System.err.println("Error receiving from SQS: " + e.getMessage());

private static int calculatePrimeNumber(int number, int i, int n) {
	while (true) {
		if (isPrime(number)) ++i;
		if (i > n && isPrime(number)) break;
	return number;

static boolean isPrime(int n) {
	for (int i = 2; i <= Math.sqrt(n); i++) {
		if (n % i == 0)
			return false;
	return true;

We can test our program by sending a example message with parameter n.

We can see the result in the S3 backet.

Deploy application on AWS

Ok. If everything looks good we can go through the next steps. Now we are going to deploy our application on AWS. What is more, we have to add our application as a system service. The reason is that, when new servers will be starting, then our app should be started on them. After that it is necessary to create AMI which future servers will be using. We have much work to do so lets go!

I have pushed my code to GitHub. So on the server I just need to install GIT, Maven and Java to compile and run it.

started application on cloud
$ cd /etc/systemd/system
$ touch example-app.service
$ vim example-app.service
Description = SQS App
After =

ExecStart = /home/ubuntu/sqs-example/     
RestartSec = 5                                            

WantedBy =

Then go to the project folder and create file with content as below.

cd /home/ubuntu/sqs-example && mvn exec:java

And check if service is starting application correctly.

$ systemctl restart example-app.service
$ systemctl status example-app.service

Now we can create AMI from current instance.

Create Auto Scaling Group

Applications is ready, AMI of instance too. Finally we can focus on creating auto scalling group.

I have created a launch configurations with before created AMI.

Click „Create Auto Scaling Group” button then fill group name, choose network and subnet.

In the next step I set max and min number of instances. What is more there we can set scaling policy. I set when CPU usage 50% then new server should be added.

On list of instances we see our one server from that auto scaling group.

Test it!

Uff… 🙂 Eventually, check how our Scaling Group works. Send via SQS some really big numbers to calculate and wait for a result. 10 000 000, 10 000 001, 10 000 002…

After a few minutes we can see new server with pending status.

And mooooore…

In S3 exists the results of the calculation. After a while servers are automatically terminating.

As you can see it takes time to scale servers, so in our case (calculation) it is not a good solution but it wasn’t be a goal in this post.