Skip to content

Instantly share code, notes, and snippets.

@mmattax
Created July 19, 2016 18:45
Show Gist options
  • Save mmattax/4530dcad17af037a983597bb1d54a03d to your computer and use it in GitHub Desktop.
Save mmattax/4530dcad17af037a983597bb1d54a03d to your computer and use it in GitHub Desktop.
Fix errant lifecycle hooks that codedeploy likes to leave within your autoscaling groups.
import boto3
autoscaling = boto3.client('autoscaling')
codedeploy = boto3.client('codedeploy')
hookMap = {}
print 'Gathering valid lifecycle hooks from CodeDeploy deployment groups...'
for applicationName in codedeploy.list_applications()['applications']:
for deploymentGroupName in codedeploy.list_deployment_groups(applicationName=applicationName)['deploymentGroups']:
deploymentGroup = codedeploy.get_deployment_group(
applicationName=applicationName,
deploymentGroupName=deploymentGroupName
)
for autoScalingGroup in deploymentGroup['deploymentGroupInfo'].get('autoScalingGroups', []):
if autoScalingGroup['name'] not in hookMap:
hookMap[autoScalingGroup['name']] = []
hookMap[autoScalingGroup['name']].append(autoScalingGroup['hook'])
deleted = 0
for AutoScalingGroupName, validHooks in hookMap.iteritems():
response = autoscaling.describe_lifecycle_hooks(AutoScalingGroupName=AutoScalingGroupName)
for LifecycleHook in response['LifecycleHooks']:
if LifecycleHook['LifecycleHookName'] not in validHooks:
print 'Deleting %s from %s...' % ( LifecycleHook['LifecycleHookName'], AutoScalingGroupName)
response = autoscaling.delete_lifecycle_hook(
LifecycleHookName=LifecycleHook['LifecycleHookName'],
AutoScalingGroupName=AutoScalingGroupName
)
deleted += 1
print 'Done.' if deleted > 0 else 'All is good.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment