Posts Tagged ‘aws’

AWS’s S3 outage was so bad Amazon couldn’t get into its own dashboard to warn the world. https://www.theregister.co.uk/2017/03/01/aws_s3_outage/

Advertisements

To get AMI ID with tags

aws ec2 describe-images –filter Name=tag:amitype,Values=autoscale –output text | grep -i ami | awk ‘{ print  $5, $7 }’ | awk ‘NR==1{print $1}’

Modify the “Name=tag:amitype,Values=autoscale” Section with your own tags.

Find Snapshots associated with an AMI

aws ec2 describe-images –image-ids <amiid> –output text –query Images[*].BlockDeviceMappings[*].Ebs.SnapshotId’

Replace “<amiid>” with the AMI ID you need.

To Deregister an AMI

aws ec2 deregister-image –image-id <amiid>

Replace “<amiid>” with the AMI ID you need to deregister.

To remove a snapshot 

aws ec2 delete-snapshot –snapshot-id <snapshot_id>

Replace “<snapshot_id>” with the Snapshot ID you need to remove.

 

Bash script to remove AMI’s older than 7 days and Snapshots associated with this AMI’s – link

 

#!/bin/bash
amiid=`aws ec2 describe-images –filter Name=tag:amitype,Values=autoscale –output text | grep -i ami | awk ‘{ print $5, $7 }’ | awk ‘NR==1{print $1}’`
time=`aws ec2 describe-images –image-ids $amiid –query ‘Images[*].[ImageId,CreationDate]’ –output text | awk ‘{ print $2 }’`

time1=`echo $time | head -c 10`

compdate=`date ‘+%y-%m-%d’ –date=’7 days ago’`

flag=$(echo $(( ( $(date -ud $time1 +’%s’) – $(date -ud $compdate +’%s’) )/60/60/24 )))
echo “flag=$flag”
if [[ “$flag” -gt 7 ]];
then
my_array=( $(aws ec2 describe-images –image-ids $amiid –output text –query ‘Images[*].BlockDeviceMappings[*].Ebs.SnapshotId’) )
echo “my_array=$my_array”
echo “older than 7 days”;

(more…)