Terminate idle instances on cloudstack using AWS
##Pre-requisites
- CloudStack cloud
- AWS CLI and AWS account API keys
- cloudmonkey (pip install cloudmonkey) and boto3 (pip install boto3) on the MS
- boto3, psutil on each cloudstack instance
##Usage
- Create Alarm Topic in SNS, SQS and API keys for Queue/SNS user
$ ./create-sqs-sns.sh <stack-name> <password for new IAM user to create>
E.g.,
$ ./create-sqs-sns.sh idleinstancestack l@mep@assword
- Grab the outputs of the stack after it is created
$ ./outputs.sh idleinstancestack
"OutputKey": "IdleInstanceAlarmSNSTopicTopicARN",
"OutputValue": "arn:aws:sns:us-west-2:987605588178:idleinstancestack0-IdleInstanceAlarmSNSTopic-BKKKKKKKKKKK"
"OutputKey": "IdleInstanceQueueInfo",
"OutputValue": "ARN: arn:aws:sqs:us-west-2:388888888888:idleinstancestack0-IdleInstanceQueue-KJJJJJJJJ URL: https://sqs.us-west-2.amazonaws.com/333333333333/idleinstancestack0-IdleInstanceQueue-KKKKKKKKKK"
"OutputKey": "IdleInstanceQueueUserInfo",
"OutputValue": "ARN: arn:aws:iam::384845588177:user/idleinstancestack0-IdleInstanceQueueUser-1FBJ8C0SOTGPR Access Key: AKIAABCDEDFHJKCIQNAD Secret Key: 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"
- Every time a VM is created, grab its UUID (say off of the event queue) and create an idle instance alarm.
$ ./create-alarm.sh <IdleInstanceAlarmSNSTopicTopicARN from output> <vm uuid>
- Inside each VM use the cpu.py script to sent CloudWatch metrics (customize the AWS region) every few minutes
- On the MS, run a job with sqs.py to fetch and delete the idle VM uuids.