title: How to find ami id using shell script
body: <p>How to find ami id using shell script </p>
  
  <p>My Script :</p>
  
  <pre><code>for i in $(cat /tmp/amidel.txt); do
          echo "Image ID of instance which needed to be Deregistered is $i ";
          val1="$i"-i-*
          aws ec2 describe-images --filters "Name=name,Values=$val1" | awk '{ print  $11 }' 
  done
  </code></pre>
  
  <p>Now Issue is that "aws ec2 describe-images" returns image id for different awk value :</p>
  
  <pre><code>$ aws ec2 describe-images --filters "Name=name,Values=instance-20Aug15-i-*" |  awk '{ print  $9 }'
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code>ami-xxxx
  
  $ aws ec2 describe-images --filters "Name=name,Values=instance-18Aug15-i-*" |  awk '{ print  $11 }' 
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code>ami-xxxx
  </code></pre>
  
  <p>This is happening because third value returned by "aws ec2 describe-images" is Comment which varies for each ami image :</p>
  
  <pre><code>$ aws ec2 describe-images --filters "Name=name,Values=instance-18Aug15-i-*" 
  </code></pre>
  
  <p>Output : </p>
  
  <pre><code>IMAGES  x86_64  This is for Daily auto AMI creation xen ami-bebfb1ec    008392659736/instance-18Aug15-i-1effb6d3    machine aki-503e7402    instance-18Aug15-i-1effb6d3 008392659736    False   /dev/sda1   ebs available   paravirtual
  BLOCKDEVICEMAPPINGS /dev/sda1
  EBS True    snap-51539764   8   gp2
  BLOCKDEVICEMAPPINGS /dev/sdf
  EBS False   snap-4e95d37b   20  gp2
  </code></pre>
  
  <p>For 2nd Ami :</p>
  
  <pre><code>$ aws ec2 describe-images --filters "Name=name,Values=instance-20Aug15-i-*" 
  </code></pre>
  
  <p>Output : </p>
  
  <pre><code>IMAGES  x86_64  This is created by ami-backup.sh    xen ami-52020b00    008392659736/instance-20Aug15-i-127fb8df    machine instance-20Aug15-i-127fb8df 008392659736    False   /dev/sda1   ebs simple  available   hvm
  BLOCKDEVICEMAPPINGS /dev/sda1
  EBS True    snap-2b563aca   8   gp2
  </code></pre>
  
  <p>So please help me how parse this to get proper ami id or is there any other method to find ami id from ami name ? </p>