AWS cli のインストールを行うスクリプト

#!/bin/sh
DEFAULT_REGION="ap-northeast-1"
SCRIPT_PATH=`dirname $0`

/usr/bin/aws --version
if [ $? != 0 ]; then
  wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
  python get-pip.py
  pip --help
  if [ $? == 0 ]; then
    pip install awscli
    pip install --upgrade awscli
  else
    echo Error at installing python.
    exit 1
  fi
fi

if [ $? == 0 ]; then
  /usr/bin/aws --version
else
  echo Error at installing awscli.
  exit 1
fi

/usr/bin/aws iam get-user > /dev/null 2>&1
if [ $? != 0 ]; then
  echo "You must set credentials for AWS. You can skip region and output."
  /usr/bin/aws configure
  REGION_CONFIGURE=`grep "region" ~/.aws/config | wc -l`
  if [ "${REGION_CONFIGURE}" = 0 ]; then
    echo "region=${DEFAULT_REGION}" >> ~/.aws/config
    AZ=`curl -q http://169.254.169.254/latest/meta-data/placement/availability-zone 2> /dev/null`
    REGION=`/usr/bin/aws --output text ec2 describe-availability-zones | grep "${AZ}" | sed "s/\s\+/ /g" | cut -f2 -d' '`
    if [ "${AZ}" != "" ] && [[ ${AZ} == ${REGION}* ]]; then
      sed -i -e "s/region=${DEFAULT_REGION}/region=${REGION}/g" ~/.aws/config
      echo "Default region set to ${REGION}"
    else
      echo "Default region set to ${DEFAULT_REGION}"
    fi
  fi

  OUTPUT_CONFIGURE=`grep "output" ~/.aws/config | wc -l`
  if [ "${OUTPUT_CONFIGURE}" = 0 ]; then
    echo "output=text" >> ~/.aws/config
    echo "Default output set to text"
  fi
fi