Using AWS EC2 CLI - part 2
Script for checking VMs status:
https://github.com/openterprise/scripts/blob/master/aws-status.sh
Example user data file script:
https://github.com/openterprise/scripts/blob/master/aws-user-data.sh
#create VM centos
aws ec2 run-instances --image-id ami-18f8df7d --security-group-ids sg-ID --subnet-id subnet-ID --count 1 --instance-type t2.micro --key-name KEY-NAME --user-data file://aws-user-data.sh --block-device-mappings 'DeviceName=/dev/sda1,Ebs={VolumeSize=10,DeleteOnTermination=false}' --query 'Instances[0].InstanceId'
#stop ALL instances (only those are running)
aws ec2 stop-instances --instance-ids `aws ec2 describe-instances --filters Name=instance-state-name,Values=running | jq .Reservations[].Instances[].InstanceId | sed 's/"//g'`
#start ALL instances (only those are stopped)
aws ec2 start-instances --instance-ids `aws ec2 describe-instances --filters Name=instance-state-name,Values=stopped | jq .Reservations[].Instances[].InstanceId | sed 's/"//g'`
#delete ALL volumes which are not in use (not attached)
aws ec2 describe-volumes --filters Name=status,Values=available | jq .Volumes[].VolumeId | sed 's/"//g' | xargs -L1 -I {} aws ec2 delete-volume --volume-id {}
#terminate ALL instances (destroys it)
aws ec2 terminate-instances --instance-ids `aws ec2 describe-instances | jq .Reservations[].Instances[].InstanceId | sed 's/"//g'`