Skip to content

Commit 8622368

Browse files
committed
handling access denied encrypted guardrail error
1 parent 86b5324 commit 8622368

File tree

2 files changed

+11
-3
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/rules
    • sra_bedrock_check_guardrail_encryption
    • sra_bedrock_check_guardrails

2 files changed

+11
-3
lines changed

aws_sra_examples/solutions/genai/bedrock_org/lambda/rules/sra_bedrock_check_guardrail_encryption/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]: # noqa: CFQ0
6161

6262
except ClientError as e:
6363
if e.response['Error']['Code'] == 'AccessDeniedException':
64-
return "NON_COMPLIANT", "Access denied to Bedrock guardrails. If encryption is enabled, ensure the IAM role has the necessary permissions to use the KMS key."
64+
LOGGER.info(f"Access denied. If guardrail uses KMS encryption, ensure Lambda's IAM role has permissions to the KMS key.")
65+
return "NON_COMPLIANT", (
66+
"Access denied. If guardrail uses KMS encryption, ensure Lambda's IAM role has permissions to the KMS key."
67+
)
6568
LOGGER.error(f"Error evaluating Bedrock guardrails encryption: {str(e)}")
6669
return "ERROR", f"Error evaluating compliance: {str(e)}"
6770

aws_sra_examples/solutions/genai/bedrock_org/lambda/rules/sra_bedrock_check_guardrails/app.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Any
1616

1717
import boto3
18+
from botocore.exceptions import ClientError
1819

1920
# Setup Default Logger
2021
LOGGER = logging.getLogger(__name__)
@@ -95,6 +96,10 @@ def lambda_handler(event: dict, context: Any) -> dict: # noqa: CCR001, C901, U1
9596

9697
except bedrock.exceptions.ResourceNotFoundException:
9798
LOGGER.warning(f"Guardrail {guardrail_name} (ID: {guardrail_id}) not found")
99+
except ClientError as e:
100+
if e.response['Error']['Code'] == 'AccessDeniedException':
101+
LOGGER.info(f"Access denied to guardrail {guardrail_name} (ID: {guardrail_id}). If guardrail uses KMS encryption, ensure Lambda's IAM role has permissions to the KMS key.")
102+
non_compliant_guardrails[guardrail_name] = ["(access_denied; see log for details)"]
98103
except Exception as e:
99104
LOGGER.error(f"Error checking guardrail {guardrail_name} (ID: {guardrail_id}): {str(e)}")
100105

@@ -108,9 +113,9 @@ def lambda_handler(event: dict, context: Any) -> dict: # noqa: CCR001, C901, U1
108113
LOGGER.info(f"Account is COMPLIANT. {annotation}")
109114
else:
110115
compliance_type = "NON_COMPLIANT"
111-
annotation = "No Bedrock guardrails contain all required features. Missing features per guardrail:\n"
116+
annotation = "No Bedrock guardrails contain all required features. "
112117
for guardrail, missing in non_compliant_guardrails.items(): # type: ignore
113-
annotation += f"- {guardrail}: missing {', '.join(missing)}\n"
118+
annotation += f" [{guardrail} is missing {', '.join(missing)}]"
114119
LOGGER.info(f"Account is NON_COMPLIANT. {annotation}")
115120

116121
evaluation = {

0 commit comments

Comments
 (0)