forked from aws-samples/ecs-refarch-cloudformation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecyclehook.yaml
145 lines (145 loc) · 5.97 KB
/
lifecyclehook.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Description: >
This template deploys a Lambda Function and Auto Scaling Lifecycle Hook to drain Tasks from your Container Instances when an Instance is selected for Termination in your Auto Scaling Group.
Parameters:
Cluster:
Type: String
Description: Name of ECS Cluster
ECSAutoScalingGroupName:
Type: String
Description: Name of Auto Scaling Group
Resources:
NotificationTopic:
Type: "AWS::SNS::Topic"
Properties:
Subscription:
- Endpoint: !GetAtt
- LifecycleHandlerFunction
- Arn
Protocol: lambda
DependsOn: LifecycleHandlerFunction
InstanceTerminatingHook:
Type: "AWS::AutoScaling::LifecycleHook"
Properties:
AutoScalingGroupName: !Ref ECSAutoScalingGroupName
DefaultResult: ABANDON
HeartbeatTimeout: "900"
LifecycleTransition: "autoscaling:EC2_INSTANCE_TERMINATING"
NotificationTargetARN: !Ref NotificationTopic
RoleARN: !GetAtt
- AutoscalingNotificationRole
- Arn
DependsOn: NotificationTopic
AutoscalingNotificationRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- autoscaling.amazonaws.com
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AutoScalingNotificationAccessRole"
LambdaExecutionRole:
Type: "AWS::IAM::Role"
Properties:
Policies:
- PolicyName: lambda-inline
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- "autoscaling:CompleteLifecycleAction"
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
- "ec2:DescribeInstances"
- "ec2:DescribeInstanceAttribute"
- "ec2:DescribeInstanceStatus"
- "ec2:DescribeHosts"
- "ecs:ListContainerInstances"
- "ecs:SubmitContainerStateChange"
- "ecs:SubmitTaskStateChange"
- "ecs:DescribeContainerInstances"
- "ecs:UpdateContainerInstancesState"
- "ecs:ListTasks"
- "ecs:DescribeTasks"
- "sns:Publish"
- "sns:ListSubscriptions"
Resource: "*"
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AutoScalingNotificationAccessRole"
LambdaInvokePermission:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName: !Ref LifecycleHandlerFunction
Action: "lambda:InvokeFunction"
Principal: sns.amazonaws.com
SourceArn: !Ref NotificationTopic
LifecycleHandlerFunction:
Type: "AWS::Lambda::Function"
Properties:
Environment:
Variables:
CLUSTER: !Ref Cluster
Code:
ZipFile: !Join
- ""
- - |
import boto3,json,os,time
ec2Client = boto3.client('ec2')
ecsClient = boto3.client('ecs')
autoscalingClient = boto3.client('autoscaling')
snsClient = boto3.client('sns')
lambdaClient = boto3.client('lambda')
def publishSNSMessage(snsMessage,snsTopicArn):
response = snsClient.publish(TopicArn=snsTopicArn,Message=json.dumps(snsMessage),Subject='reinvoking')
def setContainerInstanceStatusToDraining(ecsClusterName,containerInstanceArn):
response = ecsClient.update_container_instances_state(cluster=ecsClusterName,containerInstances=[containerInstanceArn],status='DRAINING')
def tasksRunning(ecsClusterName,ec2InstanceId):
ecsContainerInstances = ecsClient.describe_container_instances(cluster=ecsClusterName,containerInstances=ecsClient.list_container_instances(cluster=ecsClusterName)['containerInstanceArns'])['containerInstances']
for i in ecsContainerInstances:
if i['ec2InstanceId'] == ec2InstanceId:
if i['status'] == 'ACTIVE':
setContainerInstanceStatusToDraining(ecsClusterName,i['containerInstanceArn'])
return 1
if (i['runningTasksCount']>0) or (i['pendingTasksCount']>0):
return 1
return 0
return 2
def lambda_handler(event, context):
ecsClusterName=os.environ['CLUSTER']
snsTopicArn=event['Records'][0]['Sns']['TopicArn']
snsMessage=json.loads(event['Records'][0]['Sns']['Message'])
lifecycleHookName=snsMessage['LifecycleHookName']
lifecycleActionToken=snsMessage['LifecycleActionToken']
asgName=snsMessage['AutoScalingGroupName']
ec2InstanceId=snsMessage['EC2InstanceId']
checkTasks=tasksRunning(ecsClusterName,ec2InstanceId)
if checkTasks==0:
try:
response = autoscalingClient.complete_lifecycle_action(LifecycleHookName=lifecycleHookName,AutoScalingGroupName=asgName,LifecycleActionToken=lifecycleActionToken,LifecycleActionResult='CONTINUE')
except BaseException as e:
print(str(e))
elif checkTasks==1:
time.sleep(5)
publishSNSMessage(snsMessage,snsTopicArn)
Handler: index.lambda_handler
Role: !GetAtt
- LambdaExecutionRole
- Arn
Runtime: python3.6
Timeout: 10