forked from aws-solutions/quota-monitor-for-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limit-monitor.yml
326 lines (326 loc) · 11.7 KB
/
limit-monitor.yml
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
---
AWSTemplateFormatVersion: '2010-09-09'
Description: "(SO0005) - AWS Limits Monitor - This templates creates and schedules
Lambda functions to notify you of approaching limits"
Parameters:
TopicEmail:
Default: [email protected]
Description: E-Mail address to subscribe to alerts
Type: String
AccountList:
Default: '000000000000|999999999999'
Description: Pipe Delimited List of Account Numbers to Scan for Limits, if not changed the default will use your local account.
Type: String
CheckRoleName:
Default: LimitCheckRole
Description: Name of IAM Role created to check limits
Type: String
SendAnonymousData:
Description: Send anonymous data to AWS
Type: String
Default: 'Yes'
AllowedValues:
- 'Yes'
- 'No'
CronSchedule:
Default: rate(1 day)
Description: 'Schedule to execute Lambda. e.g:.cron(* * * * *) or rate(1 hour/day/etc...).
See: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html'
Type: String
LambdaMemorySize:
Default: '256'
Description: Memory to allocate to the Master and Child Lambda's. In most cases,
256MB should be enough, but larger accounts may need more. Please increase this
value if your lambda's are failing. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-memorysize
Type: String
Conditions:
DefaultAccount: !Equals [ !Ref AccountList, '000000000000|999999999999' ]
Resources:
SNSTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: AWS Limits SNS
Subscription:
- Endpoint:
Ref: TopicEmail
Protocol: email
Metadata:
Comment: Create an SNS Topic to Publish Alerts to, subscribe e-mail parameter
MasterLambda:
Type: AWS::Lambda::Function
Properties:
Description: Function which evaluates list of accounts passed by the CloudWatch
Event and calls the Child Lambda to scan each account
Code:
#S3Bucket: !Join [ '', [ solutions-, !Ref AWS::Region ] ]
S3Bucket: isco-cloudtrail
S3Key: limit-monitor/v2/limits.zip
Handler: limit-master.lambda_handler
Role:
Fn::GetAtt:
- MasterRole
- Arn
Runtime: python2.7
Timeout: '300'
Environment:
Variables:
Region: !Ref AWS::Region
AccountList: !If [ DefaultAccount, !Ref "AWS::AccountId", !Ref AccountList ]
InitiateCheckLambda: !Ref InitiateCheckLambda
DependsOn: MasterRole
Metadata:
Comment: Function which evaluates list of accounts passed by the CloudWatch
Event and calls the Child Lambda to scan each account
InitiateCheckLambda:
Type: AWS::Lambda::Function
Properties:
Description: Function which assumes a role in each account to initiate TA refresh and
creates recurring CWE to check for updated results
Code:
#S3Bucket: !Join [ '', [ solutions-, !Ref AWS::Region ] ]
S3Bucket: isco-cloudtrail
S3Key: limit-monitor/v2/limits.zip
Handler: limit-initiate.lambda_handler
Role: !GetAtt InitiateCheckLambdaRole.Arn
Runtime: python2.7
MemorySize: !Ref LambdaMemorySize
Timeout: '300'
Environment:
Variables:
CheckRoleName: !Ref CheckRoleName
RetrieveResultsLambda: !GetAtt RetrieveResultsLambda.Arn
Region: !Ref AWS::Region
DependsOn: InitiateCheckLambdaRole
Metadata:
Comment: Function which assumes a role in each account to scan for approaching
limits and sends an alert to the SNS topic
RetrieveResultsLambda:
Type: AWS::Lambda::Function
Properties:
Description: Function which assumes a role in each account to scan for approaching
limits and sends an alert to the SNS topic
Code:
#S3Bucket: !Join [ '', [ solutions-, !Ref AWS::Region ] ]
S3Bucket: isco-cloudtrail
S3Key: limit-monitor/v2/limits.zip
Handler: limit-check.lambda_handler
Role: !GetAtt RetrieveResultsLambdaRole.Arn
Runtime: python2.7
MemorySize: !Ref LambdaMemorySize
Timeout: '300'
Environment:
Variables:
CheckRoleName: !Ref CheckRoleName
UUID: !GetAtt CheckRole.UUID
SNSArn: !Ref SNSTopic
Region: !Ref AWS::Region
SendAnonymousData: !Ref SendAnonymousData
DependsOn: RetrieveResultsLambdaRole
Metadata:
Comment: Function which assumes a role in each account to scan for approaching
limits and sends an alert to the SNS topic
ConfigLambda:
Type: AWS::Lambda::Function
Properties:
Description: Custom Resource Lambda to create the cross-account role assumed
by the Child Lambda
Code:
#S3Bucket: !Join [ '', [ solutions-, !Ref AWS::Region ] ]
S3Bucket: isco-cloudtrail
S3Key: limit-monitor/v2/limits.zip
Handler: configuration.lambda_handler
Role: !GetAtt ConfigRole.Arn
Runtime: python2.7
Timeout: '300'
DependsOn: ConfigRole
Metadata:
Comment: Custom Resource Lambda to create the cross-account role assumed by
the Child Lambda
CheckRole:
Type: Custom::CheckRole
Properties:
ServiceToken: !GetAtt ConfigLambda.Arn
Region: !Ref AWS::Region
CheckRoleName: !Ref CheckRoleName
AccountNumber: !Ref AWS::AccountId
Metadata:
Comment: Invoke the Configuration Lambda Function with the specified parameters
for creating the role to assume
InitiateCheckLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: Limits-AssumeRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:log-group:/aws/lambda/*
- Effect: Allow
Action:
- sts:AssumeRole
Resource: !Join [ '', [ "arn:aws:iam::*:role/", !Ref CheckRoleName ] ]
- Effect: Allow
Action:
- lambda:Invoke*
Resource: !GetAtt RetrieveResultsLambda.Arn
Metadata:
Comment: Invocation role for the Lambda functions to use
RetrieveResultsLambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: Limits-AssumeRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:log-group:/aws/lambda/*
- Effect: Allow
Action:
- sns:Publish
Resource:
- Ref: SNSTopic
- Effect: Allow
Action:
- sts:AssumeRole
Resource: !Join [ '', [ "arn:aws:iam::*:role/", !Ref CheckRoleName ] ]
- Effect: Allow
Action:
- ec2:DescribeRegions
Resource:
- "*"
Metadata:
Comment: Invocation role for the Lambda functions to use
MasterRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: Limits-AssumeRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:log-group:/aws/lambda/*
- Effect: Allow
Action:
- lambda:Invoke*
Resource: !GetAtt InitiateCheckLambda.Arn
Metadata:
Comment: Invocation role for the Lambda functions to use
ConfigRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: "/"
Policies:
- PolicyName: Limits-AssumeRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:log-group:/aws/lambda/*
- Effect: Allow
Action:
- iam:AttachRolePolicy
- iam:DetachRolePolicy
- iam:CreateRole
- iam:DeleteRole
- iam:PutRolePolicy
- iam:DeleteRolePolicy
Resource:
- Fn::Join:
- ''
- - 'arn:aws:iam::'
- Ref: AWS::AccountId
- ":role/"
- Ref: CheckRoleName
Metadata:
Comment: Invocation role for the Lambda functions to use
MasterRule:
Type: AWS::Events::Rule
Properties:
ScheduleExpression: !Ref CronSchedule
State: ENABLED
Targets:
- Arn: !GetAtt MasterLambda.Arn
Id: LimitsRule
DependsOn: CheckRole
MasterLambdaInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt MasterLambda.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com
SourceArn: !GetAtt MasterRule.Arn
Outputs:
CreateRole:
Description: Run this in each sub-account you wish to monitor to create a role
for the primary account to assume
Value: !Sub |
aws iam create-role --role-name ${CheckRoleName} --assume-role-policy-document "{\"Version\": \"2012-10-17\",\"Statement\":[{\"Effect\": \"Allow\",\"Principal\": {\"AWS\": \"${AWS::AccountId}\"},\"Action\": \"sts:AssumeRole\"}]}"
AttachPolicy1:
Description: Add Read Only Access
Value: !Sub |
aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/ReadOnlyAccess --role-name ${CheckRoleName}
AttachPolicy2:
Description: Add Support API for TrustedAdvisor access
Value: !Sub |
aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/AWSSupportAccess --role-name ${CheckRoleName}
AttachPolicy3:
Description: Add Support API for TrustedAdvisor access
Value: !Sub |
aws iam put-role-policy --role-name ${CheckRoleName} --policy-name CloudFormationDescribe --policy-document "{\"Version\": \"2012-10-17\",\"Statement\":[{\"Sid\": \"Stmt1455149881000\",\"Effect\": \"Allow\",\"Action\": [\"cloudformation:DescribeAccountLimits\",\"dynamodb:DescribeLimits\"],\"Resource\": [\"*\"]}]}"
AttachPolicy4:
Description: Add permissions to create CloudWatch Events and invoke child Lambda function
Value: !Sub |
aws iam put-role-policy --role-name ${CheckRoleName} --policy-name ChildLambdaPermissions --policy-document "{\"Version\": \"2012-10-17\",\"Statement\":[{\"Sid\": \"Stmt1455149881000\",\"Effect\": \"Allow\",\"Action\": [\"events:DeleteRule\", \"events:PutRule\", \"events:PutTargets\", \"events:RemoveTargets\", \"lambda:AddPermission\", \"lambda:RemovePermission\"],\"Resource\": [\"*\"]}]}"
UUID:
Description: Newly created random UUID.
Value: !GetAtt CheckRole.UUID