def test_auto_tag_creator(self): session_factory = self.replay_flight_data('test_ec2_autotag_creator') policy = self.load_policy( { 'name': 'ec2-auto-tag', 'resource': 'ec2', 'mode': { 'type': 'cloudtrail', 'events': ['RunInstances'] }, 'actions': [{ 'type': 'auto-tag-user', 'tag': 'Owner' }] }, session_factory=session_factory) event = { 'detail': event_data('event-cloud-trail-run-instance-creator.json'), 'debug': True } resources = policy.push(event, None) self.assertEqual(len(resources), 1) # Verify tag added session = session_factory() instances = query_instances(session, InstanceIds=[resources[0]['InstanceId']]) tags = {t['Key']: t['Value'] for t in instances[0]['Tags']} self.assertEqual(tags['Owner'], 'c7nbot') # Verify we don't overwrite extant client = session.client('ec2') client.create_tags(Resources=[resources[0]['InstanceId']], Tags=[{ 'Key': 'Owner', 'Value': 'Bob' }]) policy = self.load_policy( { 'name': 'ec2-auto-tag', 'resource': 'ec2', 'mode': { 'type': 'cloudtrail', 'events': ['RunInstances'] }, 'actions': [{ 'type': 'auto-tag-user', 'tag': 'Owner' }] }, session_factory=session_factory) resources = policy.push(event, None) instances = query_instances(session, InstanceIds=[resources[0]['InstanceId']]) tags = {t['Key']: t['Value'] for t in instances[0]['Tags']} self.assertEqual(tags['Owner'], 'Bob')
def test_auto_tag_creator(self): session_factory = self.replay_flight_data('test_ec2_autotag_creator') policy = self.load_policy({ 'name': 'ec2-auto-tag', 'resource': 'ec2', 'mode': { 'type': 'cloudtrail', 'events': ['RunInstances']}, 'actions': [ {'type': 'auto-tag-user', 'tag': 'Owner'}] }, session_factory=session_factory) event = { 'detail': event_data('event-cloud-trail-run-instance-creator.json'), 'debug': True} resources = policy.push(event, None) self.assertEqual(len(resources), 1) # Verify tag added session = session_factory() instances = query_instances( session, InstanceIds=[resources[0]['InstanceId']]) tags = {t['Key']: t['Value'] for t in instances[0]['Tags']} self.assertEqual(tags['Owner'], 'c7nbot') # Verify we don't overwrite extant client = session.client('ec2') client.create_tags( Resources=[resources[0]['InstanceId']], Tags=[{'Key': 'Owner', 'Value': 'Bob'}]) policy = self.load_policy({ 'name': 'ec2-auto-tag', 'resource': 'ec2', 'mode': { 'type': 'cloudtrail', 'events': ['RunInstances']}, 'actions': [ {'type': 'auto-tag-user', 'tag': 'Owner'}] }, session_factory=session_factory) resources = policy.push(event, None) instances = query_instances( session, InstanceIds=[resources[0]['InstanceId']]) tags = {t['Key']: t['Value'] for t in instances[0]['Tags']} self.assertEqual(tags['Owner'], 'Bob')
def test_ec2_reboot(self): session_factory = self.replay_flight_data("test_ec2_reboot") policy = self.load_policy( { "name": "ec2-test-reboot", "resource": "ec2", "filters": [{"tag:Testing": "not-null"}], "actions": [{"type": "reboot"}], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 2) running = [] for i in resources: if i["State"]["Name"] == "running": running.append(i["InstanceId"]) if self.recording: time.sleep(25) instances = utils.query_instances( session_factory(), InstanceIds=[r["InstanceId"] for r in resources] ) cur_running = [] for i in instances: if i["State"]["Name"] == "running": cur_running.append(i["InstanceId"]) cur_running.sort() running.sort() self.assertEqual(cur_running, running)
def process(self, volumes): original_count = len(volumes) volumes = [v for v in volumes if not v['Encrypted'] or not v['Attachments']] log.debug( "EncryptVolumes filtered from %d to %d " " unencrypted attached volumes" % ( original_count, len(volumes))) # Group volumes by instance id instance_vol_map = {} for v in volumes: instance_id = v['Attachments'][0]['InstanceId'] instance_vol_map.setdefault(instance_id, []).append(v) # Query instances to find current instance state self.instance_map = { i['InstanceId']: i for i in query_instances( local_session(self.manager.session_factory), InstanceIds=instance_vol_map.keys())} with self.executor_factory(max_workers=10) as w: futures = {} for instance_id, vol_set in instance_vol_map.items(): futures[w.submit( self.process_volume, instance_id, vol_set)] = instance_id for f in as_completed(futures): if f.exception(): instance_id = futures[f] log.error( "Exception processing instance:%s volset: %s \n %s" % ( instance_id, instance_vol_map[instance_id], f.exception()))
def process(self, volumes): original_count = len(volumes) volumes = [v for v in volumes if not v['Encrypted'] or not v['Attachments']] log.debug( "EncryptVolumes filtered from %d to %d " " unencrypted attached volumes" % ( original_count, len(volumes))) # Group volumes by instance id instance_vol_map = {} for v in volumes: instance_id = v['Attachments'][0]['InstanceId'] instance_vol_map.setdefault(instance_id, []).append(v) # Query instances to find current instance state self.instance_map = { i['InstanceId']: i for i in query_instances( local_session(self.manager.session_factory), InstanceIds=instance_vol_map.keys())} with self.executor_factory(max_workers=10) as w: futures = {} for instance_id, vol_set in instance_vol_map.items(): futures[w.submit( self.process_volume, instance_id, vol_set)] = instance_id for f in as_completed(futures): if f.exception(): instance_id = futures[f] log.error( "Exception processing instance:%s volset: %s \n %s" % ( instance_id, instance_vol_map[instance_id], f.exception()))
def test_ec2_reboot(self): session_factory = self.replay_flight_data('test_ec2_reboot') policy = self.load_policy( { 'name': 'ec2-test-reboot', 'resource': 'ec2', 'filters': [{ 'tag:Testing': 'not-null' }], 'actions': [{ 'type': 'reboot' }] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 2) running = [] for i in resources: if i['State']['Name'] == 'running': running.append(i['InstanceId']) if self.recording: time.sleep(25) instances = utils.query_instances( session_factory(), InstanceIds=[r['InstanceId'] for r in resources]) cur_running = [] for i in instances: if i['State']['Name'] == 'running': cur_running.append(i['InstanceId']) cur_running.sort() running.sort() self.assertEqual(cur_running, running)
def test_auto_tag_assumed(self): # verify auto tag works with assumed roles and can optionally update session_factory = self.replay_flight_data('test_ec2_autotag_assumed') policy = self.load_policy({ 'name': 'ec2-auto-tag', 'resource': 'ec2', 'mode': { 'type': 'cloudtrail', 'events': ['RunInstances']}, 'actions': [ {'type': 'auto-tag-user', 'update': True, 'tag': 'Owner'}] }, session_factory=session_factory) event = { 'detail': event_data( 'event-cloud-trail-run-instance-creator-assumed.json'), 'debug': True} resources = policy.push(event, None) self.assertEqual(len(resources), 1) tags = {t['Key']: t['Value'] for t in resources[0]['Tags']} self.assertEqual(tags['Owner'], 'Bob') session = session_factory() instances = query_instances( session, InstanceIds=[resources[0]['InstanceId']]) tags = {t['Key']: t['Value'] for t in instances[0]['Tags']} self.assertEqual(tags['Owner'], 'Radiant')
def test_ec2_reboot(self): session_factory = self.replay_flight_data("test_ec2_reboot") policy = self.load_policy( { "name": "ec2-test-reboot", "resource": "ec2", "filters": [{"tag:Testing": "not-null"}], "actions": [{"type": "reboot"}], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 2) running = [] for i in resources: if i["State"]["Name"] == "running": running.append(i["InstanceId"]) if self.recording: time.sleep(25) instances = utils.query_instances( session_factory(), InstanceIds=[r["InstanceId"] for r in resources] ) cur_running = [] for i in instances: if i["State"]["Name"] == "running": cur_running.append(i["InstanceId"]) cur_running.sort() running.sort() self.assertEqual(cur_running, running)
def test_auto_tag_assumed(self): # verify auto tag works with assumed roles and can optionally update session_factory = self.replay_flight_data("test_ec2_autotag_assumed") policy = self.load_policy( { "name": "ec2-auto-tag", "resource": "ec2", "mode": {"type": "cloudtrail", "events": ["RunInstances"]}, "actions": [{"type": "auto-tag-user", "update": True, "tag": "Owner"}], }, session_factory=session_factory, ) event = { "detail": event_data("event-cloud-trail-run-instance-creator-assumed.json"), "debug": True, } resources = policy.push(event, None) self.assertEqual(len(resources), 1) tags = {t["Key"]: t["Value"] for t in resources[0]["Tags"]} self.assertEqual(tags["Owner"], "Bob") session = session_factory() instances = query_instances(session, InstanceIds=[resources[0]["InstanceId"]]) tags = {t["Key"]: t["Value"] for t in instances[0]["Tags"]} self.assertEqual(tags["Owner"], "Radiant")
def test_ec2_reboot(self): session_factory = self.replay_flight_data( 'test_ec2_reboot') policy = self.load_policy({ 'name': 'ec2-test-reboot', 'resource': 'ec2', 'filters': [ {'tag:Testing': 'not-null'}], 'actions': [ {'type': 'reboot'}]}, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 2) running = [] for i in resources: if i['State']['Name'] == 'running': running.append(i['InstanceId']) if self.recording: time.sleep(25) instances = utils.query_instances( session_factory(), InstanceIds=[r['InstanceId'] for r in resources]) cur_running = [] for i in instances: if i['State']['Name'] == 'running': cur_running.append(i['InstanceId']) cur_running.sort() running.sort() self.assertEqual(cur_running, running)
def test_auto_tag_assumed(self): # verify auto tag works with assumed roles and can optionally update session_factory = self.replay_flight_data('test_ec2_autotag_assumed') policy = self.load_policy({ 'name': 'ec2-auto-tag', 'resource': 'ec2', 'mode': { 'type': 'cloudtrail', 'events': ['RunInstances']}, 'actions': [ {'type': 'auto-tag-user', 'update': True, 'tag': 'Owner'}] }, session_factory=session_factory) event = { 'detail': event_data( 'event-cloud-trail-run-instance-creator-assumed.json'), 'debug': True} resources = policy.push(event, None) self.assertEqual(len(resources), 1) tags = {t['Key']: t['Value'] for t in resources[0]['Tags']} self.assertEqual(tags['Owner'], 'Bob') session = session_factory() instances = query_instances( session, InstanceIds=[resources[0]['InstanceId']]) tags = {t['Key']: t['Value'] for t in instances[0]['Tags']} self.assertEqual(tags['Owner'], 'Radiant')
def get_instance_mapping(self, resources): instance_ids = [r['Attachments'][0]['InstanceId'] for r in resources] instances = query_instances(local_session( self.manager.session_factory), InstanceIds=instance_ids) self.log.debug("Queried %d instances for %d volumes" % (len(instances), len(resources))) return {i['InstanceId']: i for i in instances}
def get_instance_mapping(self, resources): instance_ids = [r['Attachments'][0]['InstanceId'] for r in resources] instances = query_instances( local_session(self.manager.session_factory), InstanceIds=instance_ids) self.log.debug("Queried %d instances for %d volumes" % ( len(instances), len(resources))) return {i['InstanceId']: i for i in instances}
def test_auto_tag_creator(self): session_factory = self.replay_flight_data("test_ec2_autotag_creator") policy = self.load_policy( { "name": "ec2-auto-tag", "resource": "ec2", "mode": {"type": "cloudtrail", "events": ["RunInstances"]}, "actions": [{"type": "auto-tag-user", "tag": "Owner"}], }, session_factory=session_factory, ) event = { "detail": event_data("event-cloud-trail-run-instance-creator.json"), "debug": True, } resources = policy.push(event, None) self.assertEqual(len(resources), 1) # Verify tag added session = session_factory() instances = query_instances(session, InstanceIds=[resources[0]["InstanceId"]]) tags = {t["Key"]: t["Value"] for t in instances[0]["Tags"]} self.assertEqual(tags["Owner"], "c7nbot") # Verify we don't overwrite extant client = session.client("ec2") client.create_tags( Resources=[resources[0]["InstanceId"]], Tags=[{"Key": "Owner", "Value": "Bob"}], ) policy = self.load_policy( { "name": "ec2-auto-tag", "resource": "ec2", "mode": {"type": "cloudtrail", "events": ["RunInstances"]}, "actions": [{"type": "auto-tag-user", "tag": "Owner"}], }, session_factory=session_factory, ) resources = policy.push(event, None) instances = query_instances(session, InstanceIds=[resources[0]["InstanceId"]]) tags = {t["Key"]: t["Value"] for t in instances[0]["Tags"]} self.assertEqual(tags["Owner"], "Bob")
def test_ec2_resize(self): # preconditions - three instances (2 m4.4xlarge, 1 m4.1xlarge) # one of the instances stopped session_factory = self.replay_flight_data('test_ec2_resize') policy = self.load_policy({ 'name': 'ec2-resize', 'resource': 'ec2', 'filters': [ {'type': 'value', 'key': 'State.Name', 'value': ['running', 'stopped'], 'op': 'in'}, {'type': 'value', 'key': 'InstanceType', 'value': ['m4.2xlarge', 'm4.4xlarge'], 'op': 'in'}, ], 'actions': [ {'type': 'resize', 'restart': True, 'default': 'm4.large', 'type-map': { 'm4.4xlarge': 'm4.2xlarge'}}] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 3) stopped, running = [], [] for i in resources: if i['State']['Name'] == 'running': running.append(i['InstanceId']) if i['State']['Name'] == 'stopped': stopped.append(i['InstanceId']) instances = utils.query_instances( session_factory(), InstanceIds=[r['InstanceId'] for r in resources]) cur_stopped, cur_running = [], [] for i in instances: if i['State']['Name'] == 'running': cur_running.append(i['InstanceId']) if i['State']['Name'] == 'stopped': cur_stopped.append(i['InstanceId']) cur_running.sort() running.sort() self.assertEqual(cur_stopped, stopped) self.assertEqual(cur_running, running) instance_types = [i['InstanceType'] for i in instances] instance_types.sort() self.assertEqual( instance_types, list(sorted(['m4.large', 'm4.2xlarge', 'm4.2xlarge'])))
def test_ec2_resize(self): # preconditions - three instances (2 m4.4xlarge, 1 m4.1xlarge) # one of the instances stopped session_factory = self.replay_flight_data('test_ec2_resize') policy = self.load_policy({ 'name': 'ec2-resize', 'resource': 'ec2', 'filters': [ {'type': 'value', 'key': 'State.Name', 'value': ['running', 'stopped'], 'op': 'in'}, {'type': 'value', 'key': 'InstanceType', 'value': ['m4.2xlarge', 'm4.4xlarge'], 'op': 'in'}, ], 'actions': [ {'type': 'resize', 'restart': True, 'default': 'm4.large', 'type-map': { 'm4.4xlarge': 'm4.2xlarge'}}] }, session_factory=session_factory) resources = policy.run() self.assertEqual(len(resources), 3) stopped, running = [], [] for i in resources: if i['State']['Name'] == 'running': running.append(i['InstanceId']) if i['State']['Name'] == 'stopped': stopped.append(i['InstanceId']) instances = utils.query_instances( session_factory(), InstanceIds=[r['InstanceId'] for r in resources]) cur_stopped, cur_running = [], [] for i in instances: if i['State']['Name'] == 'running': cur_running.append(i['InstanceId']) if i['State']['Name'] == 'stopped': cur_stopped.append(i['InstanceId']) cur_running.sort() running.sort() self.assertEqual(cur_stopped, stopped) self.assertEqual(cur_running, running) instance_types = [i['InstanceType'] for i in instances] instance_types.sort() self.assertEqual( instance_types, list(sorted(['m4.large', 'm4.2xlarge', 'm4.2xlarge'])))
def get_instance_map(self, asgs): instance_ids = [ i['InstanceId'] for i in list( itertools.chain( *[g['Instances'] for g in asgs if g['Instances']])) ] if not instance_ids: return {} instances = query_instances(local_session( self.manager.session_factory), InstanceIds=instance_ids) return {i['InstanceId']: i for i in instances}
def get_instance_map(self, asgs): instance_ids = [ i['InstanceId'] for i in list(itertools.chain(*[ g['Instances'] for g in asgs if g['Instances']]))] if not instance_ids: return {} instances = query_instances( local_session(self.manager.session_factory), InstanceIds=instance_ids) return {i['InstanceId']: i for i in instances}
def test_ec2_terminate(self): # Test conditions: single running instance, with delete protection session_factory = self.replay_flight_data('test_ec2_terminate') p = self.load_policy({ 'name': 'ec2-term', 'resource': 'ec2', 'filters': [{'InstanceId': 'i-017cf4e2a33b853fe'}], 'actions': [ {'type': 'terminate', 'force': True}]}, session_factory=session_factory) resources = p.run() self.assertEqual(len(resources), 1) instances = utils.query_instances( session_factory(), InstanceIds=['i-017cf4e2a33b853fe']) self.assertEqual(instances[0]['State']['Name'], 'shutting-down')
def test_ec2_terminate(self): # Test conditions: single running instance, with delete protection session_factory = self.replay_flight_data('test_ec2_terminate') p = self.load_policy({ 'name': 'ec2-term', 'resource': 'ec2', 'filters': [{'InstanceId': 'i-017cf4e2a33b853fe'}], 'actions': [ {'type': 'terminate', 'force': True}]}, session_factory=session_factory) resources = p.run() self.assertEqual(len(resources), 1) instances = utils.query_instances( session_factory(), InstanceIds=['i-017cf4e2a33b853fe']) self.assertEqual(instances[0]['State']['Name'], 'shutting-down')
def process_volume_set(self, volume_set): instance_vol_map = {} for v in volume_set: instance_vol_map.setdefault( v['Attachments'][0]['InstanceId'], []).append(v) instance_map = {i['InstanceId']: i for i in query_instances( local_session(self.manager.session_factory), InstanceIds=instance_vol_map.keys())} for i in instance_vol_map: try: self.process_instance_volumes( instance_map[i], instance_vol_map[i]) except Exception as e: self.log.exception( "Error copying instance tags to volumes \n %s" % e)
def process_volume_set(self, volume_set): instance_vol_map = {} for v in volume_set: instance_vol_map.setdefault( v['Attachments'][0]['InstanceId'], []).append(v) instance_map = {i['InstanceId']: i for i in query_instances( local_session(self.manager.session_factory), InstanceIds=instance_vol_map.keys())} for i in instance_vol_map: try: self.process_instance_volumes( instance_map[i], instance_vol_map[i]) except Exception as e: self.log.exception( "Error copying instance tags to volumes \n %s" % e)
def test_ec2_terminate(self): # Test conditions: single running instance, with delete protection session_factory = self.replay_flight_data("test_ec2_terminate") p = self.load_policy( { "name": "ec2-term", "resource": "ec2", "filters": [{"InstanceId": "i-017cf4e2a33b853fe"}], "actions": [{"type": "terminate", "force": True}], }, session_factory=session_factory, ) resources = p.run() self.assertEqual(len(resources), 1) instances = utils.query_instances( session_factory(), InstanceIds=["i-017cf4e2a33b853fe"] ) self.assertEqual(instances[0]["State"]["Name"], "shutting-down")
def test_ec2_terminate(self): # Test conditions: single running instance, with delete protection session_factory = self.replay_flight_data("test_ec2_terminate") p = self.load_policy( { "name": "ec2-term", "resource": "ec2", "filters": [{"InstanceId": "i-017cf4e2a33b853fe"}], "actions": [{"type": "terminate", "force": True}], }, session_factory=session_factory, ) resources = p.run() self.assertEqual(len(resources), 1) instances = utils.query_instances( session_factory(), InstanceIds=["i-017cf4e2a33b853fe"] ) self.assertEqual(instances[0]["State"]["Name"], "shutting-down")
def test_auto_tag_assumed(self): # verify auto tag works with assumed roles and can optionally update session_factory = self.replay_flight_data("test_ec2_autotag_assumed") policy = self.load_policy( { "name": "ec2-auto-tag", "resource": "ec2", "mode": { "type": "cloudtrail", "events": ["RunInstances"] }, "actions": [{ "type": "auto-tag-user", "update": True, "tag": "Owner" }], }, session_factory=session_factory, ) event = { "detail": event_data("event-cloud-trail-run-instance-creator-assumed.json"), "debug": True, } resources = policy.push(event, None) self.assertEqual(len(resources), 1) tags = {t["Key"]: t["Value"] for t in resources[0]["Tags"]} self.assertEqual(tags["Owner"], "Bob") session = session_factory() instances = query_instances(session, InstanceIds=[resources[0]["InstanceId"]]) tags = {t["Key"]: t["Value"] for t in instances[0]["Tags"]} self.assertEqual(tags["Owner"], "Radiant")
def get_resources(self, resource_ids): return utils.query_instances( None, client=self.session_factory().client('ec2'), InstanceIds=resource_ids)
def get_resources(self, resource_ids): return utils.query_instances( None, client=self.session_factory().client('ec2'), InstanceIds=resource_ids)
def test_ec2_resize(self): # preconditions - three instances (2 m4.4xlarge, 1 m4.1xlarge) # one of the instances stopped session_factory = self.replay_flight_data("test_ec2_resize") policy = self.load_policy( { "name": "ec2-resize", "resource": "ec2", "filters": [ { "type": "value", "key": "State.Name", "value": ["running", "stopped"], "op": "in", }, { "type": "value", "key": "InstanceType", "value": ["m4.2xlarge", "m4.4xlarge"], "op": "in", }, ], "actions": [{ "type": "resize", "restart": True, "default": "m4.large", "type-map": { "m4.4xlarge": "m4.2xlarge" }, }], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 3) stopped, running = [], [] for i in resources: if i["State"]["Name"] == "running": running.append(i["InstanceId"]) if i["State"]["Name"] == "stopped": stopped.append(i["InstanceId"]) instances = utils.query_instances( session_factory(), InstanceIds=[r["InstanceId"] for r in resources]) cur_stopped, cur_running = [], [] for i in instances: if i["State"]["Name"] == "running": cur_running.append(i["InstanceId"]) if i["State"]["Name"] == "stopped": cur_stopped.append(i["InstanceId"]) cur_running.sort() running.sort() self.assertEqual(cur_stopped, stopped) self.assertEqual(cur_running, running) instance_types = [i["InstanceType"] for i in instances] instance_types.sort() self.assertEqual( instance_types, list(sorted(["m4.large", "m4.2xlarge", "m4.2xlarge"])))
def test_auto_tag_creator(self): session_factory = self.replay_flight_data("test_ec2_autotag_creator") policy = self.load_policy( { "name": "ec2-auto-tag", "resource": "ec2", "mode": { "type": "cloudtrail", "events": ["RunInstances"] }, "actions": [{ "type": "auto-tag-user", "tag": "Owner" }], }, session_factory=session_factory, ) event = { "detail": event_data("event-cloud-trail-run-instance-creator.json"), "debug": True, } resources = policy.push(event, None) self.assertEqual(len(resources), 1) # Verify tag added session = session_factory() instances = query_instances(session, InstanceIds=[resources[0]["InstanceId"]]) tags = {t["Key"]: t["Value"] for t in instances[0]["Tags"]} self.assertEqual(tags["Owner"], "c7nbot") # Verify we don't overwrite extant client = session.client("ec2") client.create_tags( Resources=[resources[0]["InstanceId"]], Tags=[{ "Key": "Owner", "Value": "Bob" }], ) policy = self.load_policy( { "name": "ec2-auto-tag", "resource": "ec2", "mode": { "type": "cloudtrail", "events": ["RunInstances"] }, "actions": [{ "type": "auto-tag-user", "tag": "Owner" }], }, session_factory=session_factory, ) resources = policy.push(event, None) instances = query_instances(session, InstanceIds=[resources[0]["InstanceId"]]) tags = {t["Key"]: t["Value"] for t in instances[0]["Tags"]} self.assertEqual(tags["Owner"], "Bob")
def test_ec2_resize(self): # preconditions - three instances (2 m4.4xlarge, 1 m4.1xlarge) # one of the instances stopped session_factory = self.replay_flight_data("test_ec2_resize") policy = self.load_policy( { "name": "ec2-resize", "resource": "ec2", "filters": [ { "type": "value", "key": "State.Name", "value": ["running", "stopped"], "op": "in", }, { "type": "value", "key": "InstanceType", "value": ["m4.2xlarge", "m4.4xlarge"], "op": "in", }, ], "actions": [ { "type": "resize", "restart": True, "default": "m4.large", "type-map": {"m4.4xlarge": "m4.2xlarge"}, } ], }, session_factory=session_factory, ) resources = policy.run() self.assertEqual(len(resources), 3) stopped, running = [], [] for i in resources: if i["State"]["Name"] == "running": running.append(i["InstanceId"]) if i["State"]["Name"] == "stopped": stopped.append(i["InstanceId"]) instances = utils.query_instances( session_factory(), InstanceIds=[r["InstanceId"] for r in resources] ) cur_stopped, cur_running = [], [] for i in instances: if i["State"]["Name"] == "running": cur_running.append(i["InstanceId"]) if i["State"]["Name"] == "stopped": cur_stopped.append(i["InstanceId"]) cur_running.sort() running.sort() self.assertEqual(cur_stopped, stopped) self.assertEqual(cur_running, running) instance_types = [i["InstanceType"] for i in instances] instance_types.sort() self.assertEqual( instance_types, list(sorted(["m4.large", "m4.2xlarge", "m4.2xlarge"])) )