Skip to content

Commit eb7465a

Browse files
committed
updating delete logic; separating delete filter/alarn from kms/sns topic
1 parent c685d28 commit eb7465a

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

aws_sra_examples/solutions/genai/bedrock_org/lambda/src/app.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,19 @@ def deploy_metric_filters_and_alarms(region, accounts, resource_properties):
727727
LOGGER.info(f"{filter_name} parameters: {filter_params}")
728728
if filter_deploy is False:
729729
LOGGER.info(f"{filter_name} filter not requested (deploy set to false). Checking to see if any need to be removed...")
730-
delete_metric_filter_alarm_topic_and_key(filter_name, acct, region, filter_params)
731-
730+
if filter_regions:
731+
LOGGER.info(f"Checking {filter_name} filter in regions: {filter_regions}...")
732+
if region not in filter_regions:
733+
LOGGER.info(f"Check found that {filter_name} filter was not requested for {region}. Skipping region...")
734+
else:
735+
for acct in accounts:
736+
if filter_accounts:
737+
LOGGER.info(f"Checking filter_accounts: {filter_accounts}")
738+
if acct not in filter_accounts:
739+
LOGGER.info(f"Check found that {filter_name} filter not requested for {acct}. Skipping account...")
740+
else:
741+
LOGGER.info(f"Check found that {filter_name} filter was defined for {acct} in {region}; Checking for need to be removed...")
742+
delete_metric_filter_and_alarm(filter_name, acct, region, filter_params)
732743
continue
733744
if filter_regions:
734745
LOGGER.info(f"{filter_name} filter regions: {filter_regions}")
@@ -1289,7 +1300,26 @@ def delete_custom_config_iam_role(rule_name: str, acct: str):
12891300
else:
12901301
LOGGER.info(f"{rule_name} IAM role for account {acct} in {region} does not exist.")
12911302

1292-
def delete_metric_filter_alarm_topic_and_key(filter_name: str, acct: str, region: str, filter_params: str):
1303+
def delete_sns_topic_and_key(acct: str, region: str):
1304+
# Delete the alarm topic
1305+
sns.SNS_CLIENT = sts.assume_role(acct, sts.CONFIGURATION_ROLE, "sns", region)
1306+
# TODO(liamschn): this will be a mypy error - need to have alarm_topic_search (sns.find_sns_topic) return string, not None
1307+
alarm_topic_search = sns.find_sns_topic(f"{SOLUTION_NAME}-alarms", region, acct)
1308+
if alarm_topic_search is not None:
1309+
if DRY_RUN is False:
1310+
LOGGER.info(f"Deleting {SOLUTION_NAME}-alarms SNS topic")
1311+
LIVE_RUN_DATA["SNSDelete"] = f"Deleted {SOLUTION_NAME}-alarms SNS topic"
1312+
sns.delete_sns_topic(alarm_topic_search)
1313+
CFN_RESPONSE_DATA["deployment_info"]["action_count"] += 1
1314+
CFN_RESPONSE_DATA["deployment_info"]["resources_deployed"] -= 1
1315+
LOGGER.info(f"Deleted {SOLUTION_NAME}-alarms SNS topic")
1316+
remove_state_table_record(alarm_topic_search)
1317+
else:
1318+
LOGGER.info(f"DRY_RUN: Delete {SOLUTION_NAME}-alarms SNS topic")
1319+
DRY_RUN_DATA["SNSDelete"] = f"DRY_RUN: Delete {SOLUTION_NAME}-alarms SNS topic"
1320+
else:
1321+
LOGGER.info(f"{SOLUTION_NAME}-alarms SNS topic does not exist.")
1322+
12931323
# Delete KMS key (schedule deletion) and delete kms alias
12941324
kms.KMS_CLIENT = sts.assume_role(acct, sts.CONFIGURATION_ROLE, "kms", region)
12951325
search_alarm_kms_key, alarm_key_alias, alarm_key_id, alarm_key_arn = kms.check_alias_exists(kms.KMS_CLIENT, f"alias/{ALARM_SNS_KEY_ALIAS}")
@@ -1319,6 +1349,8 @@ def delete_metric_filter_alarm_topic_and_key(filter_name: str, acct: str, region
13191349
else:
13201350
LOGGER.info(f"{ALARM_SNS_KEY_ALIAS} KMS key does not exist.")
13211351

1352+
1353+
def delete_metric_filter_and_alarm(filter_name: str, acct: str, region: str, filter_params: dict):
13221354
cloudwatch.CWLOGS_CLIENT = sts.assume_role(acct, sts.CONFIGURATION_ROLE, "logs", region)
13231355
cloudwatch.CLOUDWATCH_CLIENT = sts.assume_role(acct, sts.CONFIGURATION_ROLE, "cloudwatch", region)
13241356
if DRY_RUN is False:
@@ -1357,26 +1389,6 @@ def delete_metric_filter_alarm_topic_and_key(filter_name: str, acct: str, region
13571389
LOGGER.info(f"DRY_RUN: Delete {filter_name} CloudWatch metric filter")
13581390
DRY_RUN_DATA[f"{filter_name}_CloudWatchDelete"] = f"DRY_RUN: Delete {filter_name} CloudWatch metric filter"
13591391

1360-
# Delete the alarm topic
1361-
sns.SNS_CLIENT = sts.assume_role(acct, sts.CONFIGURATION_ROLE, "sns", region)
1362-
# TODO(liamschn): this will be a mypy error - need to have alarm_topic_search (sns.find_sns_topic) return string, not None
1363-
alarm_topic_search = sns.find_sns_topic(f"{SOLUTION_NAME}-alarms", region, acct)
1364-
if alarm_topic_search is not None:
1365-
if DRY_RUN is False:
1366-
LOGGER.info(f"Deleting {SOLUTION_NAME}-alarms SNS topic")
1367-
LIVE_RUN_DATA["SNSDelete"] = f"Deleted {SOLUTION_NAME}-alarms SNS topic"
1368-
sns.delete_sns_topic(alarm_topic_search)
1369-
CFN_RESPONSE_DATA["deployment_info"]["action_count"] += 1
1370-
CFN_RESPONSE_DATA["deployment_info"]["resources_deployed"] -= 1
1371-
LOGGER.info(f"Deleted {SOLUTION_NAME}-alarms SNS topic")
1372-
remove_state_table_record(alarm_topic_search)
1373-
else:
1374-
LOGGER.info(f"DRY_RUN: Delete {SOLUTION_NAME}-alarms SNS topic")
1375-
DRY_RUN_DATA["SNSDelete"] = f"DRY_RUN: Delete {SOLUTION_NAME}-alarms SNS topic"
1376-
else:
1377-
LOGGER.info(f"{SOLUTION_NAME}-alarms SNS topic does not exist.")
1378-
1379-
13801392
def delete_event(event, context):
13811393
# TODO(liamschn): handle delete error if IAM policy is updated out-of-band - botocore.errorfactory.DeleteConflictException: An error occurred (DeleteConflict) when calling the DeletePolicy operation: This policy has more than one version. Before you delete a policy, you must delete the policy's versions. The default version is deleted with the policy.
13821394
# TODO(liamschn): move re-used delete event operation code to separate functions
@@ -1505,7 +1517,8 @@ def delete_event(event, context):
15051517
filter_deploy, filter_accounts, filter_regions, filter_params = get_filter_params(filter_name, event["ResourceProperties"])
15061518
for acct in filter_accounts:
15071519
for region in filter_regions:
1508-
delete_metric_filter_alarm_topic_and_key(filter_name, acct, region, filter_params)
1520+
delete_metric_filter_and_alarm(filter_name, acct, region, filter_params)
1521+
delete_sns_topic_and_key(acct, region)
15091522

15101523
# 4) Delete config rules
15111524
# TODO(liamschn): deal with invalid rule names?

aws_sra_examples/solutions/genai/bedrock_org/templates/sra-bedrock-org-main.yaml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ Parameters:
235235
'log_group_name' (non-empty string) and 'bucket_names' (non-empty array of non-empty strings).
236236
Example: {"deploy": "true", "filter_params": {"log_group_name": "aws-controltower/CloudTrailLogs", "bucket_names": ["test-mod-eval-bucket","test-bedrock-kb-bucket"]}}
237237
238-
pBedrockInvocationLogFilterParams:
239-
# TODO(liamschn): update default value of pBedrockInvocationLogFilterParams prior to production
238+
pBedrockPromptInjectionFilterParams:
239+
# TODO(liamschn): update default value of pBedrockPromptInjectionFilterParams prior to production
240240
Type: String
241241
Default: '{"deploy": "true", "accounts": ["221082195774"], "regions": ["us-west-2"], "filter_params": {"log_group_name": "model-invocation-log-group", "input_path": "input.inputBodyJson.messages[0].content"}}'
242242
Description: Bedrock Prompt Injection and Sensitive Info Filter Parameters
@@ -247,6 +247,19 @@ Parameters:
247247
or for titan: {"deploy": "true", "filter_params": {"log_group_name": "model-invocation-log-group", "input_path": "input.inputBodyJson.inputText"}}
248248
NOTE: input_path is based on the base model used such as clause or titan; check the invocation log InvokeModel messages for details
249249
250+
pBedrockSensitiveInfoFilterParams:
251+
# TODO(liamschn): update default value of pBedrockSensitiveInfoFilterParams prior to production
252+
Type: String
253+
Default: '{"deploy": "true", "accounts": ["221082195774"], "regions": ["us-west-2"], "filter_params": {"log_group_name": "model-invocation-log-group", "input_path": "input.inputBodyJson.messages[0].content"}}'
254+
Description: Bedrock Prompt Injection and Sensitive Info Filter Parameters
255+
AllowedPattern: ^\{"deploy"\s*:\s*"(true|false)",\s*"accounts"\s*:\s*\[((?:"[0-9]+"(?:\s*,\s*)?)*)\],\s*"regions"\s*:\s*\[((?:"[a-z0-9-]+"(?:\s*,\s*)?)*)\],\s*"filter_params"\s*:\s*\{"log_group_name"\s*:\s*"[^"\s]+",\s*"input_path"\s*:\s*"[^"\s]+"\}\}$
256+
ConstraintDescription: >
257+
Must be a valid JSON string containing: 'deploy' (true/false), and 'filter_params' object with
258+
'log_group_name' (non-empty string). Examples - for claude: {"deploy": "true", "filter_params": {"log_group_name": "model-invocation-log-group", "input_path": "input.inputBodyJson.messages[0].content"}}
259+
or for titan: {"deploy": "true", "filter_params": {"log_group_name": "model-invocation-log-group", "input_path": "input.inputBodyJson.inputText"}}
260+
NOTE: input_path is based on the base model used such as clause or titan; check the invocation log InvokeModel messages for details
261+
262+
250263
pBedrockCentralObservabilityParams:
251264
# TODO(liamschn): update default value of pBedrockCentralObservabilityParams prior to production
252265
Type: String
@@ -320,7 +333,8 @@ Metadata:
320333
Parameters:
321334
- pBedrockServiceChangesFilterParams
322335
- pBedrockBucketChangesFilterParams
323-
- pBedrockInvocationLogFilterParams
336+
- pBedrockPromptInjectionFilterParams
337+
- pBedrockSensitiveInfoFilterParams
324338
- Label:
325339
default: Bedrock Central Observability
326340
Parameters:
@@ -371,8 +385,10 @@ Metadata:
371385
default: Bedrock Service Changes Filter Parameters
372386
pBedrockBucketChangesFilterParams:
373387
default: Bedrock S3 Bucket Changes Filter Parameters
374-
pBedrockInvocationLogFilterParams:
375-
default: Bedrock Prompt Injection and Sensitive Info Filter Parameters
388+
pBedrockPromptInjectionFilterParams:
389+
default: Bedrock Prompt Injection Filter Parameters
390+
pBedrockSensitiveInfoFilterParams:
391+
default: Bedrock Sensitive Info Filter Parameters
376392
pBedrockCentralObservabilityParams:
377393
default: Bedrock Central Observability Parameters
378394
pBedrockAccounts:
@@ -444,8 +460,8 @@ Resources:
444460
SRA-BEDROCK-CHECK-GUARDRAIL-ENCRYPTION: !Ref pBedrockGuardrailEncryptionRuleParams
445461
SRA-BEDROCK-FILTER-SERVICE-CHANGES: !Ref pBedrockServiceChangesFilterParams
446462
SRA-BEDROCK-FILTER-BUCKET-CHANGES: !Ref pBedrockBucketChangesFilterParams
447-
SRA-BEDROCK-FILTER-PROMPT-INJECTION: !Ref pBedrockInvocationLogFilterParams
448-
SRA-BEDROCK-FILTER-SENSITIVE-INFO: !Ref pBedrockInvocationLogFilterParams
463+
SRA-BEDROCK-FILTER-PROMPT-INJECTION: !Ref pBedrockPromptInjectionFilterParams
464+
SRA-BEDROCK-FILTER-SENSITIVE-INFO: !Ref pBedrockSensitiveInfoFilterParams
449465
SRA-BEDROCK-CENTRAL-OBSERVABILITY: !Ref pBedrockCentralObservabilityParams
450466

451467
rBedrockOrgLambdaInvokePermission:

0 commit comments

Comments
 (0)