Control your spendings in AWS

Hi Everyone,

In my previous blog post – where’s my money??? – I started discussing the shock and awe cloud newbies encounter once they start dipping their toes in the cloud computing pool. If you haven’t done so already, I suggest you start by reading this first.

So hopefully by now you begin to understand what tags are (in short, meta-data) and I assume you’re wondering how you can use them…

Manage your Instances

So, let’s start with the basics – shut down your EC2 instances when you don’t use them.

I find this to be the first, easiest and most useful tip to start saving money when using cloud computing resources.

Since the majority of cloud users start by spinning up instances (similar to virtual machines), this is usually the place where spending starts to add up.

If you didn’t know it by now, you’re paying for your EC2 instance as long as they’re turned on. Head over to EC2 Instance Pricing to get an idea on costs…

So, now you know. Great. But wait, what if you have 10, 20, 100 instances you ask? Should you go to the web console and turn them off one-by-one? Nah…

Automate It!

Open up your Linux terminal (you are using linux, right?) and put down this code:


Instances=$(aws ec2 describe-instances --region us-east-1 --filters "Name=tag:first_tag,Values=first_value,Name=tag:second_tag,Values=second_value" --query 'Reservations[*].Instances[*].[InstanceId]' --output text)

for Instance in $Instances;
do
echo "Turning off Instance: $Instance" ;
aws ec2 stop-instances --region us-east-1 --instance-ids $Instance ;
done

This script will find all instances that have the following key:value tags:

  • first_tag:first_value
  • second_tag:second_value

in the us-east-1 (a.k.a North Virginia) region and power them off

Script notes

  • You can easily modify the query and add / remove tags to meet your needs. If you need help with syntax, let me know!
  • If your instances are in a different region, simply change the us-east-1 value to the region that matches your value. If you need to get a list of available regions, run:
  • aws ec2 describe-regions

  • Running the script requires you to install & configure the AWS cli. Details here

  • Windows users – this can be done fairly easily by using batch or powershell scripts. If you need help, let me know!

Optional – Schedule It!

If you know your Linux and / or AWS, you might want to schedule the above script to run at specific times. It’s a bit out of scope for today, but I suggest you take a look at AWS Lambda and Linux crontab to discover some ways you can automate your scripts

Happy savings.

Advertisements

One thought on “Control your spendings in AWS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s