A web-hook to make a storage get deleted after its instance got terminated. - "Value for parameter instancedId is invalid"

32 views
Skip to first unread message

Brant Fortest

unread,
Aug 14, 2016, 4:13:24 AM8/14/16
to scalr-discuss

Hi, Team,
I'm trying to make a web-hook to make a storage get deleted after its instance got terminated.
Following your guide. "Using Amazon Route 53 DNS with Scalr" https://scalr-wiki.atlassian.net/wiki/display/docs/Using+Amazon+Route+53+DNS+with+Scalr

Here's my script 

------------------------

root@ec2:~/script# cat /root/script/scalr-ec2.py

#!/usr/bin/env python

# -*- coding: utf-8 -*-

 

import json

import hmac

import pytz

import boto3

import logging

import binascii

import dateutil.parser

from hashlib import sha1

from datetime import datetime

from flask import Flask, request

 

app = Flask(__name__)

 

 

SCALR_SIGNING_KEY = 'H3fhHPYOKswyMAz6/ztP+0wLrjfpXiBZ8+7Ms23V0G7pKxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

 

@app.route("/", methods=['POST'])

def webhook_listener():

    if not validateRequest(request):

        return 'Invalid signature'

 

    data = json.loads(request.data)

    if not 'eventName' in data or not 'data' in data:

        return 'Invalid call'

 

    if data['eventName'] == 'BeforeHostTerminate':

        deleteOnTermination(data['data'])

    return "Ok"

 

def deleteOnTermination(data):

    client = boto3.client('ec2')

    iid =  data['SCALR_INSTANCE_ID']

    logging.debug('Set DeleteOnTermination Flag on %s', iid)

    response = client.modify_instance_attribute(

        InstanceId = 'iid',

        BlockDeviceMappings=[

          {

            'DeviceName': '/dev/sdg',

            'Ebs': {

                'DeleteOnTermination': True

            },

          },

        ],

      )

    logging.info(response)

 

def validateRequest(request):

    if not 'X-Signature' in request.headers or not 'Date' in request.headers:

        return False

    date = request.headers['Date']

    body = request.data

    expected_signature = binascii.hexlify(hmac.new(SCALR_SIGNING_KEY, body + date, sha1).digest())

    if expected_signature != request.headers['X-Signature']:

        return False

    date = dateutil.parser.parse(date)

    now = datetime.now(pytz.utc)

    delta = abs((now - date).total_seconds())

    return delta < 300 # Requests are validfor 5 minutes

   

 

if __name__ == "__main__":

    app.run(host='0.0.0.0')

 

------------------------


According to Webhook Notifications, these  four should contain same Instance ID.

"SCALR_CLOUD_SERVER_ID", "SCALR_INSTANCE_ID", "SCALR_EVENT_CLOUD_SERVER_ID", "SCALR_EVENT_INSTANCE_ID"

But still I got this error. 


Am I missing something here? Thanks!


 


Brant Fortest

unread,
Aug 14, 2016, 10:41:30 AM8/14/16
to scalr-discuss
Why Event "BeforeHostTerminate" would detach volume before terminating instance?
This's the reason why my boto3 script couldn't make a volume get deleted after the termination of instance on your Scalr GUI, but work fine on aws console termination of instance.
------
response = client.modify_instance_attribute(
  InstanceId = 'i-d14b9644',
  BlockDeviceMappings = [
    {
       'DeviceName': '/dev/sdg',
       'Ebs': {
          'DeleteOnTermination': True
       },
    },
  ],
)
------
Can I modify the default behavior of Event "BeforeHostTerminate"? Thanks!


Brant
Reply all
Reply to author
Forward
0 new messages