Skip to content

Commit 2a8f21b

Browse files
committed
config state record
1 parent 4a66f58 commit 2a8f21b

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,22 +1703,59 @@ def deploy_config_rule(account_id: str, rule_name: str, lambda_arn: str, region:
17031703
LOGGER.info(f"Creating {rule_name} config rule in {account_id} in {region}...")
17041704
# TODO(liamschn): Determine if we need to add a description for the config rules
17051705
# TODO(liamschn): Determine what we will do for input parameters variable in the config rule create function;need an s3 bucket currently
1706-
config.create_config_rule(
1706+
config_response = config.create_config_rule(
17071707
rule_name,
17081708
lambda_arn,
17091709
"One_Hour",
17101710
"CUSTOM_LAMBDA",
17111711
rule_name,
1712-
# {"BucketName": BEDROCK_MODEL_EVAL_BUCKET},
17131712
input_params,
17141713
"DETECTIVE",
17151714
SOLUTION_NAME,
17161715
)
1716+
config_rule_arn = config_response["ConfigRule"]["ConfigRuleArn"]
17171717
else:
17181718
LOGGER.info(f"DRY_RUN: Creating Config policy permissions for {rule_name} lambda function in {account_id} in {region}...")
17191719
LOGGER.info(f"DRY_RUN: Creating {rule_name} config rule in {account_id} in {region}...")
17201720
else:
17211721
LOGGER.info(f"{rule_name} config rule already exists.")
1722+
config_rule_arn = config_rule_search[1]["ConfigRules"][0]["ConfigRuleArn"]
1723+
1724+
# Config rule state table record
1725+
# TODO(liamschn): move dynamodb resource to the dynamo class object/module
1726+
dynamodb_resource = sts.assume_role_resource(ssm_params.SRA_SECURITY_ACCT, sts.CONFIGURATION_ROLE, "dynamodb", sts.HOME_REGION)
1727+
1728+
item_found, find_result = dynamodb.find_item(
1729+
STATE_TABLE,
1730+
dynamodb_resource,
1731+
SOLUTION_NAME,
1732+
{
1733+
"arn": config_rule_arn,
1734+
},
1735+
)
1736+
if item_found is False:
1737+
config_record_id, config_date_time = dynamodb.insert_item(STATE_TABLE, dynamodb_resource, SOLUTION_NAME)
1738+
else:
1739+
config_record_id = find_result["record_id"]
1740+
1741+
dynamodb.update_item(
1742+
STATE_TABLE,
1743+
dynamodb_resource,
1744+
SOLUTION_NAME,
1745+
config_record_id,
1746+
{
1747+
"aws_service": "config",
1748+
"component_state": "implemented",
1749+
"account": account_id,
1750+
"description": "custom config rule",
1751+
"component_region": region,
1752+
"component_type": "rule",
1753+
"component_name": rule_name,
1754+
"arn": config_rule_arn,
1755+
"date_time": dynamodb.get_date_time(),
1756+
},
1757+
)
1758+
17221759

17231760

17241761
def deploy_metric_filter(log_group_name: str, filter_name: str, filter_pattern: str, metric_name: str, metric_namespace: str, metric_value: str):

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import botocore
1010
from boto3.session import Session
1111
from typing import TYPE_CHECKING
12-
if TYPE_CHECKING:
13-
from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table
14-
from mypy_boto3_dynamodb.client import DynamoDBClient
12+
# if TYPE_CHECKING:
13+
# from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table
14+
# from mypy_boto3_dynamodb.client import DynamoDBClient
1515

1616

1717

@@ -25,8 +25,8 @@ class sra_dynamodb:
2525

2626
try:
2727
MANAGEMENT_ACCOUNT_SESSION: Session = boto3.Session()
28-
DYNAMODB_RESOURCE: DynamoDBServiceResource = MANAGEMENT_ACCOUNT_SESSION.resource("dynamodb")
29-
DYNAMODB_CLIENT: DynamoDBClient = MANAGEMENT_ACCOUNT_SESSION.client("dynamodb")
28+
# DYNAMODB_RESOURCE: DynamoDBServiceResource = MANAGEMENT_ACCOUNT_SESSION.resource("dynamodb")
29+
# DYNAMODB_CLIENT: DynamoDBClient = MANAGEMENT_ACCOUNT_SESSION.client("dynamodb")
3030
except Exception:
3131
LOGGER.exception(UNEXPECTED)
3232
raise ValueError("Unexpected error executing Lambda function. Review CloudWatch logs for details.") from None
@@ -39,7 +39,7 @@ def __init__(self, profile="default") -> None:
3939
else:
4040
self.MANAGEMENT_ACCOUNT_SESSION = boto3.Session()
4141

42-
self.DYNAMODB_RESOURCE = self.MANAGEMENT_ACCOUNT_SESSION.resource("dynamodb")
42+
# self.DYNAMODB_RESOURCE = self.MANAGEMENT_ACCOUNT_SESSION.resource("dynamodb")
4343
except Exception:
4444
self.LOGGER.exception(self.UNEXPECTED)
4545
raise ValueError("Unexpected error!") from None

0 commit comments

Comments
 (0)