Shutdown your test infra and reduce cost – AWS

I hope this article should be helpful for you, if you are managing a medium or large size infra in AWS platform.

There should be a lot of services or instances that we can shutdown for many hours in a day. Consider your production infra contains 10 large type EC2 instances and your organisation have 2 QA and 1 test environment of exactly same number of instances and services like your running production infra.

We can not stop the productions tings to save money. However, if you want to reduce your monthly billing, you can do something with your QA and Test infra.

You can shutdown QA and Test infra if nobody is working on that infra.

One important thing that you need to keep in mind before going to stop your QA/Test EC2 instances is about the public IP address assigned to your instances. The public IP address will change on every machine reboot/restart if you do not have an elastic IP address associated with your EC2 instances. If you need to preserve those IP addresses, please allocate and assign Elastic IP address for those machine. That issue fixed!!

Stopped instances and its cost!

Of-course, stopped EC2 instances do cost a little bit. The EBS [Elastic Bloc Storage] attached to your instance is reserved for that instance, so it should costs.

See what AWS experts says:

Why am I being charged for EBS when all my instances are stopped?

Issue

I’ve stopped all my Amazon Elastic Compute Cloud (Amazon EC2) instances, but I’m still receiving charges for Amazon Elastic Block Store (Amazon EBS) storage. How can I stop EBS charges accruing on my account for instances I’m not using?

Resolution

You are charged for EBS storage for the amount of storage provisioned to your account, measured in “gigabyte-months.” EC2 instances accrue charges only while they’re running, but EBS volumes attached to instances continue to retain information and accrue charges, even when the instance is stopped. For more information about how EBS is billed, see Amazon EBS Pricing.

To stop EBS-related charges, delete EBS volumes and EBS snapshots you don’t need.

EBS snapshots are billed at a lower rate than active EBS volumes are. You can minimize your EBS charges but retain the information stored in EBS for later use by creating a snapshot of the volume as a backup and then deleting the active volume. Later, when you need the information stored in the snapshot, you can restore the EBS volume from the snapshot for use with your infrastructure.

 

How I automate this stop/start process? I have more that 50 instances running on my test infra..

The command-line tool “aws-cli” can help you on this a lot. Selecting a 50 instances from AWS console and doing stop/start operation daily is not a good practice.

  • Install AWS-CLI on your local machine or on one of your instance/server.
  • Create an IAM user with EC2 access policy attached.
  • Configure aws on that machine where you are going to create code for automating this task.

How to install AWS command line interface (awscli) on Linux?

AWS supports command line interface tool. It’s package name is awscli. The package awscli is available in commonly using package manager like YUM, APT, APT-GET etc..

This is not recommended to install using YUM or APT. The default package managers may not have the latest version. This is the reason.

We can simply install awscli using pip. Pip is a package management system used to install and manage software packages written in Python.

What is the command to manage EC2 instances from command line?

aws ec2 stop-instances --instance-ids <instance-id>

aws ec2 start-instances --instance-ids <instance-id>

That’s it dude. Create bash script to stop/start your instances. If you have a fixed timing stop and start procedure, execute it with the help of cron.

Sample code:

#!/bin/bash

# This will help to stop and start ec2 instances from command line.

echo "Enter state [stop/start]"
read state

aws ec2 $state-instances --instance-ids i-0f826a4605c2d3f02
aws ec2 $state-instances --instance-ids i-04667a8a3ebe47aad

Add all of your instances and you’re done!!

 

Post navigation

Arunlal Ashok

Linux Systems Architect at Endurance International Group. Linux lover. Like to play on Linux console. I started this blog to share and discuss Linux thoughts.

Always happy for an open discussion! Write to arun (@) crybit (dot) com. Check about me for more details. About this blog and our strong members, check The team CryBit.com

3 thoughts on “Shutdown your test infra and reduce cost – AWS

Leave a Reply

Your email address will not be published. Required fields are marked *