#!/usr/bin/perl use strict; use warnings; my $ID; my @FINAL; my $SYNC; my $stack; my $DETECTION; #my $email=''; my @STACK_NAME=qx(aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE --query 'sort_by(StackSummaries, &StackName)[*].[StackName]' --output text ); for $stack (@STACK_NAME) { chomp($stack); $ID=qx(aws cloudformation detect-stack-drift --stack-name "$stack" | jq .[] | awk -F '\"' '{print \$2}'); chomp($ID); ##Get status based on ID do { $SYNC=qx(aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id $ID | grep StackDriftStatus | awk -F '\"' '{print \$4}'); $DETECTION=qx(aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id $ID | grep DetectionStatus | awk -F '\"' '{print \$4}'); #system qq{aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id $ID | grep DetectionStatus | awk -F '"' '{print \$4}'}; system qq{aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id $ID | grep StackDriftStatus | awk -F '\"' '{print \$4}'}; chomp($DETECTION); chomp($SYNC); } until(("$DETECTION" eq 'DETECTION_COMPLETE') || ("$DETECTION" =~ /DETECTION_FAILED/mi)) ; #if($SYNC eq 'IN_SYNC') if ( 'IN_SYNC' =~ /$SYNC/mi ) { print "Stack $stack is OK!!\n"; } else { print "Stack $stack is out of sync!! SEND EMAIL NOW!!\n\n"; #system qq{aws ses send-email --from "systems\@eemail.com" --destination "ToAddresses=$email" --message "Subject={Data=Cloudformation Drift Detected,Charset=utf8},Body={Text={Data=String,Charset=utf8},Html={Data=A Cloudformation stack has been detected to be out of SYNC on Stack $stack,Charset=utf8}}" --region us-east-1}; } }