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…
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:
in the us-east-1 (a.k.a North Virginia) region and power them off
- 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