forked from ThomasTusche/ecs-scaling-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.yaml
107 lines (98 loc) · 2.96 KB
/
main.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
###
# Full yaml file including the lambda for stopping and starting services,
# the cloudwatch alarms to trigger lambda during off office times,
# s3 container to
# and IAM roles for the right permissions
###
Parameters:
Codebucket:
Type: String
Description: Bucket where the python code for lambda is saved
Default: "XXX"
CodeKey:
Type: String
Description: The S3 object key
Default: "lambda_function.zip"
EcsFullPermissionRole:
Type: String
Description: ARN of the AWS managed IAM Role for ECS Full Access permissions e.g arn:aws:iam::aws:policy/AmazonECS_FullAccess
Default: "XXX"
ClusterName:
Type: String
Description: Name of the ECS Cluster
Default: "XXX"
Resources:
ScalingLambda:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref Codebucket
S3Key: !Ref CodeKey
Description: Sets all ECS services desired count to 0 or 1
FunctionName: ScalingLambda
Handler: 'lambda_function.lambda_handler'
Role: !GetAtt LambdaIamRole.Arn
Runtime: 'python3.8'
Timeout: 30
DependsOn: LambdaRolePolicy
LambdaIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- 'sts:AssumeRole'
Description: IAM Role for scaling Lambda
ManagedPolicyArns:
- !Ref EcsFullPermissionRole
RoleName: ScalingLambdaIamRole
LambdaRolePolicy:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- 'ec2:CreateNetworkInterface'
- 'ec2:DeleteNetworkInterface'
- 'ec2:DescribeNetworkInterfaces'
Resource: '*'
- Effect: Allow
Action:
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: '*'
PolicyName: ScalingLambdaIamPolicy
Roles:
- !Ref LambdaIamRole
DependsOn: LambdaIamRole
StartEcsServicesRuleCloudwatch:
Type: AWS::Events::Rule
Properties:
Description: Starts at 7am on workdays
Name: StartEcsServicesRule
ScheduleExpression: 'cron(0 7 ? * MON-FRI *)'
State: ENABLED
Targets:
- Arn: !GetAtt ScalingLambda.Arn
Input: !Sub '{"action": "start", "cluster": "${ClusterName}"}'
Id: startLambda
DependsOn: ScalingLambda
StopEcsServicesRuleCloudwatch:
Type: AWS::Events::Rule
Properties:
Description: Stops at 10pm on workdays
Name: StopEcsServicesRule
ScheduleExpression: 'cron(0 22 ? * MON-FRI *)'
State: ENABLED
Targets:
- Arn: !GetAtt ScalingLambda.Arn
Input: !Sub '{"action": "stop", "cluster": "${ClusterName}"}'
Id: stopLambda
DependsOn: ScalingLambda