forked from aws-samples/golden-ami-pipeline-with-qualys
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ami_golden_ami_pipeline_qualys-vm-pc.json
3762 lines (3558 loc) · 212 KB
/
ami_golden_ami_pipeline_qualys-vm-pc.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": " This cloudformation template creates resources required to set up a golden ami pipeline.(fdp-1o82smtoh)",
"Parameters": {
"qualysUsername": {
"Type": "String",
"Default": "xxxxxxx",
"Description": "Please enter the qualys Username for the Qualys agent which will be installed on your AMI for assessment. You get to override this later by editing the SSM parameter "
},
"qualysPassword": {
"Type": "String",
"Default": "123",
"NoEcho": "true",
"Description": "Please enter the qualys Password for the Qualys agent which will be installed on your AMI for assessment. You get to override this later by editing the SSM parameter "
},
"qualysEc2Connector": {
"Type": "String",
"Default": "xxxxx",
"Description": "Please enter the Qualys EC2 Connector Name for the AWS account that will be used for the Golden AMI Pipeline VPC "
},
"qualysEc2ConnectorId": {
"Type": "String",
"Default": "11111",
"Description": "Please enter the Qualys EC2 Connector ID for the AWS account that will be used for the Golden AMI Pipeline VPC "
},
"sshKeyName": {
"Type": "String",
"Default": "xxxxxx",
"Description": "Please enter the SSH Key name that will be used for the Golden AMI Pipeline VPC and Qualys authentication record"
},
"qualysScanner": {
"Type": "String",
"Default": "GAP_AWS_Scanner_xxxx",
"Description": "Please enter the qualys Scanner Name for the Qualys agent which will be installed on your AMI for assessment. You get to override this later by editing the SSM parameter "
},
"qualysVmOptionId": {
"Type": "String",
"Default": "xxxxxx",
"Description": "Please enter the VM Option Profile ID used for running the Qualys Vulnerability assessment. You get to override this later by editing the SSM parameter "
},
"qualysRunPcScan": {
"Type": "String",
"Default": "NO",
"Description": "Please specify whether to run Qualys Compliance Scans in your Golden AMI Pipeline, if YES, then you must specify a PC Option ID. You get to override this later by editing the SSM parameter ",
"AllowedValues": [
"YES",
"NO"
]
},
"qualysPCOptionId": {
"Type": "String",
"Default": "xxxxxxx",
"Description": "Please enter the Qualys PC OptionId for the Qualys agent which will be installed on your AMI for assessment. You get to override this later by editing the SSM parameter "
},
"qualysParentTagId": {
"Type": "String",
"Default": "xxxxxxxx",
"Description": "Please enter the Qualys Parent Tag ID for the Golden AMI Pipeline Parent Tag in Qualys Asset View "
},
"qualysApiUrl": {
"Default": "https://qualysapi.qg2.apps.qualys.com",
"Description": "Your Qualys URL for accessing API",
"Type": "String"
},
"productName": {
"Type": "String",
"Default": "ProductName-ProductVersion",
"Description": "ProductName-ProductVersion combination of the product for which you intend to use the pipeline. You get to override this later when you trigger automation workflow. "
},
"productOSAndVersion": {
"Type": "String",
"Default": "OperatingSystemName-OperatingSystemVersion",
"Description": "Operating system name and OS version. You get to override this later when you trigger automation workflow."
},
"buildVersion": {
"Type": "String",
"Default": "1",
"Description": "Build-Version corresponding to your product. Note - This is just a default value, you get to override this later when you trigger automation workflow."
},
"cidrVPC" : {
"Description" : "An available CIDR block for creating a new VPC. The size of the VPC should be big enough to hold instances of all your golden AMIs at a time",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "10.0.0.0/16",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
},
"cidrPrivateSubnet" : {
"Description" : "An available CIDR block for creating a new VPC. The size of the VPC should be big enough to hold instances of all your golden AMIs at a time",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "10.0.1.0/24",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
},
"cidrPublicSubnet" : {
"Description" : "An available CIDR block for creating a new VPC. The size of the VPC should be big enough to hold instances of all your golden AMIs at a time",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "10.0.2.0/24",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
},
"ApproverUserIAMARN": {
"Type": "String",
"Default": "arn:aws:iam::111111111111:role/ReadOnlyIAM",
"Description": "IAM ARN of the Golden AMI approver. The approver must have AmazonSSMAutomationApproverAccess policy associated with it's IAM Profile ."
},
"EmailID": {
"Type": "String",
"Default": "johndoe@example.com",
"AllowedPattern": "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)",
"Description": "Your email ID for receiving Qualys assessment results and golden AMI creation notification."
},
"instanceType":
{
"Type": "String",
"Default": "t2.small",
"Description": "Specify the the InstanceType compatible with all your golden AMIs. This InstanceType will be used for launching continuous vulnerability assessment of golden AMIs."
},
"continuousInspectionFrequency": {
"Type": "String",
"Default": "rate(1 day)",
"Description": "Frequency for setting up continuous inspection of your AMIs. For syntax, check - https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html"
},
"MetadataJSON":{
"Type":"String",
"Default":"{\"Account_ID_1\":\"region_1,region_2\"}",
"Description": "Metadata of accounts and regions for distributing the golden AMI."
},
"roleName":{
"Type":"String",
"Default":"goldenAMICrossAccountRole",
"Description": "Cross account role suffix for managing Golden AMI metadata Parameters in child account(s). This role needs to exist in each account specified in MetadataJSON parameter."
},
"qualysActivationID": {
"Default": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Description": "Your Qualys Cloud Agent Activation ID",
"Type": "String"
},
"qualysCustomerID": {
"Default": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"Description": "Your Qualys Cloud Agent Customer ID",
"Type": "String"
},
"qualysAgentLocationWindows": {
"Default": "",
"Description": "(Optional) Enter FQDN or full path of Windows Qualys Cloud Agent Installer's location",
"Type": "String"
},
"qualysAgentLocationDebian": {
"Default": "",
"Description": "(Optional) Enter FQDN or full path of Debian Qualys Cloud Agent Installer's location",
"Type": "String"
},
"qualysAgentLocationRPM": {
"Default": "",
"Description": "(Optional) Enter FQDN or full path of RPM Qualys Cloud Agent Installer's location",
"Type": "String"
},
"qualysQcaLogLevel": {
"Type": "String",
"Default": "0",
"Description": "(Optional) A higher value corresponds to more verbosity. Default is to report only errors (0) ",
"AllowedValues": [
"0",
"1",
"2",
"3",
"4",
"5"
]
}
},
"Resources": {
"QualysUsername": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysUsername",
"Type": "String",
"Value": {"Ref":"qualysUsername"},
"Description": "SSM Parameter for Qualys Username."
}
},
"QualysPassword": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysPassword",
"Type": "String",
"Value": {"Ref":"qualysPassword"},
"Description": "SSM Parameter for Qualys Password."
}
},
"QualysEc2Connector": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysEc2ConnectorName",
"Type": "String",
"Value": {"Ref":"qualysEc2Connector"},
"Description": "SSM Parameter for Qualys EC2 Connector."
}
},
"QualysEc2ConnectorId": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysEc2ConnectorId",
"Type": "String",
"Value": {"Ref":"qualysEc2ConnectorId"},
"Description": "SSM Parameter for Qualys EC2 Connector ID."
}
},
"SSHKeyName": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/SSHKeyName",
"Type": "String",
"Value": {"Ref":"sshKeyName"},
"Description": "SSM Parameter for Qualys EC2 Connector."
}
},
"QualysScannerName": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysScannerName",
"Type": "String",
"Value": {"Ref":"qualysScanner"},
"Description": "SSM Parameter for Qualys Scanner Name."
}
},
"QualysVmOptionId": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysVmOptionId",
"Type": "String",
"Value": {"Ref":"qualysVmOptionId"},
"Description": "SSM Parameter for Qualys Option ID."
}
},
"QualysRunPcScan": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysRunPcScan",
"Type": "String",
"Value": {"Ref":"qualysRunPcScan"},
"Description": "SSM Parameter for Qualys PC Enabled in GAP."
}
},
"QualysPCOptionId": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysPCOptionId",
"Type": "String",
"Value": {"Ref":"qualysPCOptionId"},
"Description": "SSM Parameter for Qualys PC Option ID."
}
},
"QualysParentTagId": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysParentTagId",
"Type": "String",
"Value": {"Ref":"qualysParentTagId"},
"Description": "SSM Parameter for Qualys Parent Tag ID"
}
},
"QualysQcaActivationID": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysQcaActivationID",
"Type": "String",
"Value": {"Ref":"qualysActivationID"},
"Description": "SSM Parameter for Qualys QCA Activation ID"
}
},
"QualysQcaCustomerID": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysQcaCustomerID",
"Type": "String",
"Value": {"Ref":"qualysCustomerID"},
"Description": "SSM Parameter for Qualys QCA Customer ID"
}
},
"QualysAgentLocationWindows": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysAgentLocationWindows",
"Type": "String",
"Value": {"Ref":"qualysAgentLocationWindows"},
"Description": "(Optional) Enter FQDN or full path of Windows Qualys Cloud Agent Installer's location"
}
},
"QualysAgentLocationDebian": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysAgentLocationDebian",
"Type": "String",
"Value": {"Ref":"qualysAgentLocationDebian"},
"Description": "(Optional) Enter FQDN or full path of Debian Qualys Cloud Agent Installer's location"
}
},
"QualysAgentLocationRPM": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysAgentLocationRPM",
"Type": "String",
"Value": {"Ref":"qualysAgentLocationRPM"},
"Description": "(Optional) Enter FQDN or full path of RPM Qualys Cloud Agent Installer's location"
}
},
"QualysQcaLogLevel": {
"Type": "AWS::SSM::Parameter",
"Properties": {
"Name": "/GoldenAMI/Qualys/QualysQcaLogLevel",
"Type": "String",
"Value": {"Ref":"qualysQcaLogLevel"},
"Description": "(Optional) A higher value corresponds to more verbosity. Default is to report only errors (0) "
}
},
"VPC": {
"Type": "AWS::EC2::VPC",
"Properties": {
"EnableDnsSupport": "true",
"EnableDnsHostnames": "true",
"CidrBlock": {"Ref":"cidrVPC"}
}
},
"subnetPrivate": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"CidrBlock": {"Ref":"cidrPrivateSubnet"},
"VpcId": {
"Ref": "VPC"
}
}
},
"subnetPublic": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"MapPublicIpOnLaunch" : true,
"CidrBlock": {"Ref":"cidrPublicSubnet"},
"VpcId": {
"Ref": "VPC"
}
}
},
"InternetGateway": {
"Type": "AWS::EC2::InternetGateway"
},
"PublicVPCGatewayAttachment": {
"Type": "AWS::EC2::VPCGatewayAttachment",
"Properties": {
"VpcId": {
"Ref": "VPC"
},
"InternetGatewayId": {
"Ref": "InternetGateway"
}
}
},
"PublicRouteTable": {
"Type": "AWS::EC2::RouteTable",
"Properties": {
"VpcId": {
"Ref": "VPC"
}
}
},
"PublicRoute": {
"Type": "AWS::EC2::Route",
"DependsOn": "PublicVPCGatewayAttachment",
"Properties": {
"RouteTableId": {
"Ref": "PublicRouteTable"
},
"DestinationCidrBlock": "0.0.0.0/0",
"GatewayId": {
"Ref": "InternetGateway"
}
}
},
"PublicSubnetRouteTableAssociation": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "subnetPublic"
},
"RouteTableId": {
"Ref": "PublicRouteTable"
}
}
},
"NAT" : {
"DependsOn" : "PublicVPCGatewayAttachment",
"Type" : "AWS::EC2::NatGateway",
"Properties" : {
"AllocationId" : { "Fn::GetAtt" : ["EIP", "AllocationId"]},
"SubnetId" : { "Ref" : "subnetPublic"}
}
},
"PrivateRouteTable": {
"Type": "AWS::EC2::RouteTable",
"Properties": {
"VpcId": {
"Ref": "VPC"
}
}
},
"EIP" : {
"Type" : "AWS::EC2::EIP",
"Properties" : {
"Domain" : "vpc"
}
},
"PrivateRoute": {
"Type": "AWS::EC2::Route",
"DependsOn": "PublicVPCGatewayAttachment",
"Properties" :
{
"RouteTableId" : { "Ref" : "PrivateRouteTable" },
"DestinationCidrBlock" : "0.0.0.0/0",
"NatGatewayId" : { "Ref" : "NAT" }
}
},
"PrivateSubnetRouteTableAssociation": {
"Type": "AWS::EC2::SubnetRouteTableAssociation",
"Properties": {
"SubnetId": {
"Ref": "subnetPrivate"
},
"RouteTableId": {
"Ref": "PrivateRouteTable"
}
}
},
"secGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"VpcId": {
"Ref": "VPC"
},
"GroupDescription": "Allow HTTP traffic",
"SecurityGroupIngress": []
}
},
"GoldenAMIConfigBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"VersioningConfiguration": {
"Status": "Enabled"
},
"BucketEncryption":
{
"ServerSideEncryptionConfiguration" : [{
"ServerSideEncryptionByDefault" : {"SSEAlgorithm":"AES256"}
}]
}
}
},
"GoldenAmiConfigBucketPolicy": {
"Type": "AWS::S3::BucketPolicy",
"DependsOn": "ManagedInstanceRole",
"Properties": {
"Bucket": {
"Ref": "GoldenAMIConfigBucket"
},
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:*"
],
"Effect": "Allow",
"Resource": [
{
"Fn::Join": [
"",
[
"arn:aws:s3:::",
{
"Ref": "GoldenAMIConfigBucket"
},
"/*"
]
]
},
{
"Fn::Join": [
"",
[
"arn:aws:s3:::",
{
"Ref": "GoldenAMIConfigBucket"
}
]
]
}
],
"Principal": {
"AWS": [
{
"Fn::GetAtt": [
"ManagedInstanceRole",
"Arn"
]
},
{
"Fn::GetAtt": [
"AutomationServiceRole",
"Arn"
]
},
{
"Fn::GetAtt": [
"PublishAMILambdaRole",
"Arn"
]
}
]
}
}
]
}
}
},
"SSMConfigPolicy": {
"Type": "AWS::IAM::Policy",
"DependsOn": "ManagedInstanceRole",
"Properties": {
"PolicyName": "SSM-Policy",
"PolicyDocument": {
"Version" : "2012-10-17",
"Statement": [
{ "Effect": "Allow", "Action": "ssm:*", "Resource": "arn:aws:ssm:*:*:parameter/*" }
]
},
"Roles": [ { "Ref": "ManagedInstanceRole" } ]
}
},
"PublishAMILambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Path": "/",
"Policies": [
{
"PolicyName": "PublishAMILambdaPolicy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:GetParameter*"
],
"Resource": { "Fn::Join":
[
"",
[
"arn:aws:ssm:", "*",
":",
{
"Ref":"AWS::AccountId"
},
":parameter/GoldenAMI/*"
]
]}
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject*",
"s3:PutObject*"],
"Resource": [
{ "Fn::Join":["",["arn:aws:s3:::",{"Ref": "GoldenAMIConfigBucket"},"/*"]]
}]
},
{
"Effect": "Allow",
"Action": [
"servicecatalog:SearchProductsAsAdmin",
"servicecatalog:CreateProvisioningArtifact",
"servicecatalog:CreateProduct",
"cloudformation:ValidateTemplate"
],
"Resource": "*"
}
]
}
}
]
}
},
"PublishAMILambda": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Role": {
"Fn::GetAtt": [
"PublishAMILambdaRole",
"Arn"
]
},
"Code": {
"ZipFile": {
"Fn::Join": [
"\n",
[
"import boto3",
"import json",
"from dateutil import parser",
"import dateutil",
"import datetime",
"import collections",
"import os",
"import time",
"def lambda_handler(event, context):",
" sourceRegion = os.environ['AWS_DEFAULT_REGION']",
" s3 = boto3.resource('s3')",
" prodName=event['productNameAndVersion']",
" prodOS=event['productOSAndVersion']",
" bucket =event['bucketName']",
" s3FilePrefix=event['templateFileName']",
" version=event['versionToBeCreated']",
" amiRegionMappingParamName =event['amiRegionMappingParamName']",
" filepath='/tmp/'+s3FilePrefix",
" object=s3.Object(bucket,s3FilePrefix)",
" text=object.get()[\"Body\"].read().decode('utf-8') ",
" amiIDParamPath = '/GoldenAMI/'+prodOS+'/'+prodName+'/'+event['versionToBeCreated']",
" text=text.replace('AMI_ID_TO_REPLACE', amiIDParamPath)",
" with open(filepath, mode='w',encoding='utf-8') as file:",
" file.write(text)",
" s3.meta.client.upload_file(filepath,bucket,s3FilePrefix+'/versions/'+version)",
" ssm = boto3.client('ssm',os.environ['AWS_DEFAULT_REGION'])",
" amiIDRegionMapping = ssm.get_parameter(Name=amiRegionMappingParamName)['Parameter']['Value']",
" mappingJSON = json.loads(amiIDRegionMapping)",
" for region, amiID in mappingJSON.items():",
" sc = boto3.client('servicecatalog',region)",
" scProduct=''",
" products = sc.search_products_as_admin(ProductSource='ACCOUNT')",
" for product in products['ProductViewDetails']:",
" productName = product['ProductViewSummary']['Name']",
" if productName == prodName+'-'+prodOS: ",
" scProduct=product['ProductViewSummary']['ProductId']",
" sc.create_provisioning_artifact(ProductId=scProduct,Parameters={'Name': version,'Description': 'This is version '+version,'Info': {'LoadTemplateFromURL': 'https://s3.amazonaws.com/'+bucket+'/'+s3FilePrefix+'/versions/'+version},'Type': 'CLOUD_FORMATION_TEMPLATE'},IdempotencyToken=str(round(time.time() * 1000)))",
" if scProduct == '':",
" print('SC product not found, creating a product')",
" result = sc.create_product(Name=prodName+'-'+prodOS,Owner='CCOE',ProductType='CLOUD_FORMATION_TEMPLATE', Description='This product can be used to launch '+prodName+' in '+prodOS+' environment.', Tags=[{'Key': 'ProductName','Value': prodName+'-'+prodOS}],ProvisioningArtifactParameters={'Name':version,'Description': 'This is version '+version,'Info': {'LoadTemplateFromURL': 'https://s3.amazonaws.com/'+bucket+'/'+s3FilePrefix+'/versions/'+version},'Type': 'CLOUD_FORMATION_TEMPLATE'},IdempotencyToken=str(round(time.time() * 1000)))",
" return 'Done';"
]
]
}
},
"Runtime": "python3.6",
"Timeout": 300,
"Handler": "index.lambda_handler",
"MemorySize": 512
}
},
"DecommissionAMIVersionLambdaRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
},
"Path": "/",
"Policies": [
{
"PolicyName": "DecommissionAMIVersionLambdaPolicy",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssm:GetParameter*",
"ssm:PutParameter*",
"ssm:delete*"
],
"Resource": { "Fn::Join":
[
"",
[
"arn:aws:ssm:", "*",
":",
{
"Ref":"AWS::AccountId"
},
":parameter/GoldenAMI/*"
]
]}
},
{
"Effect": "Allow",
"Action": ["s3:delete*",
"s3:get*"],
"Resource": [
{
"Fn::Join": [
"",
[
"arn:aws:s3:::",
{
"Ref": "GoldenAMIConfigBucket"
},
"/*"
]
]
},
{
"Fn::Join": [
"",
[
"arn:aws:s3:::",
{
"Ref": "GoldenAMIConfigBucket"
}
]
]
}
]
},
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": {
"Fn::Join": [
"",
[
"arn:aws:iam::*:role/",
{
"Ref":"roleName"
}
]
]
}
},
{
"Effect": "Allow",
"Action": [
"ec2:DeregisterImage",
"ec2:DescribeSnapshots",
"ec2:DeleteSnapshot",
"sc:list*",
"sc:search*",
"servicecatalog:SearchProductsAsAdmin",
"servicecatalog:ListProvisioningArtifacts",
"servicecatalog:DeleteProduct",
"servicecatalog:DeleteProvisioningArtifact" ],
"Resource": "*"
}
]
}
}
]
}
},
"DecommissionAMIVersionLambda": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Role": {
"Fn::GetAtt": [
"DecommissionAMIVersionLambdaRole",
"Arn"
]
},
"Code": {
"ZipFile": {
"Fn::Join": [
"",
[
"\n","import boto3",
"\n","import json",
"\n","from dateutil import parser",
"\n","import dateutil",
"\n","import datetime",
"\n","import collections",
"\n","import os",
"\n","import time",
"\n","import botocore",
"\n","def lambda_handler(event, context):",
"\n"," sourceRegion = os.environ['AWS_DEFAULT_REGION']",
"\n"," s3 = boto3.resource('s3')",
"\n"," prodName=event['productNameAndVersion']",
"\n"," prodOS=event['productOSAndVersion']",
"\n"," bucketName =event['bucketName']",
"\n"," s3FilePrefix=event['templateFileName']",
"\n"," version=event['versionToBeDeleted']",
"\n"," amiRegionMappingParamName =event['amiRegionMappingParamName']",
"\n"," prefix='/GoldenAMI/'+prodOS+'/'+prodName+'/'+version",
"\n"," s3 = boto3.resource('s3')",
"\n"," masterAmis='/GoldenAMI/latest'",
"\n"," bucket = s3.Bucket(bucketName)",
"\n"," bucket.delete_objects(Delete={'Objects': [{'Key': s3FilePrefix+'/versions/'+version } ] })",
"\n"," ssm = boto3.client('ssm',sourceRegion)",
"\n"," amiIDRegionMapping=''",
"\n"," params= [prefix+'/latestInstance',prefix+'/LatestAssessmentRunARN',prefix+'/NumCVEs',prefix+'/assessmentLink',prefix+'/assessmentTemplateARN']",
"\n"," try:",
"\n"," amiIDRegionMapping = ssm.get_parameter(Name=amiRegionMappingParamName)['Parameter']['Value']",
"\n"," except botocore.exceptions.ClientError as e:",
"\n"," if e.response['Error']['Code'] == 'ParameterNotFound':",
"\n"," amiID= ssm.get_parameter(Name=prefix)['Parameter']['Value']",
"\n"," ec2 = boto3.client('ec2')",
"\n"," ec2.deregister_image(ImageId=amiID)",
"\n"," time.sleep(5)",
"\n"," snaps = ec2.describe_snapshots()",
"\n"," for snap in snaps['Snapshots']: ",
"\n"," if amiID in snap['Description']:",
"\n"," ec2.delete_snapshot(SnapshotId=snap['SnapshotId'])",
"\n"," ssm.delete_parameters(Names=params)",
"\n"," try:",
"\n"," temp = ssm.get_parameter(Name=masterAmis)['Parameter']['Value']",
"\n"," temp= temp.replace(amiID+',','').replace(','+amiID,'').replace(amiID,'')",
"\n"," if len(temp) == 0:",
"\n"," ssm.delete_parameters(Names=[masterAmis])",
"\n"," else:",
"\n"," ssm.put_parameter(Name=masterAmis,Type='String', Value=temp,Overwrite=True)",
"\n"," return 'Done'",
"\n"," except botocore.exceptions.ClientError as e:",
"\n"," if e.response['Error']['Code'] == 'ParameterNotFound':",
"\n"," print('This indicates that the active amis are not present')",
"\n"," return 'Done'",
"\n"," mappingJSON = json.loads(amiIDRegionMapping)",
"\n"," for region,amiID in mappingJSON.items():",
"\n"," sc = boto3.client('servicecatalog',region)",
"\n"," products = sc.search_products_as_admin(ProductSource='ACCOUNT')",
"\n"," for product in products['ProductViewDetails']:",
"\n"," productName = product['ProductViewSummary']['Name']",
"\n"," if productName == prodName+'-'+prodOS:",
"\n"," productID=product['ProductViewSummary']['ProductId']",
"\n"," provisioningArtifacts = sc.list_provisioning_artifacts(ProductId=productID)['ProvisioningArtifactDetails']",
"\n"," if len(provisioningArtifacts) == 1:",
"\n"," sc.delete_product(Id=productID)",
"\n"," else:",
"\n"," for artifact in provisioningArtifacts:",
"\n"," if artifact['Name'] == version:",
"\n"," sc.delete_provisioning_artifact(ProductId=productID,ProvisioningArtifactId=artifact['Id'])",
"\n"," ec2 = boto3.client('ec2',region)",
"\n"," ec2.deregister_image(ImageId=amiID)",
"\n"," time.sleep(5)",
"\n"," snaps = ec2.describe_snapshots()",
"\n"," for snap in snaps['Snapshots']:",
"\n"," if amiID in snap['Description']:",
"\n"," ec2.delete_snapshot(SnapshotId=snap['SnapshotId'])",
"\n"," ssm = boto3.client('ssm',region)",
"\n"," ssm.delete_parameters(Names=[prefix])",
"\n"," temp = ssm.get_parameter(Name=masterAmis)['Parameter']['Value']",
"\n"," temp= temp.replace(amiID+',','').replace(','+amiID,'').replace(amiID,'')",
"\n"," if len(temp) == 0:",
"\n"," ssm.delete_parameters(Names=[masterAmis])",
"\n"," else:",
"\n"," ssm.put_parameter(Name=masterAmis,Type='String', Value=temp,Overwrite=True)",
"\n"," if region == sourceRegion:",
"\n"," ssm.delete_parameters(Names=params)",
"\n"," return 'Done';"
]
]
}
},
"Runtime": "python3.6",
"Timeout": 300,
"Handler": "index.lambda_handler",
"MemorySize": 512
}
},
"DecommissionAMIVersionFromAccountsLambda": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Role": {
"Fn::GetAtt": [
"DecommissionAMIVersionLambdaRole",
"Arn"
]
},
"Code": {
"ZipFile": {
"Fn::Join": [
"",
[
"\n","import boto3",
"\n","import json",
"\n","from dateutil import parser",
"\n","import dateutil",
"\n","import datetime",
"\n","import collections",
"\n","import os",
"\n","import time",
"\n","import botocore",
"\n","def lambda_handler(event, context):",
"\n"," sourceRegion = os.environ['AWS_DEFAULT_REGION']",
"\n"," s3 = boto3.resource('s3')",
"\n"," prodName=event['productNameAndVersion']",
"\n"," prodOS=event['productOSAndVersion']",