Last active
April 6, 2021 15:18
-
-
Save roneigebert/9cf516a54ef9a8474dc111ce9667f2ee to your computer and use it in GitHub Desktop.
Attach TG to ASG after deploy + monitoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
get_instance_region() { | |
echo $(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | awk -F '"' '/\"region\"/ { print $4 }') | |
} | |
get_instance_id() { | |
curl -s http://169.254.169.254/latest/meta-data/instance-id | |
return $? | |
} | |
get_auto_scaling(){ | |
echo $(aws autoscaling --region $1 describe-auto-scaling-instances --instance-ids $2 --output text --query AutoScalingInstances[0].AutoScalingGroupName) | |
} | |
get_target_group(){ | |
echo $(aws ec2 describe-tags --region $1 --filters "Name=resource-id,Values=$2" "Name=key,Values=target-group" --output=text | cut -f5) | |
} | |
INSTANCE_REGION=$(get_instance_region) | |
INSTANCE_ID=$(get_instance_id) | |
AUTO_SCALING=$(get_auto_scaling $INSTANCE_REGION $INSTANCE_ID) | |
TARGET_GROUP=$(get_target_group $INSTANCE_REGION $INSTANCE_ID) | |
echo "InstanceId > $INSTANCE_ID" | |
echo "Region > $INSTANCE_REGION" | |
echo "ScalingGroup > $AUTO_SCALING" | |
echo "TargetGroup > $TARGET_GROUP" | |
if [ -z "$TARGET_GROUP" ]; then | |
echo "No target group defined to attach" | |
else | |
TARGET_GROUP_ARN=$(aws elbv2 describe-target-groups --region $INSTANCE_REGION --names $TARGET_GROUP | awk -F '"' '/\"TargetGroupArn\"/ { print $4 }') | |
echo "Attaching target-group $TARGET_GROUP ($TARGET_GROUP_ARN) on $AUTO_SCALING..." | |
aws autoscaling attach-load-balancer-target-groups --region $INSTANCE_REGION --auto-scaling-group-name $AUTO_SCALING --target-group-arns $TARGET_GROUP_ARN | |
echo "Attach finished!" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2, json, boto3 | |
cloudwatch = boto3.client('cloudwatch') | |
autoscaling = boto3.client('autoscaling') | |
def registrar_metrica( bad_asgs ): | |
cloudwatch.put_metric_data( | |
MetricData=[ | |
{ | |
'MetricName': 'AutoScalingWithBadHealthCheck', | |
'Dimensions': [], | |
'Unit': 'None', | |
'Value': bad_asgs | |
}, | |
], | |
Namespace='MyCustomAWSMetrics' | |
) | |
def lambda_handler(event, context): | |
describe_result = autoscaling.describe_auto_scaling_groups() | |
bad_asgs = 0 | |
for asg in describe_result['AutoScalingGroups']: | |
asg_name = asg['AutoScalingGroupName'] | |
print "\nCheking ASG " + asg_name | |
desired_capacity = asg['DesiredCapacity'] | |
if desired_capacity == 0: | |
print "ASG not enabled, skipping..." | |
continue | |
health_check_type = asg['HealthCheckType'] | |
if health_check_type != 'ELB': | |
print 'ASG with ' + health_check_type + ' health check, expected: ELB' | |
bad_asgs += 1 | |
continue | |
tg_count = len(asg['TargetGroupARNs']) | |
if tg_count < 1: | |
print 'ASG with no TG atacched' | |
bad_asgs += 1 | |
continue | |
print 'ASG OK' | |
print "\nBad ASGs: %d" % bad_asgs | |
registrar_metrica( bad_asgs ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment