Skip to content

Commit 3b974b5

Browse files
committed
add delete operations for lambda function and iam execution role state records
1 parent fb14c2d commit 3b974b5

File tree

1 file changed

+20
-3
lines changed
  • aws_sra_examples/solutions/genai/bedrock_org/lambda/src

1 file changed

+20
-3
lines changed

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def load_sra_cloudwatch_dashboard() -> dict:
8282
SRA_ALARM_TOPIC_ARN: str = ""
8383
STATE_TABLE: str = "sra_state" # for saving resource info
8484

85+
LAMBDA_RECORD_ID: str = ""
8586
LAMBDA_START: str = ""
8687
LAMBDA_FINISH: str = ""
8788

@@ -1112,7 +1113,7 @@ def create_event(event, context):
11121113
global DRY_RUN_DATA
11131114
global LIVE_RUN_DATA
11141115
global CFN_RESPONSE_DATA
1115-
1116+
global LAMBDA_RECORD_ID
11161117
global SRA_ALARM_TOPIC_ARN
11171118
DRY_RUN_DATA = {}
11181119
LIVE_RUN_DATA = {}
@@ -1124,6 +1125,15 @@ def create_event(event, context):
11241125
# TODO(liamschn): need to ensure the solution name for the state table record is sra-common-prerequisites (if it is created here), not bedrock
11251126
deploy_state_table()
11261127
LOGGER.info(f"CFN_RESPONSE_DATA POST deploy_state_table: {CFN_RESPONSE_DATA}")
1128+
# add IAM state table record for the lambda execution role
1129+
execution_role_name = os.environ["AWS_LAMBDA_FUNCTION_NAME"]
1130+
execution_role_arn = f"arn:aws:iam::{sts.MANAGEMENT_ACCOUNT}:role/{execution_role_name}"
1131+
LOGGER.info(f"Adding state table record for lambda IAM execution role: {execution_role_arn}")
1132+
add_state_table_record("iam", "implemented", "lambda execution role", "role", execution_role_arn, sts.MANAGEMENT_ACCOUNT, sts.HOME_REGION, execution_role_name)
1133+
# add lambda function state table record
1134+
LOGGER.info(f"Adding state table record for lambda function: {context.invoked_function_arn}")
1135+
LAMBDA_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)
1136+
11271137

11281138
# 1) Stage config rule lambda code (global/home region)
11291139
deploy_stage_config_rule_lambda_code()
@@ -1535,6 +1545,13 @@ def delete_event(event, context):
15351545

15361546
# 5, 6, & 7) Detach IAM policies, delete IAM policy, delete IAM execution role for custom config rule lambda
15371547
delete_custom_config_iam_role(rule_name, acct)
1548+
1549+
execution_role_name = os.environ["AWS_LAMBDA_FUNCTION_NAME"]
1550+
execution_role_arn = f"arn:aws:iam::{sts.MANAGEMENT_ACCOUNT}:role/{execution_role_name}"
1551+
LOGGER.info(f"Removing state table record for lambda IAM execution role: {execution_role_arn}")
1552+
remove_state_table_record(execution_role_arn)
1553+
LOGGER.info(f"Removing state table record for lambda function: {context.invoked_function_arn}")
1554+
remove_state_table_record(context.invoked_function_arn)
15381555

15391556
# TODO(liamschn): Consider the 256 KB limit for any cloudwatch log message
15401557
if DRY_RUN is False:
@@ -1926,6 +1943,7 @@ def lambda_handler(event, context):
19261943
global RESOURCE_TYPE
19271944
global LAMBDA_START
19281945
global LAMBDA_FINISH
1946+
global LAMBDA_RECORD_ID
19291947
LAMBDA_START = dynamodb.get_date_time()
19301948
LOGGER.info(event)
19311949
LOGGER.info({"boto3 version": boto3.__version__})
@@ -1974,13 +1992,12 @@ def lambda_handler(event, context):
19741992
"dry_run_data": DRY_RUN_DATA,
19751993
}
19761994
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)
19781995
lambda_data = {
19791996
"start_time": LAMBDA_START,
19801997
"end_time": LAMBDA_FINISH,
19811998
"lambda_result": "SUCCESS",
19821999
}
1983-
update_state_table_record(record_id, lambda_data)
2000+
update_state_table_record(LAMBDA_RECORD_ID, lambda_data)
19842001
return {
19852002
"statusCode": 200,
19862003
"lambda_start": LAMBDA_START,

0 commit comments

Comments
 (0)