snippet to add aws volumes and instances (rough)

just a snippet of something i was working on to add volumes to instances in aws using the amazon ec2 ami / api tools.

very rough draft.

TMP_VOL_OUT="$(basename $0).$$.tmp"                                                                                         
TMP_INST_OUT="$(basename $0).$$.tmp"                                                                                        
                                                                                                                            
## input the number of volumes to create-                                                                                   
echo "How many volumes to create ?: \c"                                                                                     
read VOLNUM                                                                                                                 
echo "Creating ${VOLNUM} volumes"                                                                                           
                                                                                                                            
## input size of volume                                                                                                     
echo "What do you want the size of the vols to be (in gb) ?: \c"                                                            
read VOLSIZE                                                                                                                                                                                                                                                                                                                  
echo "Creating ${VOLSIZE}gb volumes"                                                                                        
                                                                                                                            
## select availability zone                                                                                                 
echo "What zone dp you want the volumes to be in (select from list below by inputing zone - case sensitive) : \c"
ec2-describe-availability-zones | awk '{print $2}'                                                                          
read ZONE
echo "Using ${ZONE} as zone"                                                                                                
                                                                                                                            
## create the volumes                                                                                                       
x=0                                                                                                                         
while [ $x -lt ${VOLNUM} ];                                                                                                 
    do                                                                                                                      
        x=`expr $x + 1`                                                                                                     
        ec2-create-volume -s ${VOLSIZE} -z ${ZONE} >> ${TMP_VOL_OUT}                                                        
    done                                                                                                                    
                                                                                                                            
## create instance                                                                                                          
# first show images / ami's to use as image                                                                                 
echo "Select default ami to mimic for new instance (just input from column one - case sensitive) : \c"                      
ec2-describe-images | egrep IMAGE | awk '{print $2, $3}'                                                                    
read DEF_AMI                                                                                                                
echo "You selected ${DEF_AMI}"                                                                                              
                                                                                                                            
# now select instance type                                                                                                  
echo "Select instance type : \c"                                                                                            
echo "m1.small | m1.large | m1.xlarge | c1.medium | c1.xlarge | m2.xlarge | m2.2xlarge | m2.4xlarge | cc1.4xlarge | cg1.4xlarge | t1.micro"
read INST_TYPE                                                                                                              
echo "You selected ${INST_TYPE}"                                                                                            
                                                                                                                            
# now create the instance
ec2-run-instances ${DEF_AMI} -k pissedoffadmins --availability-zone ${ZONE} -t ${INST_TYPE} >> ${TMP_INST_OUT}
                                                                                                                            
## attach the volumes
x=0
while [ $x -lt ${VOLNUM} ];
    do
        x=`expr $x + 1`
        VOLNAME=`sed -n `echo $x`p ${TMP_VOL_OUT} | awk '{print $2}'`
        INSTNAME=`sed -n `echo $x`p ${TMP_INST_OUT} | awk '{print $2}'`                                                     
        ec2-attach-volume ${VOLNAME} -i ${INSTNAME} -d /dev/sdh$x
    done
«
»
  • ## attach the volumes
    x=0
    while [ $x -lt ${VOLNUM} ];
    do
    x=`expr $x + 1`
    VOLNAME=`sed -n `echo $x`p ${TMP_VOL_OUT} | awk ‘{print $2}’`
    INSTNAME=`sed -n `echo $x`p ${TMP_INST_OUT} | awk ‘{print $2}’`
    ec2-attach-volume ${VOLNAME} -i ${INSTNAME} -d /dev/sdh$x
    done

    giving errors


Leave a Reply

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