Vertical Auto Scaling AWS EC2 instances. Slack integrations.

Serhii Kuyanov
5 min readDec 16, 2020

The title of the article speaks for itself :)

Although it is not the best practice, sometimes it can be a situation when we need to use this option of scaling, and it happened to me. Some details need to be clarified right away:

1. AWS is working to create such a service.

Here you can see the second possible implementation option, which is a little more difficult to implement. But it didn’t suit us.

2. Using my version, the downtime is one and a half minutes. It is the time from the beginning of the cycle to the complete lifting of all services.

If it does not suit you then use the standard horizontal auto-scaling.

And while you are thinking, I will begin. In this case, we will use the following AWS services:

  1. AWS EC2
  2. IAM
  3. AWS Lambda
  4. AWS SNS
  5. CloudWatch

If it is new for you, do not be sad, I will try to describe everything accurately and in details. Since you are interested in this article, I suppose you can create and manage EC2 instances, if not, then read this article.

I will be using Amazon Linux 2.

So, let’s begin.

First of all, let’s go to IAM ==> Policies ==> Create Policy ==> JSON
and fill it with this content:

After click Review policy, give it a name “ModifyInstance” and Create Policy.
We have designated them as clearly as possible and nothing more.

Now we have completed this step, we need to create a role and bind it to our policy “ModifyInstance”.

In IAM, select Role ==> Create Role ==> AWS Service lambda. Select the previously created policy and then name it “RoleModifyInstance” Great, we’re done with IAM.

Now let’s create a lambda function. We’ll use Node.js because aws-sdk seemed easier to me when interacting with it. Click to create a function, select Node.Js 12 or another, and name your function. For example “UpperInstance”.
At the very bottom, select the role you created earlier.

Add this content to Function code and change InstanceId:

After that, we need to set up “Environment variables”. Сlick edit and fill in the content we need.

To increase the server.
To reduce the server.

In “Basic settings”, change the timeout to 5 minutes.

Click “Save” and “Deploy” your lambda function.

In the same way, we create the “LowerInstance” function. Do not forget to just correctly indicate the types of instances that we need to increase and decrease.
That’s where we’re done with the function.

Well, it’s time to set up Amazon SNS service.

Go to the Topics section and create two topics named “underthreshold_25” and “underthreshold_60”.

After that, go to our topics and select “Create subscription”.

We select AWS Lambda for the protocol, and select our desired function as the endpoint. Feel free to click Create subscription and that’s it. We have two topics for reducing and increasing the server.

Now let’s navigate and configure our Cloud Watch. Select Alarm on the left and click Create Alarm ==> Select Metric ==> EC2 ==> Per-Instance Metrics ==> and select your instance named CPUUtilization metric and select metric. Here we set up as on the screen, we do not touch anything else.

Define the threshold value.
Define the threshold value.

Click next and in Configure actions in the “Send a notification to …” section, select our SNS topic. When the threshold is set to 60 select underthreshold_60. It’s the same with underthreshold_25.

Everything is very simple. Give it a name and create it.

Now it’s time to test what we configured. We can create an overload or simply publish a message on our SNS topic. If you did that, now you have m5.large instance. If we publish a message in the underthreshold_25 topic, then our function will check the server and since it will have nothing to change, it will simply end and report this in the logs. It is what we wanted.

Now let’s post the message to underthreshold_60. The function for increasing the server takes the following steps in sequence:

  1. Checks instance type
  2. Stop instance
  3. Waits for it to stop
  4. Changes instance type
  5. Launch instance

When you republish the message, nothing changes.

With such settings, an increase will occur when the last three checks were on average above 60% in 15 minutes, and a decrease if below 25% in 45 minutes. We were completely satisfied with it, and of course, you can make your own changes.

There are many articles on how to combine lambda and slack. I will not repeat them. I’ll leave a link.

We need to create a new lambda layer that will contain ‘phin’.
This article will show you how to do it perfectly.

That will definitely help you configure this.

And I’m adding the code for our case.

Also, for our convenience, the environment panel will look like this:

And this will be our result:

Thanks for your attention. Happy modifications.

--

--