-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathinspector-ami-scanner.yml
More file actions
867 lines (816 loc) · 34.5 KB
/
Copy pathinspector-ami-scanner.yml
File metadata and controls
867 lines (816 loc) · 34.5 KB
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
AWSTemplateFormatVersion: "2010-09-09"
Description: RunsOn AMI vulnerability scanner using Amazon Inspector, Step Functions, Lambda, and temporary EC2 instances.
Parameters:
NotificationEmail:
Type: String
Default: security@runs-on.com
Description: Email address subscribed to scanner run summaries.
ScannerScheduleExpression:
Type: String
Default: cron(0 2 * * ? *)
Description: EventBridge schedule expression for scanner runs.
InspectorReportFormat:
Type: String
Default: JSON
AllowedValues:
- JSON
- CSV
Description: Inspector findings report format.
AmiScanTagName:
Type: String
Default: inspector_scan
Description: AMI tag name owned by this scanner.
AmiScanTagValue:
Type: String
Default: "true"
Description: AMI tag value owned by this scanner.
ProdPrefix:
Type: String
Default: runs-on-v2.2
Description: Published AMI name prefix.
DevPrefix:
Type: String
Default: runs-on-dev
Description: Development AMI name prefix.
ScannerConfigJson:
Type: String
Default: "[]"
Description: JSON array of opted-in image IDs and instance types generated from config.yml.
Rules:
UsEast1Only:
Assertions:
- Assert: !Equals [!Ref AWS::Region, us-east-1]
AssertDescription: This stack is intended to run only in us-east-1.
Resources:
ScannerVpc:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.82.0.0/16
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-vpc
ScannerInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-igw
ScannerVpcGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref ScannerInternetGateway
VpcId: !Ref ScannerVpc
ScannerSubnetA:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: 10.82.0.0/24
MapPublicIpOnLaunch: true
VpcId: !Ref ScannerVpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-public-a
ScannerSubnetB:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: 10.82.1.0/24
MapPublicIpOnLaunch: true
VpcId: !Ref ScannerVpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-public-b
ScannerRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref ScannerVpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-public
ScannerDefaultRoute:
Type: AWS::EC2::Route
DependsOn: ScannerVpcGatewayAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref ScannerInternetGateway
RouteTableId: !Ref ScannerRouteTable
ScannerSubnetARouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref ScannerRouteTable
SubnetId: !Ref ScannerSubnetA
ScannerSubnetBRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref ScannerRouteTable
SubnetId: !Ref ScannerSubnetB
ScannerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Outbound-only security group for temporary Inspector scan instances.
SecurityGroupEgress:
- IpProtocol: "-1"
CidrIp: 0.0.0.0/0
SecurityGroupIngress: []
VpcId: !Ref ScannerVpc
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-scan-instance
ScanInstanceRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-scan-instance
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
ScanInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: !Sub ${AWS::StackName}-scan-instance
Roles:
- !Ref ScanInstanceRole
ReportKey:
Type: AWS::KMS::Key
Properties:
Description: !Sub KMS key for ${AWS::StackName} Inspector findings reports.
EnableKeyRotation: true
KeyPolicy:
Version: "2012-10-17"
Statement:
- Sid: EnableAccountAdministration
Effect: Allow
Principal:
AWS: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:root
Action: kms:*
Resource: "*"
- Sid: AllowInspectorReportEncryption
Effect: Allow
Principal:
Service: inspector2.amazonaws.com
Action:
- kms:Decrypt
- kms:DescribeKey
- kms:GenerateDataKey
Resource: "*"
Condition:
StringEquals:
aws:SourceAccount: !Ref AWS::AccountId
ReportKeyAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub alias/${AWS::StackName}-reports
TargetKeyId: !Ref ReportKey
ReportBucket:
Type: AWS::S3::Bucket
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
KMSMasterKeyID: !GetAtt ReportKey.Arn
SSEAlgorithm: aws:kms
LifecycleConfiguration:
Rules:
- Id: ExpireInspectorReports
Status: Enabled
ExpirationInDays: 90
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
ReportBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ReportBucket
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: AllowInspectorReportExport
Effect: Allow
Principal:
Service: inspector2.amazonaws.com
Action:
- s3:PutObject
- s3:PutObjectAcl
Resource: !Sub ${ReportBucket.Arn}/inspector/*
Condition:
StringEquals:
aws:SourceAccount: !Ref AWS::AccountId
- Sid: AllowInspectorBucketLocation
Effect: Allow
Principal:
Service: inspector2.amazonaws.com
Action: s3:GetBucketLocation
Resource: !GetAtt ReportBucket.Arn
Condition:
StringEquals:
aws:SourceAccount: !Ref AWS::AccountId
ScannerTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub ${AWS::StackName}-summaries
ScannerTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint: !Ref NotificationEmail
Protocol: email
TopicArn: !Ref ScannerTopic
ScannerLambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-lambda
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: ScannerLambdaAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: DescribeEc2ScannerTargets
Effect: Allow
Action:
- ec2:DescribeImages
- ec2:DescribeInstances
Resource: "*"
- Sid: RotateAmiScanTags
Effect: Allow
Action:
- ec2:CreateTags
- ec2:DeleteTags
Resource: !Sub arn:${AWS::Partition}:ec2:${AWS::Region}::image/*
- Sid: TagLaunchedScanInstances
Effect: Allow
Action: ec2:CreateTags
Resource: !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*
Condition:
StringEquals:
ec2:CreateAction: RunInstances
- Sid: LaunchScanInstances
Effect: Allow
Action: ec2:RunInstances
Resource:
- !Sub arn:${AWS::Partition}:ec2:${AWS::Region}::image/*
- !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*
- !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:network-interface/*
- !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:security-group/*
- !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:subnet/*
- !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:volume/*
- Sid: TerminateScanInstances
Effect: Allow
Action: ec2:TerminateInstances
Resource: !Sub arn:${AWS::Partition}:ec2:${AWS::Region}:${AWS::AccountId}:instance/*
- Sid: PassScanInstanceRole
Effect: Allow
Action: iam:PassRole
Resource: !GetAtt ScanInstanceRole.Arn
- Sid: InspectorScanReports
Effect: Allow
Action:
- inspector2:CreateFindingsReport
- inspector2:GetFindingsReportStatus
- inspector2:ListCoverage
- inspector2:ListFindings
Resource: "*"
- Sid: WriteReportBucket
Effect: Allow
Action:
- s3:GetBucketLocation
- s3:PutObject
Resource:
- !GetAtt ReportBucket.Arn
- !Sub ${ReportBucket.Arn}/*
- Sid: UseReportKey
Effect: Allow
Action:
- kms:Decrypt
- kms:DescribeKey
- kms:GenerateDataKey
Resource: !GetAtt ReportKey.Arn
- Sid: PublishScanSummaries
Effect: Allow
Action: sns:Publish
Resource: !Ref ScannerTopic
ScannerFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-scanner
Description: Discovers opted-in AMIs, rotates scan tags, launches scan instances, exports reports, and publishes summaries.
Handler: index.handler
Role: !GetAtt ScannerLambdaRole.Arn
Runtime: python3.12
Timeout: 900
MemorySize: 256
Environment:
Variables:
AMI_SCAN_TAG_NAME: !Ref AmiScanTagName
AMI_SCAN_TAG_VALUE: !Ref AmiScanTagValue
DEV_PREFIX: !Ref DevPrefix
PROD_PREFIX: !Ref ProdPrefix
INSTANCE_PROFILE_NAME: !Ref ScanInstanceProfile
REPORT_BUCKET: !Ref ReportBucket
REPORT_FORMAT: !Ref InspectorReportFormat
REPORT_KMS_KEY_ARN: !GetAtt ReportKey.Arn
SCANNER_CONFIG_JSON: !Ref ScannerConfigJson
SECURITY_GROUP_ID: !Ref ScannerSecurityGroup
SNS_TOPIC_ARN: !Ref ScannerTopic
SUBNET_IDS: !Sub ${ScannerSubnetA},${ScannerSubnetB}
Code:
ZipFile: |
import datetime
import json
import os
import traceback
import boto3
from botocore.exceptions import ClientError
ec2 = boto3.client("ec2")
inspector = boto3.client("inspector2")
sns = boto3.client("sns")
def handler(event, _context):
action = event.get("Action", "Discover")
try:
if action == "Discover":
return discover()
if action == "Launch":
return launch(event["Item"], event["RunId"])
if action == "Poll":
return poll(event["Item"])
if action == "Export":
return export_report(event["Item"])
if action == "PollReport":
return poll_report(event["Item"])
if action == "Notify":
return notify(event["Item"])
if action == "Cleanup":
return cleanup(event.get("Item", event))
raise ValueError(f"unsupported action: {action}")
except Exception as exc:
failed = event.get("Item", {})
failed["error"] = str(exc)
failed["traceback"] = traceback.format_exc()
cleanup(failed)
raise
def scanner_config():
return json.loads(os.environ["SCANNER_CONFIG_JSON"])
def tags_to_dict(tags):
return {tag["Key"]: tag["Value"] for tag in tags or []}
def discover():
tag_name = os.environ["AMI_SCAN_TAG_NAME"]
tag_value = os.environ["AMI_SCAN_TAG_VALUE"]
selected = []
selected_ids = set()
opted_in_ids = {item["image_id"] for item in scanner_config()}
# The scan tag is distributed scanner-owned state on AMIs; rotate it before launch so stale AMIs are not scanned.
for item in scanner_config():
image_id = item["image_id"]
for channel, prefix in (("dev", os.environ["DEV_PREFIX"]), ("prod", os.environ["PROD_PREFIX"])):
images = describe_named_images(f"{prefix}-{image_id}-*")
if not images:
continue
latest = sorted(images, key=lambda image: image["CreationDate"])[-1]
tags = tags_to_dict(latest.get("Tags", []))
if tags.get(tag_name) != tag_value:
ec2.create_tags(Resources=[latest["ImageId"]], Tags=[{"Key": tag_name, "Value": tag_value}])
selected_ids.add(latest["ImageId"])
selected.append({
"ami_id": latest["ImageId"],
"ami_name": latest["Name"],
"channel": channel,
"image_id": image_id,
"instance_type": item["instance_type"]
})
for stale in images:
if stale["ImageId"] != latest["ImageId"] and tags_to_dict(stale.get("Tags", [])).get(tag_name) == tag_value:
ec2.delete_tags(Resources=[stale["ImageId"]], Tags=[{"Key": tag_name}])
for tagged in describe_tagged_images(tag_name, tag_value):
name = tagged.get("Name", "")
still_opted_in = any(
name.startswith(f"{prefix}-{image_id}-")
for image_id in opted_in_ids
for prefix in (os.environ["DEV_PREFIX"], os.environ["PROD_PREFIX"])
)
if tagged["ImageId"] not in selected_ids and not still_opted_in:
ec2.delete_tags(Resources=[tagged["ImageId"]], Tags=[{"Key": tag_name}])
return {"Items": selected}
def describe_named_images(pattern):
response = ec2.describe_images(
Owners=["self"],
Filters=[
{"Name": "name", "Values": [pattern]},
{"Name": "state", "Values": ["available"]}
]
)
return response.get("Images", [])
def describe_tagged_images(tag_name, tag_value):
response = ec2.describe_images(
Owners=["self"],
Filters=[
{"Name": f"tag:{tag_name}", "Values": [tag_value]},
{"Name": "state", "Values": ["available"]}
]
)
return response.get("Images", [])
def launch(item, run_id):
subnets = os.environ["SUBNET_IDS"].split(",")
preferred_index = sum(ord(char) for char in item["ami_id"]) % len(subnets)
ordered_subnets = subnets[preferred_index:] + subnets[:preferred_index]
launched_at = datetime.datetime.now(datetime.timezone.utc).isoformat()
last_error = None
# Instance-type availability is AZ-specific; try every scanner subnet before failing this AMI scan.
for subnet_id in ordered_subnets:
try:
response = ec2.run_instances(
ImageId=item["ami_id"],
InstanceType=item["instance_type"],
MinCount=1,
MaxCount=1,
IamInstanceProfile={"Name": os.environ["INSTANCE_PROFILE_NAME"]},
NetworkInterfaces=[{
"AssociatePublicIpAddress": True,
"DeviceIndex": 0,
"Groups": [os.environ["SECURITY_GROUP_ID"]],
"SubnetId": subnet_id
}],
TagSpecifications=[{
"ResourceType": "instance",
"Tags": [
{"Key": "Name", "Value": f"inspector-scan-{item['image_id']}-{item['channel']}"},
{"Key": "InspectorScannerRunId", "Value": run_id},
{"Key": "InspectorScannerAmiId", "Value": item["ami_id"]},
{"Key": "InspectorScannerAmiName", "Value": item["ami_name"][:256]},
{"Key": "InspectorScannerChannel", "Value": item["channel"]},
{"Key": "InspectorScannerImageId", "Value": item["image_id"]},
{"Key": "InspectorScannerSubnetId", "Value": subnet_id},
{"Key": os.environ["AMI_SCAN_TAG_NAME"], "Value": os.environ["AMI_SCAN_TAG_VALUE"]}
]
}]
)
item["instance_id"] = response["Instances"][0]["InstanceId"]
item["subnet_id"] = subnet_id
item["launched_at"] = launched_at
item["scan_attempts"] = 0
return item
except ClientError as exc:
code = exc.response.get("Error", {}).get("Code")
if code not in ("Unsupported", "InsufficientInstanceCapacity", "InsufficientCapacity"):
raise
last_error = exc
item.setdefault("launch_failures", []).append({"subnet_id": subnet_id, "error": str(exc)})
raise last_error
def poll(item):
item["scan_attempts"] = item.get("scan_attempts", 0) + 1
launched_at = datetime.datetime.fromisoformat(item["launched_at"])
coverage = inspector.list_coverage(
filterCriteria={
"resourceId": [{"comparison": "EQUALS", "value": item["instance_id"]}],
"resourceType": [{"comparison": "EQUALS", "value": "AWS_EC2_INSTANCE"}]
}
).get("coveredResources", [])
for resource in coverage:
scanned_at = resource.get("lastScannedAt")
if scanned_at and scanned_at >= launched_at:
item["scan_complete"] = True
item["scan_status"] = resource.get("scanStatus", {})
return item
item["scan_complete"] = False
item["scan_timed_out"] = item["scan_attempts"] >= 12
return item
def export_report(item):
prefix = f"inspector/{item['image_id']}/{item['channel']}/{item['ami_id']}/{datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')}"
item["report_export_attempts"] = item.get("report_export_attempts", 0) + 1
item["report_export_wait"] = False
try:
response = inspector.create_findings_report(
filterCriteria={
"resourceId": [{"comparison": "EQUALS", "value": item["instance_id"]}]
},
reportFormat=os.environ["REPORT_FORMAT"],
s3Destination={
"bucketName": os.environ["REPORT_BUCKET"],
"keyPrefix": prefix,
"kmsKeyArn": os.environ["REPORT_KMS_KEY_ARN"]
}
)
except ClientError as exc:
error = exc.response.get("Error", {})
if error.get("Code") == "ValidationException" and "multiple reports in-progress" in error.get("Message", ""):
item["report_export_wait"] = True
item["report_export_wait_reason"] = error.get("Message")
return item
raise
item["report_id"] = response["reportId"]
item["report_s3_prefix"] = f"s3://{os.environ['REPORT_BUCKET']}/{prefix}"
item["finding_counts"] = finding_counts(item["instance_id"])
return item
def poll_report(item):
item["report_attempts"] = item.get("report_attempts", 0) + 1
response = inspector.get_findings_report_status(reportId=item["report_id"])
item["report_status"] = response.get("status")
item["report_error_code"] = response.get("errorCode")
item["report_error_message"] = response.get("errorMessage")
item["report_complete"] = item["report_status"] == "SUCCEEDED"
item["report_failed"] = item["report_status"] in ("FAILED", "CANCELLED")
item["report_timed_out"] = item["report_attempts"] >= 30
return item
def finding_counts(instance_id):
counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "INFORMATIONAL": 0, "UNTRIAGED": 0}
paginator = inspector.get_paginator("list_findings")
for page in paginator.paginate(filterCriteria={"resourceId": [{"comparison": "EQUALS", "value": instance_id}]}):
for finding in page.get("findings", []):
counts[finding.get("severity", "UNTRIAGED")] = counts.get(finding.get("severity", "UNTRIAGED"), 0) + 1
return counts
def notify(item):
update_ami_metadata(item)
counts = item.get("finding_counts", {})
subject = f"Inspector AMI scan {item['channel']} {item['image_id']}"
body = "\n".join([
f"AMI: {item['ami_name']} ({item['ami_id']})",
f"Instance: {item.get('instance_id', 'n/a')}",
f"Channel: {item['channel']}",
f"Image ID: {item['image_id']}",
f"Scan complete: {item.get('scan_complete', False)}",
f"Timed out: {item.get('scan_timed_out', False)}",
f"Report status: {item.get('report_status', 'not checked')}",
f"Report: {item.get('report_s3_prefix', 'not exported')}",
f"Report error: {item.get('report_error_message', 'n/a')}",
f"Findings: {json.dumps(counts, sort_keys=True)}"
])
sns.publish(TopicArn=os.environ["SNS_TOPIC_ARN"], Subject=subject[:100], Message=body)
return item
def update_ami_metadata(item):
# These AMI tags are operational metadata for humans and later scanner runs, not build or release inputs.
status = "complete" if item.get("scan_complete") else "timeout" if item.get("scan_timed_out") else "failed"
tags = [
{"Key": "inspector_last_scan_at", "Value": datetime.datetime.now(datetime.timezone.utc).isoformat()},
{"Key": "inspector_last_scan_status", "Value": status},
{"Key": "inspector_last_scan_instance_id", "Value": item.get("instance_id", "n/a")}
]
if item.get("report_id"):
tags.append({"Key": "inspector_last_report_id", "Value": item["report_id"]})
ec2.create_tags(Resources=[item["ami_id"]], Tags=tags)
def cleanup(item):
# Cleanup must be idempotent because both Lambda error handling and Step Functions catch paths can reach it.
instance_id = item.get("instance_id")
if instance_id:
ec2.terminate_instances(InstanceIds=[instance_id])
if item.get("error"):
sns.publish(
TopicArn=os.environ["SNS_TOPIC_ARN"],
Subject=f"Inspector AMI scanner failure {item.get('image_id', 'unknown')}"[:100],
Message=f"{item.get('error')}\n\n{item.get('traceback', '')}"
)
return item
ScannerStateMachineRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-states
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: InvokeScannerLambda
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: lambda:InvokeFunction
Resource: !GetAtt ScannerFunction.Arn
ScannerStateMachine:
Type: AWS::StepFunctions::StateMachine
Properties:
StateMachineName: !Sub ${AWS::StackName}
RoleArn: !GetAtt ScannerStateMachineRole.Arn
DefinitionString: !Sub
- |
{
"StartAt": "Discover",
"States": {
"Discover": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": { "Action": "Discover" }
},
"OutputPath": "$.Payload",
"Next": "ScanImages"
},
"ScanImages": {
"Type": "Map",
"ItemsPath": "$.Items",
"MaxConcurrency": 1,
"Iterator": {
"StartAt": "Launch",
"States": {
"Launch": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Launch",
"Item.$": "$",
"RunId.$": "$$.Execution.Name"
}
},
"OutputPath": "$.Payload",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CleanupFailure" }],
"Next": "WaitForInspector"
},
"WaitForInspector": {
"Type": "Wait",
"Seconds": 900,
"Next": "Poll"
},
"Poll": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Poll",
"Item.$": "$"
}
},
"OutputPath": "$.Payload",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CleanupFailure" }],
"Next": "ScanComplete?"
},
"ScanComplete?": {
"Type": "Choice",
"Choices": [
{ "Variable": "$.scan_complete", "BooleanEquals": true, "Next": "ExportReport" },
{ "Variable": "$.scan_timed_out", "BooleanEquals": true, "Next": "NotifyTimeout" }
],
"Default": "WaitForInspector"
},
"ExportReport": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Export",
"Item.$": "$"
}
},
"OutputPath": "$.Payload",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CleanupFailure" }],
"Next": "ExportStarted?"
},
"ExportStarted?": {
"Type": "Choice",
"Choices": [
{ "Variable": "$.report_export_wait", "BooleanEquals": true, "Next": "WaitForReportSlot" }
],
"Default": "WaitForReportExport"
},
"WaitForReportSlot": {
"Type": "Wait",
"Seconds": 60,
"Next": "ExportReport"
},
"WaitForReportExport": {
"Type": "Wait",
"Seconds": 60,
"Next": "PollReport"
},
"PollReport": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "PollReport",
"Item.$": "$"
}
},
"OutputPath": "$.Payload",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CleanupFailure" }],
"Next": "ReportComplete?"
},
"ReportComplete?": {
"Type": "Choice",
"Choices": [
{ "Variable": "$.report_complete", "BooleanEquals": true, "Next": "NotifySuccess" },
{ "Variable": "$.report_failed", "BooleanEquals": true, "Next": "NotifySuccess" },
{ "Variable": "$.report_timed_out", "BooleanEquals": true, "Next": "NotifySuccess" }
],
"Default": "WaitForReportExport"
},
"NotifySuccess": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Notify",
"Item.$": "$"
}
},
"OutputPath": "$.Payload",
"Catch": [{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "CleanupFailure" }],
"Next": "CleanupSuccess"
},
"NotifyTimeout": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Notify",
"Item.$": "$"
}
},
"OutputPath": "$.Payload",
"Next": "CleanupSuccess"
},
"CleanupSuccess": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Cleanup",
"Item.$": "$"
}
},
"OutputPath": "$.Payload",
"End": true
},
"CleanupFailure": {
"Type": "Task",
"Resource": "arn:${AWS::Partition}:states:::lambda:invoke",
"Parameters": {
"FunctionName": "${ScannerFunctionArn}",
"Payload": {
"Action": "Cleanup",
"Item.$": "$"
}
},
"End": true
}
}
},
"End": true
}
}
}
- ScannerFunctionArn: !GetAtt ScannerFunction.Arn
ScannerScheduleRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub ${AWS::StackName}-events
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: events.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: StartScannerStateMachine
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: states:StartExecution
Resource: !Ref ScannerStateMachine
ScannerSchedule:
Type: AWS::Events::Rule
Properties:
Description: Nightly RunsOn AMI Inspector scanner.
ScheduleExpression: !Ref ScannerScheduleExpression
State: ENABLED
Targets:
- Arn: !Ref ScannerStateMachine
Id: RunsOnInspectorAmiScanner
RoleArn: !GetAtt ScannerScheduleRole.Arn
Outputs:
ReportBucketName:
Description: S3 bucket where Inspector findings reports are exported.
Value: !Ref ReportBucket
ScannerTopicArn:
Description: SNS topic that publishes scanner summaries.
Value: !Ref ScannerTopic
ScannerStateMachineArn:
Description: Step Functions state machine that runs AMI scans.
Value: !Ref ScannerStateMachine