-
Notifications
You must be signed in to change notification settings - Fork 0
/
warmup-function.yaml
176 lines (152 loc) · 4.71 KB
/
warmup-function.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
AWSTemplateFormatVersion: "2010-09-09"
Description:
Function to perform service discovery tags based and puts a scaling rule
Transform:
- AWS::Serverless-2016-10-31
Metadata:
Maintainer: [email protected]
AWS::CloudFormation::Interface:
ParameterGroups:
- Parameters:
- LambdaLayerVersionArn
Label:
default: Lambda Settings
- Parameters:
- PermissionsBoundary
Label:
default: IAM Settings
Parameters:
PermissionsBoundary:
Type: String
Default: none
Description: IAM Policy ARN to use for PermissionsBoundary if required. Optional.
LambdaLayerVersionArn:
Type: String
Description: Lambda Layer ARN to use with the function(s)
FunctionsPrefix:
Type: String
Default: ecsScalingScheduler
ServicesWarmupTags:
Type: String
Description: The list of tags and values to use to perform service discovery.
ServiceDimensionTag:
Type: String
Default: warmup-dimension
Description: Tag associated with the service to identify the dimension
ServiceWarmupDurationTag:
Type: String
Description: Tag on the service that indicates duration of warmup.
ServicesCluster:
Type: String
Description: ECS Cluster to scan services into.
TriggerTopicName:
Type: String
LogLevel:
Type: String
Default: INFO
AllowedValues:
- INFO
- ERROR
- WARNING
- DEBUG
- CRITICAL
Conditions:
PermissionsBoundaryCon: !Not [!Equals [!Ref PermissionsBoundary, "none"]]
OverrideFunctionName: !Not [!Equals [!Ref FunctionsPrefix, "none"]]
Resources:
###############################################
# ECS Warmup via Services Tags discovery
SnsTriggerTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Ref TriggerTopicName
ecsScalingSchedulerPolicy:
DependsOn: ecsWarmupSchedulerFunctionRole
Type: AWS::IAM::Policy
Properties:
Roles:
- !Ref ecsWarmupSchedulerFunctionRole
PolicyName: "ECSScheduler"
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: DescribeAllEcs
Effect: Allow
Action:
- ecs:Describe*
- ecs:List*
- ecs:Get*
Resource:
- "*"
- Sid: ScalingPolicyActions
Effect: Allow
Action:
- "application-autoscaling:Describe*"
- "application-autoscaling:PutScheduledAction"
- "application-autoscaling:DeleteScheduledAction"
Resource:
- "*"
ecsWarmupSchedulerFunctionRole:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
- arn:aws:iam::aws:policy/ResourceGroupsandTagEditorReadOnlyAccess
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Condition:
Bool:
aws:SecureTransport: 'true'
Effect: Allow
Principal:
Service:
- !Sub 'lambda.${AWS::URLSuffix}'
Version: '2012-10-17'
Description: !Sub 'ecsWarmupSchedulerFunctionRole-${AWS::StackName}'
PermissionsBoundary: !If
- PermissionsBoundaryCon
- !Ref PermissionsBoundary
- !Ref AWS::NoValue
ecsWarmupAtScalingScheduler:
Type: AWS::Serverless::Function
DependsOn:
- SnsTriggerTopic
Properties:
Tags:
Name: ecs-scaling-scheduler-Warmup-at-action
Source: https://github.com/compose-x/ecs-scaling-scheduler
FunctionName: !If
- OverrideFunctionName
- !Sub '${FunctionsPrefix}-WarmupScheduler'
- !Sub '${AWS::StackName}-WarmupScheduler'
Runtime: python3.8
Handler: index.lambda_handler
Timeout: 60
Role: !GetAtt ecsWarmupSchedulerFunctionRole.Arn
Layers:
- !Ref LambdaLayerVersionArn
Environment:
Variables:
WARMUP_ECS_SERVICES_TAGS: !Ref ServicesWarmupTags
WARMUP_DURATION_TAG: !Ref ServiceWarmupDurationTag
ECS_CLUSTER_NAME: !Ref ServicesCluster
LOG_LEVEL: !Ref LogLevel
InlineCode: |
#!/usr/bin/env python
from ecs_scaling_scheduler.aws_lambda_functions import services_event_warmup as handler
def lambda_handler(event, context):
""" Entry point function """
return handler(event, context)
Events:
SnsTrigger:
Type: SNS
Properties:
Topic:
!Ref SnsTriggerTopic
Outputs:
WarmupSchedulerFunctionArn:
Value: !GetAtt ecsWarmupAtScalingScheduler.Arn
WarmupSchedulerFunctionName:
Value: !Ref "ecsWarmupAtScalingScheduler"