Skip to content

Commit fb14c2d

Browse files
committed
add lambda function record to state table
1 parent eb7465a commit fb14c2d

File tree

1 file changed

+24
-19
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/src

1 file changed

+24
-19
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,6 @@ def get_rule_params(rule_name, resource_properties):
293293
rule_regions (list): list of regions to deploy the rule to
294294
rule_input_params (dict): dictionary of rule input parameters
295295
"""
296-
# rule_accounts (list): list of accounts to deploy the rule to
297-
# rule_regions (list): list of regions to deploy the rule to
298296

299297
if rule_name.upper() in resource_properties:
300298
LOGGER.info(f"{rule_name} parameter found in event ResourceProperties")
@@ -501,7 +499,6 @@ def add_state_table_record(aws_service: str, component_state: str, description:
501499
"""
502500
LOGGER.info(f"Add a record to the state table for {component_name}")
503501
# TODO(liamschn): check to ensure we got a 200 back from the service API call before inserting the dynamodb records
504-
505502
dynamodb.DYNAMODB_RESOURCE = sts.assume_role_resource(ssm_params.SRA_SECURITY_ACCT, sts.CONFIGURATION_ROLE, "dynamodb", sts.HOME_REGION)
506503

507504
item_found, find_result = dynamodb.find_item(
@@ -533,6 +530,7 @@ def add_state_table_record(aws_service: str, component_state: str, description:
533530
"date_time": dynamodb.get_date_time(),
534531
},
535532
)
533+
return sra_resource_record_id
536534

537535

538536
def remove_state_table_record(resource_arn):
@@ -544,7 +542,6 @@ def remove_state_table_record(resource_arn):
544542
Returns:
545543
response: response from dynamodb delete_item
546544
"""
547-
# TODO(liamschn): move dynamodb resource to the dynamo class object/module
548545
dynamodb.DYNAMODB_RESOURCE = sts.assume_role_resource(ssm_params.SRA_SECURITY_ACCT, sts.CONFIGURATION_ROLE, "dynamodb", sts.HOME_REGION)
549546
LOGGER.info(f"Searching for {resource_arn} in {STATE_TABLE} dynamodb table...")
550547
try:
@@ -568,6 +565,21 @@ def remove_state_table_record(resource_arn):
568565
response = {}
569566
return response
570567

568+
def update_state_table_record(record_id: str, update_data: dict):
569+
dynamodb.DYNAMODB_RESOURCE = sts.assume_role_resource(ssm_params.SRA_SECURITY_ACCT, sts.CONFIGURATION_ROLE, "dynamodb", sts.HOME_REGION)
570+
571+
try:
572+
dynamodb.update_item(
573+
STATE_TABLE,
574+
SOLUTION_NAME,
575+
record_id,
576+
update_data,
577+
)
578+
except Exception as error:
579+
LOGGER.error(f"Error updating {record_id} record in {STATE_TABLE} dynamodb table: {error}")
580+
response = {}
581+
return
582+
571583

572584
def deploy_stage_config_rule_lambda_code():
573585
global DRY_RUN_DATA
@@ -624,21 +636,7 @@ def deploy_sns_configuration_topics(context):
624636
else:
625637
DRY_RUN_DATA["SNSCreate"] = f"DRY_RUN: Created {SOLUTION_NAME}-configuration SNS topic"
626638
DRY_RUN_DATA["SNSPermissions"] = "DRY_RUN: Added lambda sns-invoke permissions for SNS topic"
627-
DRY_RUN_DATA["SNSSubscription"] = f"DRY_RUN: Subscribed {context.invoked_function_arn} lambda to {SOLUTION_NAME}-configuration SNS topic"
628-
629-
# else:
630-
# LOGGER.info(f"DRY_RUN: Creating {SOLUTION_NAME}-configuration SNS topic")
631-
# DRY_RUN_DATA["SNSCreate"] = f"DRY_RUN: Create {SOLUTION_NAME}-configuration SNS topic"
632-
633-
# LOGGER.info(
634-
# f"DRY_RUN: Creating SNS topic policy permissions for {SOLUTION_NAME}-configuration SNS topic on {context.function_name} lambda function"
635-
# )
636-
# DRY_RUN_DATA["SNSPermissions"] = "DRY_RUN: Add lambda sns-invoke permissions for SNS topic"
637-
638-
# LOGGER.info(f"DRY_RUN: Subscribing {context.invoked_function_arn} to {SOLUTION_NAME}-configuration SNS topic")
639-
# DRY_RUN_DATA["SNSSubscription"] = f"DRY_RUN: Subscribe {context.invoked_function_arn} lambda to {SOLUTION_NAME}-configuration SNS topic"
640-
# topic_arn = f"arn:aws:sns:{sts.HOME_REGION}:{ACCOUNT}:{SOLUTION_NAME}-configuration"
641-
639+
DRY_RUN_DATA["SNSSubscription"] = f"DRY_RUN: Subscribed {context.invoked_function_arn} lambda to {SOLUTION_NAME}-configuration SNS topic"
642640
else:
643641
LOGGER.info(f"{SOLUTION_NAME}-configuration SNS topic already exists.")
644642
topic_arn = topic_search
@@ -1976,6 +1974,13 @@ def lambda_handler(event, context):
19761974
"dry_run_data": DRY_RUN_DATA,
19771975
}
19781976
LAMBDA_FINISH = dynamodb.get_date_time()
1977+
record_id = add_state_table_record("lambda", "implemented", "bedrock solution function", "lambda", context.invoked_function_arn, sts.MANAGEMENT_ACCOUNT, sts.HOME_REGION, context.function_name)
1978+
lambda_data = {
1979+
"start_time": LAMBDA_START,
1980+
"end_time": LAMBDA_FINISH,
1981+
"lambda_result": "SUCCESS",
1982+
}
1983+
update_state_table_record(record_id, lambda_data)
19791984
return {
19801985
"statusCode": 200,
19811986
"lambda_start": LAMBDA_START,

0 commit comments

Comments
 (0)