AWS elasticbeanstalk automation example



#!/bin/bash

PowerOnDuringWeekDaysAt="08:30"
PowerOffDuringWeekDaysAt="18:30"
CurrentTime=$(TZ=Europe/London date | sed 's/.* \([0-9]*:[0-9]*\):[0-9]*.*/\1/')
WeekDay=`date +%u` # 1 is Monday - 7 is Sunday
ListInstances=$(aws elasticbeanstalk describe-instances-health --environment-name my-aws-elastic-beanstalk-environment | jq -r '.InstanceHealthList[].InstanceId')
PowerOn=$(aws elasticbeanstalk update-environment --environment-name my-aws-elastic-beanstalk-environment --option-settings file://poweron.json)
PowerOff=$(aws elasticbeanstalk update-environment --environment-name my-aws-elastic-beanstalk-environment --option-settings file://poweroff.json)

if (($WeekDay >= 1 && $WeekDay <= 5))
then

  echo "Weekdays Schedule"
  echo "-----------------"

  if [[ "$CurrentTime" > $PowerOnDuringWeekDaysAt ]] && [[ "$CurrentTime" < $PowerOffDuringWeekDaysAt ]]
  then

    echo "Schedule status: Power ON"
    echo "Active between: $PowerOnDuringWeekDaysAt and $PowerOffDuringWeekDaysAt - Monday to Friday (TMZ Europe/London)"

    if [[ $ListInstances = "" ]]
    then

      echo "No instances are currently running, Powering ON the Environment..."
      $PowerOn

    else

      echo "Found running instances, skipping..."

    fi

  else

    echo "Schedule status: Power OFF"
    echo "Active between: $PowerOffDuringWeekDaysAt and $PowerOnDuringWeekDaysAt - Monday to Friday (TMZ Europe/London)"

    if [[ $ListInstances = "" ]]
    then

      echo "No running instances found, skipping..."

    else

      echo "Found instances running, Powering OFF the Environment..."
      $PowerOff

    fi

  fi

elif (($WeekDay >= 6 && $WeekDay <= 7))
then

  echo "Weekend Schedule"
  echo "----------------"
  echo "Schedule status: Power OFF"
  echo "Active between: Friday $PowerOffDuringWeekDaysAt and Monday $PowerOnDuringWeekDaysAt - Friday, Saturday and Sunday (TMZ Europe/London)"

  if [[ $ListInstances = "" ]]
  then

    echo "No running instances found, skipping..."

  else

    echo "Found instances running, Powering OFF the Environment..."
    $PowerOff

  fi

fi

Comments

Popular posts from this blog

AWS, Elastic Beanstalk - Answers

AWS resources

AWS CLI