Using AWS Systems Manager to save money with automation



Hi everyone !

How u guys doing today ?

We all know very well cloud computing has a lot of benefits but, we should also say that when you get your bill, you may be surprised if you are not enough diligent and organised. Remember, you pay for what you use, for as long as you use it.

I will bring up a real case a friend of mine was tellling me the other day. He works for an university in Canada and a lot of users are professors. They do a lot of research and as a result, they need multiple machines and , many times, very robust machines. In the cloud world, this can cost a lot of money. As we know, you pay for what you use.
Can you imagine how the bill can go high if you don't manage those machines very well ?

He was telling me how surprised they were when, a few times,  they got the bill and they found out, many machines had been let running when there was no need and as a result, the bill was insanely high.

Well, in this case, systems manager can help us out. We can set up an automation in such a way that a given group of machines will be shut down on a schedule.

For instance, let's say these professors only work from Monday to Friday, of course, you are going to discuss and both parties have to agree, but the point is, you can set up a maintenance window where these machines will be shut down every Friday at 8PM, for instance. By doing this, you make sure they will remain off over the weekend and not incur any extra cost.

I will show you today how to do that.

Here is the scenario :

I got 4 windows servers for this example.
My goal is to shut them off every Friday at 8 PM. No manual intervention should be needed.

Voila ! Go ahead and figure it out!

Of course you have more than one way of doing this but, I will leverage AWS Systems Manager for this.

Here is what you are going to need to make this happen :

  • Have SSM agent installed on those servers;
  • Tag the servers properly. I will use "Owner: Professor" to identify the targeted servers;
  • Have a document created to perform the shutdown action;
  • Setup a maintenance window that will take place every Friday at 8PM to execute the shutdown command on a group of servers.                                                                                                                                                                                                                                                        Open the console and go to Services>type "Systems"> then pick Systems Manager                                                             
  Scroll all the way down and click on "Documents"


 Click on create "command or session"

Name the document with a name of your choice and pick "Command document" as document type

Scroll down, switch to yaml and type the content below (this issues a powershell command to stop the machine. It only works if target machine is a windows) and finally hit "Create Document"


    Click on "Owned by me"and you will find the document in there: (shutdown_prof_Servers)


Now you have the document. You need now the resource group that will be your target for this job.

Go to AWS Systems Manager > Resource Groups > Create Resource Groups



Pick Tag based, then EC2 Instance and in tag you do Owner:Professor (this is for my example), fill in the group details and hit create group.




These are my 4 servers that need to be shut down.


Here is how you tag them.


Here are they after being tagged.



If you check the resource group you have created (Professors_Machines) you can verify it has 4 members.



Alright, the only thing missing now is the maintenance window.

Go to AWS Systems Manager > Maintenance Window > Create maintenance window


Fill the details in.


Set the schedule as per your need. In my case, it will run every Friday, for 1 hour.







Once you have created a maintenance window, you need to set up a task and the target.
In my case, need to register a run command task

Fill that in


Look for the document you created earlier (shutdown_prof_servers)



This is the script that will run each time this maintenance window kicks in.

Same idea for target.



Remember you've tagged your servers that were after added into a resource group. Now, it's time to pick them up.




Here you can determine how many servers this task can run on simultaneosly and the error threshold that will make this script stop running.


Do not forget to set up an IAM service role. This is required because Systems Manager needs some access on EC2 to perform the actions.


If you want, you can enable writting on S3, cloudwatch and send SNS notifications.



Then you have it.  This is a summary of what this maintenance window will do.


Alright, if you've come to this point, you just need to wait for the task to kick in. The history tab will show you whether the maintenance window was successfully executed, which was, in my case.


As you can see, my 4 servers have been shut down.


And, from within my maintenance window I can have more details.


There you go folks !

I hope you have found this useful. Thanks for reading it.


Comments

Popular posts from this blog

Understanding Azure AD Device Management

Certification or Degree ??

What if I am asked to provide last time a particular attribute was modified in Active Directory ?