AWS CLIとbashでLaunch more like thisのようなことをする

AWSCMD="aws --output=text"
INSTANCE_DETAIL=`${AWSCMD} ec2 describe-instances --instance-ids ${INSTANCE_ID} --query 'Reservations[].Instances[].{INSTANCEID:InstanceId,INSTANCETYPE:InstanceType,KEYNAME:KeyName,SUBNETID:SubnetId,SECURITYGROUPS:SecurityGroups,PLACEMENT:Placement}'`

RAW_PLACEMENT=`echo "${INSTANCE_DETAIL}" | egrep "^PLACEMENT" | head -n1`
AVAILABILITYZONE=`echo "${RAW_PLACEMENT}" | awk -F'\t' '{print $2}'`
GROUPNAME=`echo "${RAW_PLACEMENT}" | awk -F'\t' '{print $3}'`
TENANCY=`echo "${RAW_PLACEMENT}" | awk -F'\t' '{print $4}'`
HOSTID=`echo "${RAW_PLACEMENT}" | egrep "^PLACEMENT" | awk -F'\t' '{print $5}'`
AFFINITY=`echo "${RAW_PLACEMENT}" | egrep "^PLACEMENT" | awk -F'\t' '{print $6}'`
PLACEMENT="AvailabilityZone=${AVAILABILITYZONE},GroupName=${GROUPNAME},Tenancy=${TENANCY},HostId=${HOSTID},Affinity=${AFFINITY}"
PLACEMENT=`echo "${PLACEMENT}" | sed -e "s/[^=,]\+=,//g" | sed -e "s/,[^=,]\+=$//g"`

SECURITYGROUPS=`echo "${INSTANCE_DETAIL}" | egrep "^SECURITYGROUPS" | awk -F'\t' '{print $2}' | tr '\n' ' '`

INSTANCETYPE=`echo "${INSTANCE_DETAIL}" | egrep "^${INSTANCE_ID}" | awk -F'\t' '{print $2}'`
KEYNAME=`echo "${INSTANCE_DETAIL}" | egrep "^${INSTANCE_ID}" | awk -F'\t' '{print $3}'`
SUBNETID=`echo "${INSTANCE_DETAIL}" | egrep "^${INSTANCE_ID}" | awk -F'\t' '{print $4}'`

${AWSCMD} ec2 run-instances --image-id ${IMAGE_NAME} --instance-type ${INSTANCETYPE} --key-name ${KEYNAME} --security-group-ids ${SECURITYGROUPS} --subnet-id ${SUBNETID} --placement ${PLACEMENT}