Skip to content

Commit a3643b3

Browse files
committed
working on delete operation for cw dashboard; untested
1 parent b6f16f9 commit a3643b3

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -598,34 +598,6 @@ def create_event(event, context):
598598
DRY_RUN_DATA[f"{filter}_CloudWatch"] = "DRY_RUN: Filter deploy parameter is 'false'; Skip CloudWatch metric filter deployment"
599599

600600
# 5) Central CloudWatch Observability
601-
# TODO(liamschn): determine if we need the CloudWatch-CrossAccountListAccountsRole (needed for "Enable account selector"?).
602-
# TRUST
603-
# {
604-
# "Version": "2012-10-17",
605-
# "Statement": [
606-
# {
607-
# "Effect": "Allow",
608-
# "Principal": {
609-
# "AWS": "arn:aws:iam::533267199951:root"
610-
# },
611-
# "Action": "sts:AssumeRole"
612-
# }
613-
# ]
614-
# }
615-
# PERMISSIONS
616-
# {
617-
# "Version": "2012-10-17",
618-
# "Statement": [
619-
# {
620-
# "Action": [
621-
# "organizations:ListAccounts",
622-
# "organizations:ListAccountsForParent"
623-
# ],
624-
# "Resource": "*",
625-
# "Effect": "Allow"
626-
# }
627-
# ]
628-
# }
629601
central_observability_params = json.loads(event["ResourceProperties"]["SRA-BEDROCK-CENTRAL-OBSERVABILITY"])
630602
# TODO(liamschn): create a parameter to choose to deploy central observability or not: deploy_central_observability = true/false
631603
# 5a) OAM Sink in security account
@@ -768,6 +740,7 @@ def create_event(event, context):
768740
DRY_RUN_DATA["CloudWatchDashboardCreate"] = "DRY_RUN: Create CloudWatch observability dashboard"
769741
else:
770742
LOGGER.info(f"Cloudwatch dashboard already exists: {search_dashboard[1]}")
743+
# TODO(liamschn): check content of dashboard to ensure it is the latest content and update as needed
771744
# check_dashboard = cloudwatch.compare_dashboard(search_dashboard[1], cloudwatch_dashboard)
772745
# if check_dashboard is False:
773746
# if DRY_RUN is False:
@@ -841,6 +814,23 @@ def delete_event(event, context):
841814
LOGGER.info(f"{SOLUTION_NAME}-configuration SNS topic does not exist.")
842815

843816
# 2) Delete Central CloudWatch Observability
817+
# 2a) Delete cloudwatch dashboard
818+
cloudwatch.CLOUDWATCH_CLIENT = sts.assume_role(SECURITY_ACCOUNT, sts.CONFIGURATION_ROLE, "cloudwatch", sts.HOME_REGION)
819+
search_dashboard = cloudwatch.find_dashboard(SOLUTION_NAME)
820+
if search_dashboard[0] is False:
821+
LOGGER.info("CloudWatch observability dashboard not found")
822+
else:
823+
if DRY_RUN is False:
824+
LOGGER.info("Deleting CloudWatch observability dashboard")
825+
LIVE_RUN_DATA["CloudWatchDashboardDelete"] = "Deleted CloudWatch observability dashboard"
826+
cloudwatch.delete_dashboard(SOLUTION_NAME)
827+
CFN_RESPONSE_DATA["deployment_info"]["action_count"] += 1
828+
CFN_RESPONSE_DATA["deployment_info"]["resources_deployed"] -= 1
829+
else:
830+
LOGGER.info("DRY_RUN: Deleting CloudWatch observability dashboard")
831+
832+
833+
844834
central_observability_params = json.loads(event["ResourceProperties"]["SRA-BEDROCK-CENTRAL-OBSERVABILITY"])
845835

846836
cloudwatch.CWOAM_CLIENT = sts.assume_role(SECURITY_ACCOUNT, sts.CONFIGURATION_ROLE, "oam", sts.HOME_REGION)
@@ -852,8 +842,9 @@ def delete_event(event, context):
852842
oam_sink_arn = "Error:Sink:Arn:Not:Found"
853843

854844
# Add management account to the bedrock accounts list
855-
central_observability_params["bedrock_accounts"].append(sts.MANAGEMENT_ACCOUNT)
856-
for bedrock_account in central_observability_params["bedrock_accounts"]:
845+
bedrock_and_mgmt_accounts = copy.deepcopy(central_observability_params["bedrock_accounts"])
846+
bedrock_and_mgmt_accounts.append(sts.MANAGEMENT_ACCOUNT)
847+
for bedrock_account in bedrock_and_mgmt_accounts:
857848
for bedrock_region in central_observability_params["regions"]:
858849
# 2a) OAM link in bedrock account
859850
cloudwatch.CWOAM_CLIENT = sts.assume_role(bedrock_account, sts.CONFIGURATION_ROLE, "oam", bedrock_region)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,18 +460,18 @@ def create_dashboard(self, dashboard_name: str, dashboard_body: dict) -> str:
460460
self.LOGGER.info(self.UNEXPECTED)
461461
raise ValueError(f"Unexpected error executing Lambda function. {error}") from None
462462

463-
def delete_dashboard(self, dashboard_arn: str) -> None:
463+
def delete_dashboard(self, dashboard_name: str) -> None:
464464
"""Delete the CloudWatch dashboard for SRA in the organization.
465465
466466
Args:
467-
dashboard_arn (str): ARN of the dashboard
467+
dashboard_name (str): Name of the dashboard
468468
469469
Returns:
470470
None
471471
"""
472472
try:
473-
self.CLOUDWATCH_CLIENT.delete_dashboards(DashboardNames=[dashboard_arn])
474-
self.LOGGER.info(f"CloudWatch dashboard {dashboard_arn} deleted")
473+
self.CLOUDWATCH_CLIENT.delete_dashboards(DashboardNames=[dashboard_name])
474+
self.LOGGER.info(f"CloudWatch dashboard {dashboard_name} deleted")
475475
except ClientError as e:
476476
self.LOGGER.info(self.UNEXPECTED)
477477
raise ValueError(f"Unexpected error executing Lambda function. {e}") from None

0 commit comments

Comments
 (0)