Skip to content

Commit 382cf16

Browse files
committed
fix NoSuchLifecycleConfiguration issue
1 parent 2e7ff10 commit 382cf16

File tree

1 file changed

+16
-9
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/rules/sra_bedrock_check_invocation_log_s3

1 file changed

+16
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
config_client = boto3.client("config", region_name=AWS_REGION)
3131
s3_client = boto3.client("s3", region_name=AWS_REGION)
3232

33+
# Global variables
34+
BUCKET_NAME = ""
3335

3436
def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]: # noqa: CFQ004, CCR001, C901
3537
"""Evaluate if Bedrock Model Invocation Logging is properly configured for S3.
@@ -41,6 +43,7 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]: # noqa: CFQ0
4143
tuple[str, str]: Compliance status and annotation message.
4244
4345
"""
46+
global BUCKET_NAME
4447
# Parse rule parameters
4548
params = json.loads(json.dumps(rule_parameters)) if rule_parameters else {}
4649
check_retention = params.get("check_retention", "true").lower() == "true"
@@ -57,17 +60,22 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]: # noqa: CFQ0
5760
LOGGER.info(f"Bedrock Model Invocation S3 config: {s3_config}")
5861
bucket_name = s3_config.get("bucketName", "")
5962
LOGGER.info(f"Bedrock Model Invocation S3 bucketName: {bucket_name}")
60-
63+
BUCKET_NAME = bucket_name
6164
if not s3_config or not bucket_name:
6265
return "NON_COMPLIANT", "S3 logging is not enabled for Bedrock Model Invocation Logging"
6366

6467
# Check S3 bucket configurations
6568
issues = []
6669

6770
if check_retention:
68-
lifecycle = s3_client.get_bucket_lifecycle_configuration(Bucket=bucket_name)
69-
if not any(rule.get("Expiration") for rule in lifecycle.get("Rules", [])):
70-
issues.append("retention not set")
71+
try:
72+
lifecycle = s3_client.get_bucket_lifecycle_configuration(Bucket=bucket_name)
73+
if not any(rule.get("Expiration") for rule in lifecycle.get("Rules", [])):
74+
issues.append("retention not set")
75+
except botocore.exceptions.ClientError as client_error:
76+
if client_error.response['Error']['Code'] == 'NoSuchLifecycleConfiguration':
77+
LOGGER.info(f"No lifecycle configuration found for S3 bucket: {bucket_name}")
78+
issues.append("lifecycle not set")
7179

7280
if check_encryption:
7381
encryption = s3_client.get_bucket_encryption(Bucket=bucket_name)
@@ -98,12 +106,11 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]: # noqa: CFQ0
98106
return "INSUFFICIENT_DATA", f"Error evaluating Object Lock configuration: {str(error)}"
99107

100108
if issues:
101-
return "NON_COMPLIANT", f"S3 logging enabled but {', '.join(issues)}"
109+
return "NON_COMPLIANT", f"S3 logging to {BUCKET_NAME} enabled but {', '.join(issues)}"
102110
return "COMPLIANT", f"S3 logging properly configured for Bedrock Model Invocation Logging. Bucket: {bucket_name}"
103-
104-
except Exception as e:
105-
LOGGER.error(f"Error evaluating Bedrock Model Invocation Logging configuration: {str(e)}")
106-
return "INSUFFICIENT_DATA", f"Error evaluating compliance: {str(e)}"
111+
except botocore.exceptions.ClientError as client_error:
112+
LOGGER.error(f"Error evaluating Bedrock Model Invocation Logging configuration: {str(client_error)}")
113+
return "INSUFFICIENT_DATA", f"Error evaluating compliance: {str(client_error)}"
107114

108115

109116
def lambda_handler(event: dict, context: Any) -> None: # noqa: U100

0 commit comments

Comments
 (0)