-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudformation.yaml
More file actions
1457 lines (1201 loc) · 66.5 KB
/
Copy pathcloudformation.yaml
File metadata and controls
1457 lines (1201 loc) · 66.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
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: 'Blog-Complete EC2 Compliance with 5-Stage Notifications and Enhanced Features'
Parameters:
NotificationEmail:
Type: String
Description: Email address for compliance notifications
LambdaTimeout:
Type: Number
Description: Timeout for SmartCompliance Lambda function in seconds
Default: 300
MinValue: 60
MaxValue: 900
RFCScannerTimeout:
Type: Number
Description: Timeout for RFC Scanner Lambda function in seconds
Default: 900
MinValue: 300
MaxValue: 900
LambdaMemorySize:
Type: Number
Description: Memory size for Lambda functions in MB
Default: 512
AllowedValues: [128, 256, 512, 1024, 2048, 3008]
BedrockModelId:
Type: String
Description: Bedrock model ID for AI processing
Default: anthropic.claude-3-haiku-20240307-v1:0
AllowedValues:
- anthropic.claude-3-haiku-20240307-v1:0
- anthropic.claude-3-5-sonnet-20241022-v2:0
- anthropic.claude-3-opus-20240229-v1:0
BedrockMaxTokens:
Type: Number
Description: Maximum tokens for Bedrock API calls
Default: 1000
MinValue: 100
MaxValue: 4000
CloudWatchAlarmThreshold:
Type: Number
Description: CPU utilization threshold for CloudWatch alarms
Default: 80
MinValue: 50
MaxValue: 95
NotificationDelaySeconds:
Type: Number
Description: Delay between notification stages in seconds
Default: 5
MinValue: 1
MaxValue: 30
DefaultEnvironment:
Type: String
Description: Default environment when detection fails
Default: development
AllowedValues: [development, production, testing, staging]
TagPrefix:
Type: String
Description: Prefix for auto-generated tag values
Default: auto
MinLength: 1
MaxLength: 20
Resources:
RFCBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub 'rfc-docs-${AWS::AccountId}'
NotificationConfiguration:
EventBridgeConfiguration:
EventBridgeEnabled: true
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
SNSTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub '${AWS::StackName}-alerts'
Subscription:
- Protocol: email
Endpoint: !Ref NotificationEmail
LambdaRole:
Type: AWS::IAM::Role
Properties:
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: CompliancePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ec2:DescribeInstances
- ec2:CreateTags
- ec2:DescribeTags
- cloudwatch:PutMetricAlarm
- cloudwatch:DescribeAlarms
- s3:GetObject
- s3:ListBucket
- sns:Publish
- bedrock:InvokeModel
Resource: '*'
SmartComplianceFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-compliance'
Runtime: python3.12
Handler: index.lambda_handler
Role: !GetAtt LambdaRole.Arn
Timeout: !Ref LambdaTimeout
MemorySize: !Ref LambdaMemorySize
Environment:
Variables:
RFC_BUCKET: !Ref RFCBucket
SNS_TOPIC: !Ref SNSTopic
BEDROCK_MODEL_ID: !Ref BedrockModelId
BEDROCK_MAX_TOKENS: !Ref BedrockMaxTokens
CLOUDWATCH_ALARM_THRESHOLD: !Ref CloudWatchAlarmThreshold
NOTIFICATION_DELAY_SECONDS: !Ref NotificationDelaySeconds
DEFAULT_ENVIRONMENT: !Ref DefaultEnvironment
TAG_PREFIX: !Ref TagPrefix
Code:
ZipFile: |
import json
import boto3
import logging
import os
import time
from datetime import datetime
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ec2 = boto3.client('ec2')
cloudwatch = boto3.client('cloudwatch')
s3 = boto3.client('s3')
sns = boto3.client('sns')
bedrock = boto3.client('bedrock-runtime')
def lambda_handler(event, context):
try:
instance_id = event['detail']['instance-id']
logger.info(f'Processing compliance for {instance_id}')
response = ec2.describe_instances(InstanceIds=[instance_id])
instance = response['Reservations'][0]['Instances'][0]
environment = get_environment_from_instance(instance)
# STAGE 1: Launch Detection
send_stage1_launch_notification(instance_id, environment, instance)
time.sleep(int(os.environ['NOTIFICATION_DELAY_SECONDS']))
# STAGE 2: RFC Requirements Analysis
rules = get_compliance_rules_from_rfc_two_pass(environment)
violations = check_compliance(instance, rules)
send_stage2_requirements_notification(instance_id, environment, rules, violations)
time.sleep(int(os.environ['NOTIFICATION_DELAY_SECONDS']))
if not violations['missing_tags'] and not violations['missing_alarms']:
# STAGE 4: Already Compliant
send_stage4_already_compliant_notification(instance_id, environment)
return {'statusCode': 200, 'body': 'Already compliant'}
# STAGE 3: Auto-Remediation
actions = auto_fix_violations_with_fallback(instance_id, environment, violations)
send_stage3_completion_notification(instance_id, environment, actions)
return {'statusCode': 200, 'body': 'Compliance applied'}
except Exception as e:
logger.error(f'Error: {str(e)}')
send_error_notification(instance_id if 'instance_id' in locals() else 'unknown', str(e))
return {'statusCode': 500, 'body': str(e)}
def get_environment_from_instance(instance):
# Check existing Environment tag first
for tag in instance.get('Tags', []):
if tag['Key'] == 'Environment':
return tag['Value'].lower()
# Analyze Name tag for patterns
name = ''
for tag in instance.get('Tags', []):
if tag['Key'] == 'Name':
name = tag['Value'].lower()
break
if any(env in name for env in ['dev', 'development', 'sandbox']):
return 'development'
elif any(env in name for env in ['prod', 'production', 'live']):
return 'production'
elif any(env in name for env in ['test', 'staging', 'qa']):
return 'testing'
else:
return os.environ['DEFAULT_ENVIRONMENT']
def get_compliance_rules_from_rfc_two_pass(environment):
"""Two-pass AI analysis as described in blog"""
try:
rfc_content = get_latest_rfc_content()
if not rfc_content:
return {'tags': [], 'alarms': []}
# PASS 1: Extract structure
structure_rules = extract_rule_structure(rfc_content, environment)
# PASS 2: Extract exact values
if structure_rules.get('tags'):
tag_values = extract_tag_values(rfc_content, environment, structure_rules['tags'])
structure_rules['tag_values'] = tag_values
return structure_rules
except Exception as e:
logger.error(f'Error in two-pass RFC analysis: {str(e)}')
return {'tags': [], 'alarms': []}
def extract_rule_structure(rfc_content, environment):
"""First pass: Extract rule structure"""
prompt = f"""Extract compliance rules for {environment} environment from this RFC:
{rfc_content}
Return ONLY a JSON object: {{"tags": ["tag1", "tag2"], "alarms": ["CPUUtilization"]}}
Look for required tags and monitoring requirements for {environment} environment."""
try:
response = bedrock.invoke_model(
modelId=os.environ['BEDROCK_MODEL_ID'],
body=json.dumps({
"messages": [{"role": "user", "content": prompt}],
"max_tokens": int(os.environ['BEDROCK_MAX_TOKENS']),
"anthropic_version": "bedrock-2023-05-31"
})
)
ai_response = json.loads(response['body'].read())['content'][0]['text']
return json.loads(ai_response)
except Exception as e:
logger.error(f'Bedrock structure extraction failed: {str(e)}')
return {'tags': [], 'alarms': []}
def extract_tag_values(rfc_content, environment, required_tags):
"""Second pass: Extract exact tag values"""
prompt = f"""Extract exact tag values for {environment} environment from this RFC document:
{rfc_content}
For the {environment} environment section, find the exact values for these tags: {required_tags}
Look for patterns like:
Environment = development
Owner = dev-team@company.com
CostCenter = DEV-2024
Return ONLY a JSON object where the key is the tag name and the value is the tag value:
{{
"Environment": "development",
"Owner": "dev-team@company.com",
"CostCenter": "DEV-2024"
}}
If a tag is not found in the RFC, use "{os.environ['TAG_PREFIX']}-{environment}" as the value."""
try:
response = bedrock.invoke_model(
modelId=os.environ['BEDROCK_MODEL_ID'],
body=json.dumps({
"messages": [{"role": "user", "content": prompt}],
"max_tokens": int(os.environ['BEDROCK_MAX_TOKENS']),
"anthropic_version": "bedrock-2023-05-31"
})
)
ai_response = json.loads(response['body'].read())['content'][0]['text']
extracted_values = json.loads(ai_response)
# Parse the AI response which might have "Key = Value" format in keys
clean_values = {}
for key, value in extracted_values.items():
if ' = ' in key:
# Split "CostCenter = DEV-2024" into "CostCenter" and "DEV-2024"
tag_name, tag_value = key.split(' = ', 1)
clean_values[tag_name.strip()] = tag_value.strip()
else:
# Normal format
clean_values[key] = value
# Ensure we have values for all required tags
final_values = {}
for tag in required_tags:
if tag in clean_values and clean_values[tag] and clean_values[tag] != f'{os.environ["TAG_PREFIX"]}-{environment}':
final_values[tag] = clean_values[tag]
else:
final_values[tag] = f'{os.environ["TAG_PREFIX"]}-{environment}'
return final_values
except Exception as e:
logger.error(f'Bedrock value extraction failed: {str(e)}')
return {tag: f'{os.environ["TAG_PREFIX"]}-{environment}' for tag in required_tags}
def get_latest_rfc_content():
"""Get latest RFC document from S3"""
try:
s3_objects = s3.list_objects_v2(Bucket=os.environ['RFC_BUCKET'])
if 'Contents' not in s3_objects:
return None
md_files = [obj for obj in s3_objects['Contents'] if obj['Key'].endswith('.md')]
if not md_files:
return None
latest_rfc = max(md_files, key=lambda x: x['LastModified'])
response = s3.get_object(Bucket=os.environ['RFC_BUCKET'], Key=latest_rfc['Key'])
return response['Body'].read().decode('utf-8')
except Exception as e:
logger.error(f'Error reading RFC from S3: {str(e)}')
return None
def check_compliance(instance, rules):
current_tags = {tag['Key']: tag['Value'] for tag in instance.get('Tags', [])}
required_tags = rules.get('tags', [])
missing_tags = [tag for tag in required_tags if tag not in current_tags]
# Check for existing alarms
instance_id = instance['InstanceId']
missing_alarms = []
if 'CPUUtilization' in rules.get('alarms', []):
try:
existing = cloudwatch.describe_alarms(AlarmNames=[f'{instance_id}-CPUUtilization'])
if not existing['MetricAlarms']:
missing_alarms.append('CPUUtilization')
except:
missing_alarms.append('CPUUtilization')
return {'missing_tags': missing_tags, 'missing_alarms': missing_alarms}
def auto_fix_violations_with_fallback(instance_id, environment, violations):
"""Auto-fix with graceful fallback if Bedrock unavailable"""
actions = []
if violations['missing_tags']:
try:
# Try to get values from RFC
rfc_content = get_latest_rfc_content()
if rfc_content:
tag_values = extract_tag_values(rfc_content, environment, violations['missing_tags'])
else:
raise Exception("No RFC content available")
except Exception as e:
logger.warning(f'Bedrock unavailable, using fallback values: {str(e)}')
# Graceful fallback
tag_values = {tag: f'{os.environ["TAG_PREFIX"]}-{environment}' for tag in violations['missing_tags']}
tags_to_add = []
for tag in violations['missing_tags']:
value = tag_values.get(tag, f'{os.environ["TAG_PREFIX"]}-{environment}')
tags_to_add.append({'Key': tag, 'Value': value})
actions.append(f'🏷️ Added tag: {tag} = {value}')
if tags_to_add:
ec2.create_tags(Resources=[instance_id], Tags=tags_to_add)
if violations['missing_alarms']:
alarm_name = f'{instance_id}-CPUUtilization'
# Idempotent alarm creation
try:
existing = cloudwatch.describe_alarms(AlarmNames=[alarm_name])
if not existing['MetricAlarms']:
cloudwatch.put_metric_alarm(
AlarmName=alarm_name,
ComparisonOperator='GreaterThanThreshold',
EvaluationPeriods=2,
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Period=300,
Statistic='Average',
Threshold=float(os.environ['CLOUDWATCH_ALARM_THRESHOLD']),
ActionsEnabled=True,
AlarmActions=[os.environ['SNS_TOPIC']],
AlarmDescription='CPU utilization alarm',
Dimensions=[{'Name': 'InstanceId', 'Value': instance_id}]
)
actions.append(f'📊 Created CPU monitoring alarm (>{os.environ["CLOUDWATCH_ALARM_THRESHOLD"]}% threshold)')
else:
actions.append('📊 CPU monitoring alarm already exists')
except Exception as e:
logger.error(f'Error creating alarm: {str(e)}')
actions.append('❌ Failed to create monitoring alarm')
return actions
# 5-STAGE NOTIFICATION SYSTEM
def send_stage1_launch_notification(instance_id, environment, instance):
"""STAGE 1: Launch Detection with Maximum Details"""
subject = f'🚀 STAGE 1: EC2 INSTANCE DETECTED - {environment.upper()} Environment'
# Get comprehensive instance details
details = get_comprehensive_instance_details(instance)
message = f"""🚀 **STAGE 1: NEW EC2 INSTANCE LAUNCHED**
🎯 **INSTANCE IDENTIFICATION:**
• Instance ID: {instance_id} 🆔
• Environment: {environment.upper()} 🏷️
• Launch Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')} ⏰
• Instance Name: {details['name']} 📛
🌍 **AWS ENVIRONMENT:**
• Account ID: {details['account_id']} 🏦
• Region: {details['region']} 🌐
• Availability Zone: {details['availability_zone']} 📍
💻 **COMPUTE SPECIFICATIONS:**
• Instance Type: {details['instance_type']} ⚙️
• Architecture: {details['architecture']} 🏗️
• CPU Cores: {details['cpu_cores']} 🧠
• Memory: {details['memory']} 💾
• Storage: {details['storage']} 💿
• Platform: {details['platform']} 🖥️
🔗 **NETWORK CONFIGURATION:**
• VPC ID: {details['vpc_id']} 🌐
• Subnet ID: {details['subnet_id']} 📡
• Private IP: {details['private_ip']} 🔒
• Public IP: {details['public_ip']} 🌍
• Private DNS: {details['private_dns']} 🔗
• Public DNS: {details['public_dns']} 🌐
🛡️ **SECURITY CONFIGURATION:**
• Security Groups: {details['security_groups']} 🔐
• Key Pair: {details['key_name']} 🔑
• IAM Role: {details['iam_role']} 👤
• Source/Dest Check: {details['source_dest_check']} ✅
📊 **INSTANCE STATE:**
• State: {details['state']} 🟢
• State Reason: {details['state_reason']} 📝
• Monitoring: {details['monitoring']} 📈
• Tenancy: {details['tenancy']} 🏠
• Hypervisor: {details['hypervisor']} ⚡
• Virtualization: {details['virtualization']} 🔄
🏷️ **CURRENT TAGS ({len(details['current_tags'])}):**
{chr(10).join([f' • {k}: {v} 🏷️' for k, v in details['current_tags'].items()])}
💰 **BILLING INFORMATION:**
• Instance Lifecycle: {details['lifecycle']} 💳
• Spot Instance: {details['spot_instance']} 💸
• Usage Operation: {details['usage_operation']} 📊
🔍 **STATUS:** Starting RFC compliance analysis...
📋 **NEXT:** Stage 2 - RFC Requirements Analysis"""
sns.publish(TopicArn=os.environ['SNS_TOPIC'], Subject=subject, Message=message)
logger.info(f'Stage 1: Launch notification sent for {instance_id}')
def get_comprehensive_instance_details(instance):
"""Get maximum EC2 instance details"""
import boto3
# Get current AWS context
sts = boto3.client('sts')
account_info = sts.get_caller_identity()
# Get region from instance placement
region = instance.get('Placement', {}).get('AvailabilityZone', 'unknown')[:-1]
# Extract all instance details
details = {
'account_id': account_info.get('Account', 'unknown'),
'region': region,
'name': next((tag['Value'] for tag in instance.get('Tags', []) if tag['Key'] == 'Name'), 'unnamed'),
'instance_type': instance.get('InstanceType', 'unknown'),
'architecture': instance.get('Architecture', 'unknown'),
'platform': instance.get('Platform', 'Linux/Unix'),
'availability_zone': instance.get('Placement', {}).get('AvailabilityZone', 'unknown'),
'private_ip': instance.get('PrivateIpAddress', 'none'),
'public_ip': instance.get('PublicIpAddress', 'none'),
'private_dns': instance.get('PrivateDnsName', 'none'),
'public_dns': instance.get('PublicDnsName', 'none'),
'vpc_id': instance.get('VpcId', 'none'),
'subnet_id': instance.get('SubnetId', 'none'),
'security_groups': ', '.join([f"{sg['GroupName']} ({sg['GroupId']})" for sg in instance.get('SecurityGroups', [])]),
'key_name': instance.get('KeyName', 'none'),
'iam_role': instance.get('IamInstanceProfile', {}).get('Arn', 'none').split('/')[-1] if instance.get('IamInstanceProfile') else 'none',
'state': instance.get('State', {}).get('Name', 'unknown'),
'state_reason': instance.get('StateReason', {}).get('Message', 'none'),
'monitoring': 'enabled' if instance.get('Monitoring', {}).get('State') == 'enabled' else 'disabled',
'tenancy': instance.get('Placement', {}).get('Tenancy', 'default'),
'hypervisor': instance.get('Hypervisor', 'unknown'),
'virtualization': instance.get('VirtualizationType', 'unknown'),
'lifecycle': instance.get('InstanceLifecycle', 'normal'),
'spot_instance': 'yes' if instance.get('SpotInstanceRequestId') else 'no',
'usage_operation': instance.get('UsageOperation', 'unknown'),
'source_dest_check': 'enabled' if instance.get('SourceDestCheck', True) else 'disabled',
'current_tags': {tag['Key']: tag['Value'] for tag in instance.get('Tags', [])},
}
# Get instance type details
try:
ec2 = boto3.client('ec2')
instance_types = ec2.describe_instance_types(InstanceTypes=[details['instance_type']])
if instance_types['InstanceTypes']:
type_info = instance_types['InstanceTypes'][0]
details['cpu_cores'] = f"{type_info.get('VCpuInfo', {}).get('DefaultVCpus', 'unknown')} vCPUs"
details['memory'] = f"{type_info.get('MemoryInfo', {}).get('SizeInMiB', 'unknown')} MiB"
# Storage details
storage_info = type_info.get('InstanceStorageInfo', {})
if storage_info.get('TotalSizeInGB'):
details['storage'] = f"{storage_info['TotalSizeInGB']} GB SSD"
else:
details['storage'] = 'EBS-only'
else:
details['cpu_cores'] = 'unknown'
details['memory'] = 'unknown'
details['storage'] = 'unknown'
except:
details['cpu_cores'] = 'unknown'
details['memory'] = 'unknown'
details['storage'] = 'unknown'
return details
def send_stage2_requirements_notification(instance_id, environment, rules, violations):
"""STAGE 2: RFC Requirements Analysis with Maximum Details"""
subject = f'📋 STAGE 2: RFC REQUIREMENTS ANALYSIS - {environment.upper()} Environment'
# Get comprehensive details
response = ec2.describe_instances(InstanceIds=[instance_id])
instance = response['Reservations'][0]['Instances'][0]
details = get_comprehensive_instance_details(instance)
required_tags = rules.get('tags', [])
required_alarms = rules.get('alarms', [])
missing_tags = violations.get('missing_tags', [])
missing_alarms = violations.get('missing_alarms', [])
message = f"""📋 **STAGE 2: RFC REQUIREMENTS ANALYSIS COMPLETE**
🎯 **INSTANCE IDENTIFICATION:**
• Instance ID: {instance_id} 🆔
• Environment: {environment.upper()} 🏷️
• Instance Name: {details['name']} 📛
• Analysis Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')} ⏰
🌍 **AWS ENVIRONMENT:**
• Account ID: {details['account_id']} 🏦
• Region: {details['region']} 🌐
• Availability Zone: {details['availability_zone']} 📍
💻 **INSTANCE SPECIFICATIONS:**
• Instance Type: {details['instance_type']} ⚙️
• CPU Cores: {details['cpu_cores']} 🧠
• Memory: {details['memory']} 💾
• Platform: {details['platform']} 🖥️
• Architecture: {details['architecture']} 🏗️
🔗 **NETWORK DETAILS:**
• VPC ID: {details['vpc_id']} 🌐
• Subnet ID: {details['subnet_id']} 📡
• Private IP: {details['private_ip']} 🔒
• Public IP: {details['public_ip']} 🌍
• Security Groups: {details['security_groups']} 🛡️
📊 **RFC REQUIREMENTS FOR {environment.upper()}:**
🏷️ **Required Tags ({len(required_tags)}):**
{chr(10).join([f' • {tag} 🏷️' for tag in required_tags])}
📈 **Required Monitoring ({len(required_alarms)}):**
{chr(10).join([f' • {alarm} 📊' for alarm in required_alarms])}
❌ **COMPLIANCE VIOLATIONS FOUND:**
🏷️ **Missing Tags ({len(missing_tags)}):**
{chr(10).join([f' • {tag} ❌' for tag in missing_tags]) if missing_tags else ' • None - All tags present ✅'}
📈 **Missing Alarms ({len(missing_alarms)}):**
{chr(10).join([f' • {alarm} ❌' for alarm in missing_alarms]) if missing_alarms else ' • None - All alarms present ✅'}
🏷️ **CURRENT TAGS ({len(details['current_tags'])}):**
{chr(10).join([f' • {k}: {v} 🏷️' for k, v in details['current_tags'].items()])}
💰 **BILLING CONTEXT:**
• Instance Lifecycle: {details['lifecycle']} 💳
• Spot Instance: {details['spot_instance']} 💸
• Usage Operation: {details['usage_operation']} 📊
🔧 **NEXT:** {'Stage 3 - Auto-Remediation 🛠️' if (missing_tags or missing_alarms) else 'Stage 4 - Already Compliant ✅'}"""
sns.publish(TopicArn=os.environ['SNS_TOPIC'], Subject=subject, Message=message)
logger.info(f'Stage 2: Requirements notification sent for {instance_id}')
def send_stage3_completion_notification(instance_id, environment, actions):
"""STAGE 3: Completion Summary with Maximum Details"""
subject = f'✅ STAGE 3: RFC COMPLIANCE COMPLETE - {environment.upper()} Environment'
# Get comprehensive details after remediation
response = ec2.describe_instances(InstanceIds=[instance_id])
instance = response['Reservations'][0]['Instances'][0]
details = get_comprehensive_instance_details(instance)
# Get alarm details
alarm_details = get_alarm_details(instance_id)
message = f"""✅ **STAGE 3: RFC COMPLIANCE SUCCESSFULLY APPLIED**
🎯 **INSTANCE IDENTIFICATION:**
• Instance ID: {instance_id} 🆔
• Environment: {environment.upper()} 🏷️
• Instance Name: {details['name']} 📛
• Completion Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')} ⏰
🌍 **AWS ENVIRONMENT:**
• Account ID: {details['account_id']} 🏦
• Region: {details['region']} 🌐
• Availability Zone: {details['availability_zone']} 📍
💻 **INSTANCE SPECIFICATIONS:**
• Instance Type: {details['instance_type']} ⚙️
• CPU Cores: {details['cpu_cores']} 🧠
• Memory: {details['memory']} 💾
• Storage: {details['storage']} 💿
• Platform: {details['platform']} 🖥️
• Architecture: {details['architecture']} 🏗️
🔗 **NETWORK CONFIGURATION:**
• VPC ID: {details['vpc_id']} 🌐
• Subnet ID: {details['subnet_id']} 📡
• Private IP: {details['private_ip']} 🔒
• Public IP: {details['public_ip']} 🌍
• Security Groups: {details['security_groups']} 🛡️
• Key Pair: {details['key_name']} 🔑
🔧 **ACTIONS COMPLETED ({len(actions)}):**
{chr(10).join([f'• {action}' for action in actions])}
🏷️ **FINAL TAGS ({len(details['current_tags'])}):**
{chr(10).join([f' • {k}: {v} ✅' for k, v in details['current_tags'].items()])}
📊 **MONITORING CONFIGURATION:**
{alarm_details}
💰 **BILLING INFORMATION:**
• Instance Lifecycle: {details['lifecycle']} 💳
• Spot Instance: {details['spot_instance']} 💸
• Usage Operation: {details['usage_operation']} 📊
• Tenancy: {details['tenancy']} 🏠
🛡️ **SECURITY STATUS:**
• IAM Role: {details['iam_role']} 👤
• Monitoring: {details['monitoring']} 📈
• Source/Dest Check: {details['source_dest_check']} ✅
🎉 **COMPLIANCE STATUS:** FULLY COMPLIANT ✅
📊 **SUMMARY:**
• Instance is now RFC compliant ✅
• All required tags applied ✅
• Monitoring alarms configured ✅
• Ready for production use ✅
🔔 **MONITORING:** You will receive alerts if compliance drifts
Thank you for using Smart RFC Compliance! 🚀"""
sns.publish(TopicArn=os.environ['SNS_TOPIC'], Subject=subject, Message=message)
logger.info(f'Stage 3: Completion notification sent for {instance_id}')
def get_alarm_details(instance_id):
"""Get CloudWatch alarm details"""
try:
alarm_name = f'{instance_id}-CPUUtilization'
response = cloudwatch.describe_alarms(AlarmNames=[alarm_name])
if response['MetricAlarms']:
alarm = response['MetricAlarms'][0]
return f"""• Alarm Name: {alarm['AlarmName']} 📊
• Metric: {alarm['MetricName']} 📈
• Threshold: {alarm['Threshold']}% 🎯
• Comparison: {alarm['ComparisonOperator']} ⚖️
• Evaluation Periods: {alarm['EvaluationPeriods']} 🔄
• Period: {alarm['Period']} seconds ⏱️
• State: {alarm['StateValue']} 🚦
• Actions Enabled: {'Yes' if alarm['ActionsEnabled'] else 'No'} 🔔"""
else:
return "• No alarms configured ❌"
except Exception as e:
return f"• Alarm status unknown: {str(e)} ❓"
def send_stage4_already_compliant_notification(instance_id, environment):
"""STAGE 4: Already Compliant with Maximum Details"""
subject = f'✅ STAGE 4: ALREADY RFC COMPLIANT - {environment.upper()} Environment'
# Get comprehensive details
response = ec2.describe_instances(InstanceIds=[instance_id])
instance = response['Reservations'][0]['Instances'][0]
details = get_comprehensive_instance_details(instance)
# Get alarm details
alarm_details = get_alarm_details(instance_id)
message = f"""✅ **STAGE 4: INSTANCE ALREADY RFC COMPLIANT**
🎯 **INSTANCE IDENTIFICATION:**
• Instance ID: {instance_id} 🆔
• Environment: {environment.upper()} 🏷️
• Instance Name: {details['name']} 📛
• Check Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')} ⏰
🌍 **AWS ENVIRONMENT:**
• Account ID: {details['account_id']} 🏦
• Region: {details['region']} 🌐
• Availability Zone: {details['availability_zone']} 📍
💻 **INSTANCE SPECIFICATIONS:**
• Instance Type: {details['instance_type']} ⚙️
• CPU Cores: {details['cpu_cores']} 🧠
• Memory: {details['memory']} 💾
• Storage: {details['storage']} 💿
• Platform: {details['platform']} 🖥️
• Architecture: {details['architecture']} 🏗️
🔗 **NETWORK CONFIGURATION:**
• VPC ID: {details['vpc_id']} 🌐
• Subnet ID: {details['subnet_id']} 📡
• Private IP: {details['private_ip']} 🔒
• Public IP: {details['public_ip']} 🌍
• Security Groups: {details['security_groups']} 🛡️
• Key Pair: {details['key_name']} 🔑
🎉 **GREAT NEWS!** This instance already meets all RFC requirements:
🏷️ **COMPLIANT TAGS ({len(details['current_tags'])}):**
{chr(10).join([f' • {k}: {v} ✅' for k, v in details['current_tags'].items()])}
📊 **MONITORING STATUS:**
{alarm_details}
💰 **BILLING INFORMATION:**
• Instance Lifecycle: {details['lifecycle']} 💳
• Spot Instance: {details['spot_instance']} 💸
• Usage Operation: {details['usage_operation']} 📊
• Tenancy: {details['tenancy']} 🏠
🛡️ **SECURITY STATUS:**
• IAM Role: {details['iam_role']} 👤
• Monitoring: {details['monitoring']} 📈
• Source/Dest Check: {details['source_dest_check']} ✅
• Hypervisor: {details['hypervisor']} ⚡
• Virtualization: {details['virtualization']} 🔄
📊 **COMPLIANCE SUMMARY:**
• All required tags present ✅
• Monitoring alarms configured ✅
• No action needed ✅
• RFC compliant ✅
Keep up the excellent work! 🌟"""
sns.publish(TopicArn=os.environ['SNS_TOPIC'], Subject=subject, Message=message)
logger.info(f'Stage 4: Already compliant notification sent for {instance_id}')
def send_error_notification(instance_id, error_message):
"""Error notification with maximum environment details"""
subject = f'❌ RFC COMPLIANCE ERROR - Instance {instance_id}'
# Get AWS environment details
import boto3
sts = boto3.client('sts')
account_info = sts.get_caller_identity()
region = os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
# Try to get instance details if possible
instance_details = "Unable to retrieve instance details due to error ❌"
try:
response = ec2.describe_instances(InstanceIds=[instance_id])
instance = response['Reservations'][0]['Instances'][0]
details = get_comprehensive_instance_details(instance)
instance_details = f"""🎯 **INSTANCE IDENTIFICATION:**
• Instance ID: {instance_id} 🆔
• Instance Name: {details['name']} 📛
• Instance Type: {details['instance_type']} ⚙️
• Environment: {get_environment_from_instance(instance).upper()} 🏷️
• Availability Zone: {details['availability_zone']} 📍
• VPC ID: {details['vpc_id']} 🌐
• Private IP: {details['private_ip']} 🔒
• State: {details['state']} 🚦"""
except:
pass
message = f"""❌ **RFC COMPLIANCE ERROR**
🌍 **AWS ENVIRONMENT:**
• Account ID: {account_info.get('Account', 'unknown')} 🏦
• Region: {region} 🌐
• Error Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')} ⏰
• User ARN: {account_info.get('Arn', 'unknown')} 👤
{instance_details}
🚨 **ERROR DETAILS:**
• Error Message: {error_message} ❌
• Error Type: Compliance Processing Failure 🔧
• Impact: Partial or no compliance applied ⚠️
🔧 **FALLBACK ACTIONS ATTEMPTED:**
• Basic tags may have been applied with default values 🏷️
• Manual review recommended 👀
• Check CloudWatch logs for detailed error trace 📋
• Bedrock fallback values used if AI unavailable 🤖
📊 **TROUBLESHOOTING STEPS:**
1. Check Bedrock model access permissions 🔐
2. Verify RFC document format in S3 📄
3. Review CloudWatch logs for detailed errors 📋
4. Ensure IAM permissions are sufficient 👤
5. Check network connectivity to AWS services 🌐
📞 **SUPPORT INFORMATION:**
• Contact platform team if this persists 📞
• Include this error notification in support ticket 📧
• Check AWS Service Health Dashboard 🏥
• Review recent AWS account changes 🔄
🔍 **MONITORING:**
• CloudWatch Logs: /aws/lambda/{os.environ.get('AWS_LAMBDA_FUNCTION_NAME', 'unknown')} 📊
• Error will be retried automatically if transient ♻️
• Manual remediation may be required 🛠️"""
sns.publish(TopicArn=os.environ['SNS_TOPIC'], Subject=subject, Message=message)
logger.error(f'Error notification sent for {instance_id}: {error_message}')
RFCUpdateScannerFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-rfc-scanner'
Runtime: python3.12
Handler: index.lambda_handler
Role: !GetAtt LambdaRole.Arn
Timeout: !Ref RFCScannerTimeout
MemorySize: !Ref LambdaMemorySize
Environment:
Variables:
RFC_BUCKET: !Ref RFCBucket
SNS_TOPIC: !Ref SNSTopic
BEDROCK_MODEL_ID: !Ref BedrockModelId
BEDROCK_MAX_TOKENS: !Ref BedrockMaxTokens
CLOUDWATCH_ALARM_THRESHOLD: !Ref CloudWatchAlarmThreshold
NOTIFICATION_DELAY_SECONDS: !Ref NotificationDelaySeconds
DEFAULT_ENVIRONMENT: !Ref DefaultEnvironment
TAG_PREFIX: !Ref TagPrefix
Code:
ZipFile: |
import json
import boto3
import logging
import os
import time
from datetime import datetime
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ec2 = boto3.client('ec2')
cloudwatch = boto3.client('cloudwatch')
s3 = boto3.client('s3')
sns = boto3.client('sns')
bedrock = boto3.client('bedrock-runtime')
def lambda_handler(event, context):
try:
logger.info('RFC document updated - scanning existing instances')
# STAGE 5: RFC Update Processing
send_stage5_rfc_update_notification()
time.sleep(int(os.environ['NOTIFICATION_DELAY_SECONDS']))
# Get all running instances
instances = get_all_running_instances()
logger.info(f'Found {len(instances)} running instances to check')
updated_instances = []
for instance in instances:
instance_id = instance['InstanceId']
environment = get_environment_from_instance(instance)
# Get latest RFC rules with two-pass analysis
rules = get_compliance_rules_from_rfc_two_pass(environment)
violations = check_compliance(instance, rules)
if violations['missing_tags'] or violations['missing_alarms']:
actions = auto_fix_violations_with_fallback(instance_id, environment, violations)
updated_instances.append({
'instance_id': instance_id,
'environment': environment,
'actions': actions
})
time.sleep(2) # Avoid API throttling
# Send summary notification
send_update_summary_notification(updated_instances)
return {'statusCode': 200, 'body': f'Updated {len(updated_instances)} instances'}
except Exception as e:
logger.error(f'Error: {str(e)}')
return {'statusCode': 500, 'body': str(e)}
# Copy all helper functions from SmartComplianceFunction
def get_all_running_instances():
response = ec2.describe_instances(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
instances = []
for reservation in response['Reservations']:
instances.extend(reservation['Instances'])
return instances
def get_environment_from_instance(instance):
for tag in instance.get('Tags', []):
if tag['Key'] == 'Environment':
return tag['Value'].lower()
name = ''
for tag in instance.get('Tags', []):
if tag['Key'] == 'Name':
name = tag['Value'].lower()
break
if any(env in name for env in ['dev', 'development', 'sandbox']):
return 'development'
elif any(env in name for env in ['prod', 'production', 'live']):
return 'production'
elif any(env in name for env in ['test', 'staging', 'qa']):
return 'testing'
else:
return os.environ['DEFAULT_ENVIRONMENT']
def get_comprehensive_instance_details(instance):
"""Get maximum EC2 instance details"""
import boto3
# Get current AWS context
sts = boto3.client('sts')
account_info = sts.get_caller_identity()
# Get region from instance placement
region = instance.get('Placement', {}).get('AvailabilityZone', 'unknown')[:-1]
# Extract all instance details
details = {
'account_id': account_info.get('Account', 'unknown'),
'region': region,
'name': next((tag['Value'] for tag in instance.get('Tags', []) if tag['Key'] == 'Name'), 'unnamed'),
'instance_type': instance.get('InstanceType', 'unknown'),
'architecture': instance.get('Architecture', 'unknown'),
'platform': instance.get('Platform', 'Linux/Unix'),
'availability_zone': instance.get('Placement', {}).get('AvailabilityZone', 'unknown'),
'private_ip': instance.get('PrivateIpAddress', 'none'),
'public_ip': instance.get('PublicIpAddress', 'none'),
'private_dns': instance.get('PrivateDnsName', 'none'),
'public_dns': instance.get('PublicDnsName', 'none'),
'vpc_id': instance.get('VpcId', 'none'),
'subnet_id': instance.get('SubnetId', 'none'),
'security_groups': ', '.join([f"{sg['GroupName']} ({sg['GroupId']})" for sg in instance.get('SecurityGroups', [])]),
'key_name': instance.get('KeyName', 'none'),