Monday, May 1, 2017

SAP S/4 HANA automation on Cloud

   Cloud service providers  like AWS, Azure and GCP have certified SAP HANA on their cloud platform where IBM softlayer also has certified bare metal HANA certified server options.

 There are multiple benefits of implementing SAP on cloud. The major once are High availability, Simplified Disaster recovery, and Cost effective compared to other options. 

   But wait there is a caveat!! 

  In order to utilize all above benefits you need to automate many SAP BASIS level activity and also bring governance to development and quality process.

  I have implemented few automation scripts  to automate cost optimization, high availability, and disaster recovery with moderate RPO and RTO.


 In this post i will show basic cost optimization automation using scheduled starting and stopping of SAP HANA and S/4 application. This script is designed for AWS platform but the same can be converted to other cloud platform as well. 

Pre-requisites:
  1) Configure SNS topic and note down the ARN
  2) SSM agent is configured and EC2 instance is configured with lambda execution role.
  3) I have assumed that S/4 (App) and HANA on different servers with distributed architecture.
 

Gracefully shutdown process:


Step 1: Create a shell script Dev-SAPStop.sh for application server graceful shutdown and save the same to /home/ec2-user location. replace <> with SAP S/4 HANA and AWS resource level details.


-------------------------------------------------------Start Script-----------------------------------------------------
#! /bin/bash

#Stop the SAP R3 application service
sudo su - -c "stopsap r3"
if [ "$?" = "0" ]; then

echo "SAP App service was stopped hence instance is stopped."
#Send the notification for the lambda invocation
aws sns publish --topic-arn --message "Development server SAP APP service is stopped. hence we have initiated the instance shutdown."
          # Invoke lambda function to stop the instance. the lambda function to stop and start is given in earlier post http://www.cloudometry.in/2016/07/iac-with-aws-lambda-automate-infra.html
aws lambda invoke --invocation-type RequestResponse --function-name --region us-east-1 --log-type Tail outfile


else 
#Send notification in case if SAP App was not able to stop by the script
echo "SAP App service was not able to stop hence instance is not stopped." 
aws sns publish --topic-arn --message "Development server SAP APP or Hana service was not able to stop. hence we have not initiated the instance shutdown. Kindly login to server and stop the same manually." 
exit 
fi
-------------------------------------------------------------------End Script---------------------------------------------------------------- 
      
Step 2: Create a shell script Dev-HANAStop.sh for HANA server graceful shutdown.
       
--------------------------------------------------------------------Start Script--------------------------------------------------------------
#! /bin/bash
#Application Instance name 
 
INSTANCE_NAME=
#Initiate Application server shutdown process through remote ssh execution.
ssh -i ec2-user@ /home/ec2-user/Dev-SAPStop.sh

times=0
#Check if the instance is stopped or still running
while [ 5 -gt $times ] && aws ec2 describe-instance-status --instance-id $INSTANCE_NAME | grep "running"
do
     times=$(( $times + 1 ))
     echo Attempt $times at verifying $INSTANCE_NAME is stopped or not...
     sleep 20
done
#If Application server is still running then do not shutdown HANA. Exit the script execution
if [ 5 -eq $times ]; then
  echo Instance $INSTANCE_NAME is not stopped so we can not initiate the HANA shutdown.
  exit
fi 
#If the Application server is stopped then Stop the HANA service with HDB user 
sudo su - -c "/usr/sap//HDB/HDB stop" 
#Check if HANA services are stopped sucessfully or not.
case "$(pidof hdbindexserver | wc -w)" in
0) echo "SAP HANA Service is successfully stopped"
  #Send notification and Execute the lambda function to stop the instance.
                           aws sns publish --topic-arn --message "Development server SAP HANA Service is successfully stopped"
                                       # Invoke Lambda function to stop SAP HANA instance. 
                           aws lambda invoke --invocation-type RequestResponse --function-name --region us-east-1 --log-type Tail outfile       ;; 
1) echo "Hana service was not able to stop. Kindly login to server and stop the same manually"
    aws sns publish --topic-arn --message "Development server Hana service was not able to stop. Kindly login to server and stop the same manually."
            ;;
esac   
   -----------------------------------------------------------------END Script ----------------------------------------------------------

Step 3: Go To the EC2 Systems manager and create a maintenance window and execute Dev-HANAStop.sh script with defined interval. We are using AWS Systems manager because it provides multiple automation and logging benefits compared to cron job.


Create a maintenance window for scheduled task











     
Provide Maintenance window and provide the cron expression schedule along with maintenance window time frame.







Once the window is created, now we have to register a server to the maintenance window and configure the execution of script.









Select the shell script from the option to initiate the Dev-HANAStop.sh script execution.







Select the execution role and execution threshold.








  The same way we can configure the script to start  SAP S/4 HANA. Since this script is too specific with SAP S/4 HANA solution. it should be modified for BW/4 HANA, BO, Gateway, and solution manager.