/
ec2.py
1916 lines (1559 loc) · 62.3 KB
/
ec2.py
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2015-2017 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import, division, print_function, unicode_literals
import base64
import itertools
import operator
import random
import re
import zlib
import six
from botocore.exceptions import ClientError
from dateutil.parser import parse
from concurrent.futures import as_completed
import jmespath
from c7n.actions import (
ActionRegistry, BaseAction, ModifyVpcSecurityGroupsAction
)
from c7n.exceptions import PolicyValidationError
from c7n.filters import (
FilterRegistry, AgeFilter, ValueFilter, Filter, DefaultVpcBase
)
from c7n.filters.offhours import OffHour, OnHour
import c7n.filters.vpc as net_filters
from c7n.manager import resources
from c7n import query, utils
from c7n.utils import type_schema, filter_empty
from c7n.resources.iam import CheckPermissions
from c7n.resources.securityhub import PostFinding
RE_ERROR_INSTANCE_ID = re.compile("'(?P<instance_id>i-.*?)'")
filters = FilterRegistry('ec2.filters')
actions = ActionRegistry('ec2.actions')
@resources.register('ec2')
class EC2(query.QueryResourceManager):
class resource_type(query.TypeInfo):
service = 'ec2'
arn_type = 'instance'
enum_spec = ('describe_instances', 'Reservations[].Instances[]', None)
id = 'InstanceId'
filter_name = 'InstanceIds'
filter_type = 'list'
name = 'PublicDnsName'
date = 'LaunchTime'
dimension = 'InstanceId'
config_type = "AWS::EC2::Instance"
default_report_fields = (
'CustodianDate',
'InstanceId',
'tag:Name',
'InstanceType',
'LaunchTime',
'VpcId',
'PrivateIpAddress',
)
filter_registry = filters
action_registry = actions
# if we have to do a fallback scenario where tags don't come in describe
permissions = ('ec2:DescribeTags',)
def __init__(self, ctx, data):
super(EC2, self).__init__(ctx, data)
self.queries = QueryFilter.parse(self.data.get('query', []))
def resources(self, query=None):
q = self.resource_query()
if q is not None:
query = query or {}
query['Filters'] = q
return super(EC2, self).resources(query=query)
def resource_query(self):
qf = []
qf_names = set()
# allow same name to be specified multiple times and append the queries
# under the same name
for q in self.queries:
qd = q.query()
if qd['Name'] in qf_names:
for qf in qf:
if qd['Name'] == qf['Name']:
qf['Values'].extend(qd['Values'])
else:
qf_names.add(qd['Name'])
qf.append(qd)
return qf
def get_source(self, source_type):
if source_type == 'describe':
return DescribeEC2(self)
elif source_type == 'config':
return query.ConfigSource(self)
raise ValueError('invalid source %s' % source_type)
class DescribeEC2(query.DescribeSource):
def augment(self, resources):
"""EC2 API and AWOL Tags
While ec2 api generally returns tags when doing describe_x on for
various resources, it may also silently fail to do so unless a tag
is used as a filter.
See footnote on for official documentation.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI
Apriori we may be using custodian to ensure tags (including
name), so there isn't a good default to ensure that we will
always get tags from describe_x calls.
"""
# First if we're in event based lambda go ahead and skip this,
# tags can't be trusted in ec2 instances immediately post creation.
if not resources or self.manager.data.get(
'mode', {}).get('type', '') in (
'cloudtrail', 'ec2-instance-state'):
return resources
# AWOL detector, so we don't make extraneous api calls.
resource_count = len(resources)
search_count = min(int(resource_count % 0.05) + 1, 5)
if search_count > resource_count:
search_count = resource_count
found = False
for r in random.sample(resources, search_count):
if 'Tags' in r:
found = True
break
if found:
return resources
# Okay go and do the tag lookup
client = utils.local_session(self.manager.session_factory).client('ec2')
tag_set = self.manager.retry(
client.describe_tags,
Filters=[{'Name': 'resource-type',
'Values': ['instance']}])['Tags']
resource_tags = {}
for t in tag_set:
t.pop('ResourceType')
rid = t.pop('ResourceId')
resource_tags.setdefault(rid, []).append(t)
m = self.manager.get_model()
for r in resources:
r['Tags'] = resource_tags.get(r[m.id], ())
return resources
@filters.register('security-group')
class SecurityGroupFilter(net_filters.SecurityGroupFilter):
RelatedIdsExpression = "SecurityGroups[].GroupId"
@filters.register('subnet')
class SubnetFilter(net_filters.SubnetFilter):
RelatedIdsExpression = "SubnetId"
@filters.register('vpc')
class VpcFilter(net_filters.VpcFilter):
RelatedIdsExpression = "VpcId"
@filters.register('check-permissions')
class ComputePermissions(CheckPermissions):
def get_iam_arns(self, resources):
profile_arn_map = {
r['IamInstanceProfile']['Arn']: r['IamInstanceProfile']['Id']
for r in resources if 'IamInstanceProfile' in r}
# py2 compat on dict ordering
profile_arns = list(profile_arn_map.items())
profile_role_map = {
arn: profile['Roles'][0]['Arn']
for arn, profile in zip(
[p[0] for p in profile_arns],
self.manager.get_resource_manager(
'iam-profile').get_resources(
[p[1] for p in profile_arns]))}
return [
profile_role_map.get(r.get('IamInstanceProfile', {}).get('Arn'))
for r in resources]
@filters.register('state-age')
class StateTransitionAge(AgeFilter):
"""Age an instance has been in the given state.
.. code-block:: yaml
policies:
- name: ec2-state-running-7-days
resource: ec2
filters:
- type: state-age
op: ge
days: 7
"""
RE_PARSE_AGE = re.compile(r"\(.*?\)")
# this filter doesn't use date_attribute, but needs to define it
# to pass AgeFilter's validate method
date_attribute = "dummy"
schema = type_schema(
'state-age',
op={'$ref': '#/definitions/filters_common/comparison_operators'},
days={'type': 'number'})
def get_resource_date(self, i):
v = i.get('StateTransitionReason')
if not v:
return None
dates = self.RE_PARSE_AGE.findall(v)
if dates:
return parse(dates[0][1:-1])
return None
class StateTransitionFilter(object):
"""Filter instances by state.
Try to simplify construction for policy authors by automatically
filtering elements (filters or actions) to the instances states
they are valid for.
For more details see
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html
"""
valid_origin_states = ()
def filter_instance_state(self, instances, states=None):
states = states or self.valid_origin_states
orig_length = len(instances)
results = [i for i in instances
if i['State']['Name'] in states]
self.log.info("%s %d of %d instances" % (
self.__class__.__name__, len(results), orig_length))
return results
@filters.register('ebs')
class AttachedVolume(ValueFilter):
"""EC2 instances with EBS backed volume
Filters EC2 instances with EBS backed storage devices (non ephemeral)
:Example:
.. code-block:: yaml
policies:
- name: ec2-encrypted-ebs-volumes
resource: ec2
filters:
- type: ebs
key: Encrypted
value: true
"""
schema = type_schema(
'ebs', rinherit=ValueFilter.schema,
**{'operator': {'enum': ['and', 'or']},
'skip-devices': {'type': 'array', 'items': {'type': 'string'}}})
schema_alias = False
def get_permissions(self):
return self.manager.get_resource_manager('ebs').get_permissions()
def process(self, resources, event=None):
self.volume_map = self.get_volume_mapping(resources)
self.skip = self.data.get('skip-devices', [])
self.operator = self.data.get(
'operator', 'or') == 'or' and any or all
return list(filter(self, resources))
def get_volume_mapping(self, resources):
volume_map = {}
manager = self.manager.get_resource_manager('ebs')
for instance_set in utils.chunks(resources, 200):
volume_ids = []
for i in instance_set:
for bd in i.get('BlockDeviceMappings', ()):
if 'Ebs' not in bd:
continue
volume_ids.append(bd['Ebs']['VolumeId'])
for v in manager.get_resources(volume_ids):
if not v['Attachments']:
continue
volume_map.setdefault(
v['Attachments'][0]['InstanceId'], []).append(v)
return volume_map
def __call__(self, i):
volumes = self.volume_map.get(i['InstanceId'])
if not volumes:
return False
if self.skip:
for v in list(volumes):
for a in v.get('Attachments', []):
if a['Device'] in self.skip:
volumes.remove(v)
return self.operator(map(self.match, volumes))
@filters.register('termination-protected')
class DisableApiTermination(Filter):
"""EC2 instances with ``disableApiTermination`` attribute set
Filters EC2 instances with ``disableApiTermination`` attribute set to true.
:Example:
.. code-block:: yaml
policies:
- name: termination-protection-enabled
resource: ec2
filters:
- type: termination-protected
:Example:
.. code-block:: yaml
policies:
- name: termination-protection-NOT-enabled
resource: ec2
filters:
- not:
- type: termination-protected
"""
schema = type_schema('termination-protected')
permissions = ('ec2:DescribeInstanceAttribute',)
def get_permissions(self):
perms = list(self.permissions)
perms.extend(self.manager.get_permissions())
return perms
def process(self, resources, event=None):
client = utils.local_session(
self.manager.session_factory).client('ec2')
return [r for r in resources
if self.is_termination_protection_enabled(client, r)]
def is_termination_protection_enabled(self, client, inst):
attr_val = self.manager.retry(
client.describe_instance_attribute,
Attribute='disableApiTermination',
InstanceId=inst['InstanceId']
)
return attr_val['DisableApiTermination']['Value']
class InstanceImageBase(object):
def prefetch_instance_images(self, instances):
image_ids = [i['ImageId'] for i in instances if 'c7n:instance-image' not in i]
self.image_map = self.get_local_image_mapping(image_ids)
def get_base_image_mapping(self):
return {i['ImageId']: i for i in
self.manager.get_resource_manager('ami').resources()}
def get_instance_image(self, instance):
image = instance.get('c7n:instance-image', None)
if not image:
image = instance['c7n:instance-image'] = self.image_map.get(instance['ImageId'], None)
return image
def get_local_image_mapping(self, image_ids):
base_image_map = self.get_base_image_mapping()
resources = {i: base_image_map[i] for i in image_ids if i in base_image_map}
missing = list(set(image_ids) - set(resources.keys()))
if missing:
loaded = self.manager.get_resource_manager('ami').get_resources(missing, False)
resources.update({image['ImageId']: image for image in loaded})
return resources
@filters.register('image-age')
class ImageAge(AgeFilter, InstanceImageBase):
"""EC2 AMI age filter
Filters EC2 instances based on the age of their AMI image (in days)
:Example:
.. code-block:: yaml
policies:
- name: ec2-ancient-ami
resource: ec2
filters:
- type: image-age
op: ge
days: 90
"""
date_attribute = "CreationDate"
schema = type_schema(
'image-age',
op={'$ref': '#/definitions/filters_common/comparison_operators'},
days={'type': 'number'})
def get_permissions(self):
return self.manager.get_resource_manager('ami').get_permissions()
def process(self, resources, event=None):
self.prefetch_instance_images(resources)
return super(ImageAge, self).process(resources, event)
def get_resource_date(self, i):
image = self.get_instance_image(i)
if image:
return parse(image['CreationDate'])
else:
return parse("2000-01-01T01:01:01.000Z")
@filters.register('image')
class InstanceImage(ValueFilter, InstanceImageBase):
schema = type_schema('image', rinherit=ValueFilter.schema)
schema_alias = False
def get_permissions(self):
return self.manager.get_resource_manager('ami').get_permissions()
def process(self, resources, event=None):
self.prefetch_instance_images(resources)
return super(InstanceImage, self).process(resources, event)
def __call__(self, i):
image = self.get_instance_image(i)
# Finally, if we have no image...
if not image:
self.log.warning(
"Could not locate image for instance:%s ami:%s" % (
i['InstanceId'], i["ImageId"]))
# Match instead on empty skeleton?
return False
return self.match(image)
@filters.register('offhour')
class InstanceOffHour(OffHour, StateTransitionFilter):
"""Custodian OffHour filter
Filters running EC2 instances with the intent to stop at a given hour of
the day. A list of days to excluded can be included as a list of strings
with the format YYYY-MM-DD. Alternatively, the list (using the same syntax)
can be taken from a specified url.
:Example:
.. code-block:: yaml
policies:
- name: offhour-evening-stop
resource: ec2
filters:
- type: offhour
tag: custodian_downtime
default_tz: et
offhour: 20
actions:
- stop
- name: offhour-evening-stop-skip-holidays
resource: ec2
filters:
- type: offhour
tag: custodian_downtime
default_tz: et
offhour: 20
skip-days: ['2017-12-25']
actions:
- stop
- name: offhour-evening-stop-skip-holidays-from
resource: ec2
filters:
- type: offhour
tag: custodian_downtime
default_tz: et
offhour: 20
skip-days-from:
expr: 0
format: csv
url: 's3://location/holidays.csv'
actions:
- stop
"""
valid_origin_states = ('running',)
def process(self, resources, event=None):
return super(InstanceOffHour, self).process(
self.filter_instance_state(resources))
@filters.register('network-location')
class EC2NetworkLocation(net_filters.NetworkLocation, StateTransitionFilter):
valid_origin_states = ('pending', 'running', 'shutting-down', 'stopping',
'stopped')
def process(self, resources, event=None):
resources = self.filter_instance_state(resources)
if not resources:
return []
return super(EC2NetworkLocation, self).process(resources)
@filters.register('onhour')
class InstanceOnHour(OnHour, StateTransitionFilter):
"""Custodian OnHour filter
Filters stopped EC2 instances with the intent to start at a given hour of
the day. A list of days to excluded can be included as a list of strings
with the format YYYY-MM-DD. Alternatively, the list (using the same syntax)
can be taken from a specified url.
:Example:
.. code-block:: yaml
policies:
- name: onhour-morning-start
resource: ec2
filters:
- type: onhour
tag: custodian_downtime
default_tz: et
onhour: 6
actions:
- start
- name: onhour-morning-start-skip-holidays
resource: ec2
filters:
- type: onhour
tag: custodian_downtime
default_tz: et
onhour: 6
skip-days: ['2017-12-25']
actions:
- start
- name: onhour-morning-start-skip-holidays-from
resource: ec2
filters:
- type: onhour
tag: custodian_downtime
default_tz: et
onhour: 6
skip-days-from:
expr: 0
format: csv
url: 's3://location/holidays.csv'
actions:
- start
"""
valid_origin_states = ('stopped',)
def process(self, resources, event=None):
return super(InstanceOnHour, self).process(
self.filter_instance_state(resources))
@filters.register('ephemeral')
class EphemeralInstanceFilter(Filter):
"""EC2 instances with ephemeral storage
Filters EC2 instances that have ephemeral storage (an instance-store backed
root device)
:Example:
.. code-block:: yaml
policies:
- name: ec2-ephemeral-instances
resource: ec2
filters:
- type: ephemeral
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html
"""
schema = type_schema('ephemeral')
def __call__(self, i):
return self.is_ephemeral(i)
@staticmethod
def is_ephemeral(i):
for bd in i.get('BlockDeviceMappings', []):
if bd['DeviceName'] in ('/dev/sda1', '/dev/xvda', 'xvda'):
if 'Ebs' in bd:
return False
return True
return True
@filters.register('instance-uptime')
class UpTimeFilter(AgeFilter):
date_attribute = "LaunchTime"
schema = type_schema(
'instance-uptime',
op={'$ref': '#/definitions/filters_common/comparison_operators'},
days={'type': 'number'})
@filters.register('instance-age')
class InstanceAgeFilter(AgeFilter):
"""Filters instances based on their age (in days)
:Example:
.. code-block:: yaml
policies:
- name: ec2-30-days-plus
resource: ec2
filters:
- type: instance-age
op: ge
days: 30
"""
date_attribute = "LaunchTime"
ebs_key_func = operator.itemgetter('AttachTime')
schema = type_schema(
'instance-age',
op={'$ref': '#/definitions/filters_common/comparison_operators'},
days={'type': 'number'},
hours={'type': 'number'},
minutes={'type': 'number'})
def get_resource_date(self, i):
# LaunchTime is basically how long has the instance
# been on, use the oldest ebs vol attach time
ebs_vols = [
block['Ebs'] for block in i['BlockDeviceMappings']
if 'Ebs' in block]
if not ebs_vols:
# Fall back to using age attribute (ephemeral instances)
return super(InstanceAgeFilter, self).get_resource_date(i)
# Lexographical sort on date
ebs_vols = sorted(ebs_vols, key=self.ebs_key_func)
return ebs_vols[0]['AttachTime']
@filters.register('default-vpc')
class DefaultVpc(DefaultVpcBase):
""" Matches if an ec2 database is in the default vpc
"""
schema = type_schema('default-vpc')
def __call__(self, ec2):
return ec2.get('VpcId') and self.match(ec2.get('VpcId')) or False
def deserialize_user_data(user_data):
data = base64.b64decode(user_data)
# try raw and compressed
try:
return data.decode('utf8')
except UnicodeDecodeError:
return zlib.decompress(data, 16).decode('utf8')
@filters.register('user-data')
class UserData(ValueFilter):
"""Filter on EC2 instances which have matching userdata.
Note: It is highly recommended to use regexes with the ?sm flags, since Custodian
uses re.match() and userdata spans multiple lines.
:example:
.. code-block:: yaml
policies:
- name: ec2_userdata_stop
resource: ec2
filters:
- type: user-data
op: regex
value: (?smi).*password=
actions:
- stop
"""
schema = type_schema('user-data', rinherit=ValueFilter.schema)
schema_alias = False
batch_size = 50
annotation = 'c7n:user-data'
permissions = ('ec2:DescribeInstanceAttribute',)
def __init__(self, data, manager):
super(UserData, self).__init__(data, manager)
self.data['key'] = '"c7n:user-data"'
def process(self, resources, event=None):
client = utils.local_session(self.manager.session_factory).client('ec2')
results = []
with self.executor_factory(max_workers=3) as w:
futures = {}
for instance_set in utils.chunks(resources, self.batch_size):
futures[w.submit(
self.process_instance_set,
client, instance_set)] = instance_set
for f in as_completed(futures):
if f.exception():
self.log.error(
"Error processing userdata on instance set %s", f.exception())
results.extend(f.result())
return results
def process_instance_set(self, client, resources):
results = []
for r in resources:
if self.annotation not in r:
try:
result = client.describe_instance_attribute(
Attribute='userData',
InstanceId=r['InstanceId'])
except ClientError as e:
if e.response['Error']['Code'] == 'InvalidInstanceId.NotFound':
continue
if 'Value' not in result['UserData']:
r[self.annotation] = None
else:
r[self.annotation] = deserialize_user_data(
result['UserData']['Value'])
if self.match(r):
results.append(r)
return results
@filters.register('singleton')
class SingletonFilter(Filter, StateTransitionFilter):
"""EC2 instances without autoscaling or a recover alarm
Filters EC2 instances that are not members of an autoscaling group
and do not have Cloudwatch recover alarms.
:Example:
.. code-block:: yaml
policies:
- name: ec2-recover-instances
resource: ec2
filters:
- singleton
actions:
- type: tag
key: problem
value: instance is not resilient
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-recover.html
"""
schema = type_schema('singleton')
permissions = ('cloudwatch:DescribeAlarmsForMetric',)
valid_origin_states = ('running', 'stopped', 'pending', 'stopping')
in_asg = ValueFilter({
'key': 'tag:aws:autoscaling:groupName',
'value': 'not-null'}).validate()
def process(self, instances, event=None):
return super(SingletonFilter, self).process(
self.filter_instance_state(instances))
def __call__(self, i):
if self.in_asg(i):
return False
else:
return not self.has_recover_alarm(i)
def has_recover_alarm(self, i):
client = utils.local_session(self.manager.session_factory).client('cloudwatch')
alarms = client.describe_alarms_for_metric(
MetricName='StatusCheckFailed_System',
Namespace='AWS/EC2',
Dimensions=[
{
'Name': 'InstanceId',
'Value': i['InstanceId']
}
]
)
for i in alarms['MetricAlarms']:
for a in i['AlarmActions']:
if (
a.startswith('arn:aws:automate:') and
a.endswith(':ec2:recover')
):
return True
return False
@EC2.filter_registry.register('ssm')
class SsmStatus(ValueFilter):
"""Filter ec2 instances by their ssm status information.
:Example:
Find ubuntu 18.04 instances are active with ssm.
.. code-block:: yaml
policies:
- name: ec2-ssm-check
resource: ec2
filters:
- type: ssm
key: PingStatus
value: Online
- type: ssm
key: PlatformName
value: Ubuntu
- type: ssm
key: PlatformVersion
value: 18.04
"""
schema = type_schema('ssm', rinherit=ValueFilter.schema)
schema_alias = False
permissions = ('ssm:DescribeInstanceInformation',)
annotation = 'c7n:SsmState'
def process(self, resources, event=None):
client = utils.local_session(self.manager.session_factory).client('ssm')
results = []
for resource_set in utils.chunks(
[r for r in resources if self.annotation not in r], 50):
self.process_resource_set(client, resource_set)
for r in resources:
if self.match(r[self.annotation]):
results.append(r)
return results
def process_resource_set(self, client, resources):
instance_ids = [i['InstanceId'] for i in resources]
info_map = {
info['InstanceId']: info for info in
client.describe_instance_information(
Filters=[{'Key': 'InstanceIds', 'Values': instance_ids}]).get(
'InstanceInformationList', [])}
for r in resources:
r[self.annotation] = info_map.get(r['InstanceId'], {})
@EC2.action_registry.register("post-finding")
class InstanceFinding(PostFinding):
def format_resource(self, r):
ip_addresses = jmespath.search(
"NetworkInterfaces[].PrivateIpAddresses[].PrivateIpAddress", r)
# limit to max 10 ip addresses, per security hub service limits
ip_addresses = ip_addresses and ip_addresses[:10] or ip_addresses
details = {
"Type": r["InstanceType"],
"ImageId": r["ImageId"],
"IpV4Addresses": ip_addresses,
"KeyName": r.get("KeyName"),
"LaunchedAt": r["LaunchTime"].isoformat()
}
if "VpcId" in r:
details["VpcId"] = r["VpcId"]
if "SubnetId" in r:
details["SubnetId"] = r["SubnetId"]
if "IamInstanceProfile" in r:
details["IamInstanceProfileArn"] = r["IamInstanceProfile"]["Arn"]
instance = {
"Type": "AwsEc2Instance",
"Id": "arn:{}:ec2:{}:{}:instance/{}".format(
utils.REGION_PARTITION_MAP.get(self.manager.config.region, 'aws'),
self.manager.config.region,
self.manager.config.account_id,
r["InstanceId"]),
"Region": self.manager.config.region,
"Tags": {t["Key"]: t["Value"] for t in r.get("Tags", [])},
"Details": {"AwsEc2Instance": filter_empty(details)},
}
instance = filter_empty(instance)
return instance
@actions.register('start')
class Start(BaseAction, StateTransitionFilter):
"""Starts a previously stopped EC2 instance.
:Example:
.. code-block:: yaml
policies:
- name: ec2-start-stopped-instances
resource: ec2
query:
- instance-state-name: stopped
actions:
- start
http://docs.aws.amazon.com/cli/latest/reference/ec2/start-instances.html
"""
valid_origin_states = ('stopped',)
schema = type_schema('start')
permissions = ('ec2:StartInstances',)
batch_size = 10
exception = None
def _filter_ec2_with_volumes(self, instances):
return [i for i in instances if len(i['BlockDeviceMappings']) > 0]
def process(self, instances):
instances = self._filter_ec2_with_volumes(
self.filter_instance_state(instances))
if not len(instances):
return
client = utils.local_session(self.manager.session_factory).client('ec2')
failures = {}
# Play nice around aws having insufficient capacity...
for itype, t_instances in utils.group_by(
instances, 'InstanceType').items():
for izone, z_instances in utils.group_by(
t_instances, 'Placement.AvailabilityZone').items():
for batch in utils.chunks(z_instances, self.batch_size):
fails = self.process_instance_set(client, batch, itype, izone)
if fails:
failures["%s %s" % (itype, izone)] = [i['InstanceId'] for i in batch]
if failures:
fail_count = sum(map(len, failures.values()))
msg = "Could not start %d of %d instances %s" % (
fail_count, len(instances), utils.dumps(failures))
self.log.warning(msg)
raise RuntimeError(msg)
def process_instance_set(self, client, instances, itype, izone):
# Setup retry with insufficient capacity as well
retryable = ('InsufficientInstanceCapacity', 'RequestLimitExceeded',
'Client.RequestLimitExceeded'),
retry = utils.get_retry(retryable, max_attempts=5)
instance_ids = [i['InstanceId'] for i in instances]
while instance_ids: