Configuring Tags
A tag is a key-value pair.
For instance, name = my_server
Using tags you can create different categories of your EC2 machines. Let’s imagine a scenario where you have 100 production machines and 100 development machine, you will see in your dashboard 200 machines.
Now you shutsown all of your developement machines at the end of the day and start them tomorrow.
Without tags, you will probably stop the develomement machines manually, one by one but if you want to do things faster, you can tag all of your develoepment machines with “env = dev” for example. After that, all what you have to do is
aws ec2 stop-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:env,Values=dev" --query 'Reservations[].Instances[].InstanceId' --output text`
Note: The below command is using AWS CLI. The CLI should be installed and configured before using it.
Let’s examine each command apart. The embedded command will list the instance ids having the tag “env=dev”:
aws ec2 stop-instances --instance-ids `aws ec2 describe-instances --filters "Name=tag:env,Values=dev" --query 'Reservations[].Instances[].InstanceId' --output text`
The second one will simply kill these machines:
aws ec2 describe-instances --filters "Name=tag:env,Values=dev" --query 'Reservations[].Instances[].InstanceId' --output text aws ec2 stop-instances --instance-ids <instance_id_1> <instance_id_2> .. <instance_id_n>