Skip to content

Commit eb55bd1

Browse files
committed
fix flake8 config issues
1 parent f7d3dde commit eb55bd1

File tree

2 files changed

+33
-10
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/rules
    • sra_bedrock_check_invocation_log_cloudwatch
    • sra_bedrock_check_invocation_log_s3

2 files changed

+33
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Config rule to check invocation log for Bedrock environemts.
1+
"""Config rule to check invocation log cloudwatch enabled for Bedrock environemts.
22
33
Version: 1.0
44

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""Config rule to check invocation log s3 enabled for Bedrock environemts.
2+
3+
Version: 1.0
4+
5+
Config rule for SRA in the repo, https://github.com/aws-samples/aws-security-reference-architecture-examples
6+
7+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
8+
SPDX-License-Identifier: MIT-0
9+
"""
110
from typing import Any
211
import boto3
312
import json
@@ -20,9 +29,17 @@
2029
config_client = boto3.client('config', region_name=AWS_REGION)
2130
s3_client = boto3.client('s3', region_name=AWS_REGION)
2231

23-
def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]:
24-
"""Evaluates if Bedrock Model Invocation Logging is properly configured for S3"""
25-
32+
33+
def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]: # noqa: CFQ004, CCR001, C901
34+
"""Evaluate if Bedrock Model Invocation Logging is properly configured for S3.
35+
36+
Args:
37+
rule_parameters (dict): Rule parameters from AWS Config.
38+
39+
Returns:
40+
tuple[str, str]: Compliance status and annotation message.
41+
42+
"""
2643
# Parse rule parameters
2744
params = json.loads(json.dumps(rule_parameters)) if rule_parameters else {}
2845
check_retention = params.get('check_retention', 'true').lower() == 'true'
@@ -34,7 +51,7 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]:
3451
try:
3552
response = bedrock_client.get_model_invocation_logging_configuration()
3653
logging_config = response.get('loggingConfig', {})
37-
54+
3855
s3_config = logging_config.get('s3Config', {})
3956
LOGGER.info(f"Bedrock Model Invocation S3 config: {s3_config}")
4057
bucket_name = s3_config.get('bucketName', "")
@@ -81,22 +98,28 @@ def evaluate_compliance(rule_parameters: dict) -> tuple[str, str]:
8198

8299
if issues:
83100
return 'NON_COMPLIANT', f"S3 logging enabled but {', '.join(issues)}"
84-
else:
85-
return 'COMPLIANT', f"S3 logging properly configured for Bedrock Model Invocation Logging. Bucket: {bucket_name}"
101+
return 'COMPLIANT', f"S3 logging properly configured for Bedrock Model Invocation Logging. Bucket: {bucket_name}"
86102

87103
except Exception as e:
88104
LOGGER.error(f"Error evaluating Bedrock Model Invocation Logging configuration: {str(e)}")
89105
return 'INSUFFICIENT_DATA', f"Error evaluating compliance: {str(e)}"
90106

91-
def lambda_handler(event: dict, context: Any) -> None:
107+
108+
def lambda_handler(event: dict, context: Any) -> None: # noqa: U100
109+
"""Lambda handler.
110+
111+
Args:
112+
event (dict): Config event data
113+
context (Any): Lambda event object
114+
"""
92115
LOGGER.info('Evaluating compliance for AWS Config rule')
93116
LOGGER.info(f"Event: {json.dumps(event)}")
94117

95118
invoking_event = json.loads(event['invokingEvent'])
96119
rule_parameters = json.loads(event['ruleParameters']) if 'ruleParameters' in event else {}
97120

98121
compliance_type, annotation = evaluate_compliance(rule_parameters)
99-
122+
100123
evaluation = {
101124
'ComplianceResourceType': 'AWS::::Account',
102125
'ComplianceResourceId': event['accountId'],
@@ -113,4 +136,4 @@ def lambda_handler(event: dict, context: Any) -> None:
113136
ResultToken=event['resultToken']
114137
)
115138

116-
LOGGER.info("Compliance evaluation complete.")
139+
LOGGER.info("Compliance evaluation complete.")

0 commit comments

Comments
 (0)