Repository: cloud-custodian/cloud-custodian

Issue:
aws - asg.image-age filter fails 
**Describe the bug**
A policy that filters asg based off the image-age is failing  

**To Reproduce**
Dry run the policy shown below

**Expected behavior**
Should execute the policy completely

**Background (please complete the following information):**

 - Python Version: 3.7
 - Custodian Version: 0.9.1
 - Tool Version: [if applicable]
 - Cloud Provider: aws
 - Policy: [please exclude any account/sensitive information]
```yaml
 - name: asg-ami
   resource: asg
    filters:
      - "tag:custodian": not-null
      - or:
        - type: image-age
          op: lt
          days: x
```
 - Traceback: [if applicable, please exclude sensitive/account information]
```
custodian run --dryrun --profile abc asg-ami.yml --region us-east-1 -s asg-ami --cache-period 0 -v
2020-05-11 16:26:37,495: custodian.cache:DEBUG Disabling cache
2020-05-11 16:26:37,496: custodian.commands:DEBUG Loaded file asg-ami.yml. Contains 1 policies
2020-05-11 16:26:38,023: custodian.output:DEBUG Storing output with <LogFile asg-ami/custodian-run.log>
2020-05-11 16:26:38,165: custodian.policy:DEBUG Running policy:asg-ami
resource:asg region:us-east-1 c7n:0.9.1
2020-05-11 16:26:51,425: custodian.resources.launchconfig:DEBUG Filtered from 527 to 527 launchconfig
2020-05-11 16:26:51,683: custodian.output:DEBUG metric:PolicyException Count:1 policy:asg-ami
restype:asg
2020-05-11 16:26:51,683: custodian.output:DEBUG metric:ApiCalls Count:28 policy:asg-ami
restype:asg
2020-05-11 16:26:51,683: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
  File "/Users/abc/custodian/cloud-custodian/c7n/policy.py", line 291, in run
    resources = self.policy.resource_manager.resources()
  File "/Users/abc/custodian/cloud-custodian/c7n/query.py", line 465, in resources
    resources = self.filter_resources(resources)
  File "/Users/abc/custodian/cloud-custodian/c7n/manager.py", line 109, in filter_resources
    resources = f.process(resources, event)
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 258, in process
    return self.process_set(resources, event)
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 274, in process_set
    r[rtype_id] for r in f.process(resources, event)])
  File "/Users/abc/custodian/cloud-custodian/c7n/resources/asg.py", line 573, in process
    return super(ImageAgeFilter, self).process(asgs, event)
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 197, in process
    return list(filter(self, resources))
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 677, in __call__
    v = self.get_resource_date(i)
  File "/Users/abc/custodian/cloud-custodian/c7n/resources/asg.py", line 577, in get_resource_date
    ami = self.images.get(cfg.get('ImageId'), {})
AttributeError: 'NoneType' object has no attribute 'get'
2020-05-11 16:26:51,688: custodian.commands:ERROR Error while executing policy asg-ami, continuing
Traceback (most recent call last):
  File "/Users/abc/custodian/cloud-custodian/c7n/commands.py", line 281, in run
    policy()
  File "/Users/abc/custodian/cloud-custodian/c7n/policy.py", line 1031, in __call__
    resources = PullMode(self).run()
  File "/Users/abc/custodian/cloud-custodian/c7n/policy.py", line 291, in run
    resources = self.policy.resource_manager.resources()
  File "/Users/abc/custodian/cloud-custodian/c7n/query.py", line 465, in resources
    resources = self.filter_resources(resources)
  File "/Users/abc/custodian/cloud-custodian/c7n/manager.py", line 109, in filter_resources
    resources = f.process(resources, event)
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 258, in process
    return self.process_set(resources, event)
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 274, in process_set
    r[rtype_id] for r in f.process(resources, event)])
  File "/Users/abc/custodian/cloud-custodian/c7n/resources/asg.py", line 573, in process
    return super(ImageAgeFilter, self).process(asgs, event)
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 197, in process
    return list(filter(self, resources))
  File "/Users/abc/custodian/cloud-custodian/c7n/filters/core.py", line 677, in __call__
    v = self.get_resource_date(i)
  File "/Users/abc/custodian/cloud-custodian/c7n/resources/asg.py", line 577, in get_resource_date
    ami = self.images.get(cfg.get('ImageId'), {})
AttributeError: 'NoneType' object has no attribute 'get'
```
 - `custodian version --debug` output

**Additional context**
Add any other context about the problem here.


Discussion:
PratMis: Will add a small test for it

Patch:
diff --git a/c7n/resources/asg.py b/c7n/resources/asg.py
index dfefff94d..9d01ee426 100644
--- a/c7n/resources/asg.py
+++ b/c7n/resources/asg.py
@@ -574,6 +574,8 @@ class ImageAgeFilter(AgeFilter):
 
     def get_resource_date(self, asg):
         cfg = self.launch_info.get(asg)
+        if cfg is None:
+            cfg = {}
         ami = self.images.get(cfg.get('ImageId'), {})
         return parse(ami.get(
             self.date_attribute, "2000-01-01T01:01:01.000Z"))


Test patch:
diff --git a/tests/data/placebo/test_asg_image_age_filter_deleted_config/autoscaling.DescribeAutoScalingGroups_1.json b/tests/data/placebo/test_asg_image_age_filter_deleted_config/autoscaling.DescribeAutoScalingGroups_1.json
new file mode 100644
index 000000000..7a03850fa
--- /dev/null
+++ b/tests/data/placebo/test_asg_image_age_filter_deleted_config/autoscaling.DescribeAutoScalingGroups_1.json
@@ -0,0 +1,100 @@
+{
+    "status_code": 200,
+    "data": {
+        "AutoScalingGroups": [
+            {
+                "AutoScalingGroupName": "devx",
+                "AutoScalingGroupARN": "arn:aws:autoscaling:us-east-1:644160558196:autoScalingGroup:94c763b0-e6ce-49ee-a7e9-d014090fad73:autoScalingGroupName/devx",
+                "MixedInstancesPolicy": {
+                    "LaunchTemplate": {
+                        "LaunchTemplateSpecification": {
+                            "LaunchTemplateId": "lt-0877401c93c294001",
+                            "LaunchTemplateName": "test",
+                            "Version": "4"
+                        },
+                        "Overrides": [
+                            {
+                                "InstanceType": "t3.medium"
+                            }
+                        ]
+                    },
+                    "InstancesDistribution": {
+                        "OnDemandAllocationStrategy": "prioritized",
+                        "OnDemandBaseCapacity": 1,
+                        "OnDemandPercentageAboveBaseCapacity": 0,
+                        "SpotAllocationStrategy": "capacity-optimized"
+                    }
+                },
+                "MinSize": 0,
+                "MaxSize": 2,
+                "DesiredCapacity": 1,
+                "DefaultCooldown": 300,
+                "AvailabilityZones": [
+                    "us-east-1d",
+                    "us-east-1e"
+                ],
+                "LoadBalancerNames": [],
+                "TargetGroupARNs": [],
+                "HealthCheckType": "EC2",
+                "HealthCheckGracePeriod": 300,
+                "Instances": [
+                    {
+                        "InstanceId": "i-04e252315a3b7940a",
+                        "InstanceType": "t1.micro",
+                        "AvailabilityZone": "us-east-1d",
+                        "LifecycleState": "InService",
+                        "HealthStatus": "Healthy",
+                        "LaunchTemplate": {
+                            "LaunchTemplateId": "lt-0877401c93c294001",
+                            "LaunchTemplateName": "test",
+                            "Version": "4"
+                        },
+                        "ProtectedFromScaleIn": false
+                    }
+                ],
+                "CreatedTime": {
+                    "__class__": "datetime",
+                    "year": 2019,
+                    "month": 10,
+                    "day": 29,
+                    "hour": 14,
+                    "minute": 37,
+                    "second": 33,
+                    "microsecond": 482000
+                },
+                "SuspendedProcesses": [],
+                "VPCZoneIdentifier": "subnet-3a334610,subnet-e3b194de",
+                "EnabledMetrics": [],
+                "Tags": [
+                    {
+                        "ResourceId": "devx",
+                        "ResourceType": "auto-scaling-group",
+                        "Key": "Creator",
+                        "Value": "xyz@gmail.com",
+                        "PropagateAtLaunch": false
+                    },
+                    {
+                        "ResourceId": "devx",
+                        "ResourceType": "auto-scaling-group",
+                        "Key": "Env",
+                        "Value": "App",
+                        "PropagateAtLaunch": true
+                    },
+                    {
+                        "ResourceId": "devx",
+                        "ResourceType": "auto-scaling-group",
+                        "Key": "Owner",
+                        "Value": "test",
+                        "PropagateAtLaunch": true
+                    }
+                ],
+                "TerminationPolicies": [
+                    "Default"
+                ],
+                "NewInstancesProtectedFromScaleIn": false,
+                "ServiceLinkedRoleARN": "arn:aws:iam::644160558196:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
+            }
+        ],
+        "ResponseMetadata": {}
+    }
+}
\ No newline at end of file
diff --git a/tests/data/placebo/test_asg_image_age_filter_deleted_config/ec2.DescribeLaunchTemplateVersions_1.json b/tests/data/placebo/test_asg_image_age_filter_deleted_config/ec2.DescribeLaunchTemplateVersions_1.json
new file mode 100644
index 000000000..178f48a5a
--- /dev/null
+++ b/tests/data/placebo/test_asg_image_age_filter_deleted_config/ec2.DescribeLaunchTemplateVersions_1.json
@@ -0,0 +1,10 @@
+{
+    "status_code": 400,
+    "data": {
+        "Error": {
+            "Code": "InvalidLaunchTemplateId.NotFound",
+            "Message": "The specified launch template, with template ID lt-0877401c93c294001, does not exist."
+        },
+        "ResponseMetadata": {}
+    }
+}
\ No newline at end of file
diff --git a/tests/test_asg.py b/tests/test_asg.py
index 08c5a5bfe..a4b7d75b5 100644
--- a/tests/test_asg.py
+++ b/tests/test_asg.py
@@ -199,6 +199,22 @@ class AutoScalingTest(BaseTest):
         resources = p.run()
         self.assertEqual(len(resources), 1)
 
+    def test_asg_image_age_filter_deleted_config(self):
+        factory = self.replay_flight_data("test_asg_image_age_filter_deleted_config")
+        p = self.load_policy(
+            {
+                "name": "asg-image-age-filter",
+                "resource": "asg",
+                "filters": [
+                    {"tag:Env": "present"},
+                    {"type": "image-age", "days": 5000, "op": "gt"}],
+            },
+            session_factory=factory,
+        )
+        resources = p.run()
+        self.assertEqual(len(resources), 1)
+        self.assertTrue('Env' in resources[0].get('Tags')[1].values())
+
     def test_asg_config_filter(self):
         factory = self.replay_flight_data("test_asg_config_filter")
         p = self.load_policy(