@@ -979,29 +979,30 @@ def deploy_metric_filters_and_alarms(region: str, accounts: list, resource_prope
979979 DRY_RUN_DATA ["KMSAliasCreate" ] = "DRY_RUN: Create SRA alarm KMS key alias"
980980 else :
981981 LOGGER .info (f"Found SRA alarm KMS key: { alarm_key_id } " )
982- # Add KMS resource records to sra state table
983- add_state_table_record (
984- "kms" ,
985- "implemented" ,
986- "alarms sns kms key" ,
987- "key" ,
988- f"arn:aws:kms:{ region } :{ acct } :key/{ alarm_key_id } " ,
989- acct ,
990- region ,
991- alarm_key_id ,
992- alarm_key_id ,
993- )
994- add_state_table_record (
995- "kms" ,
996- "implemented" ,
997- "alarms sns kms alias" ,
998- "alias" ,
999- f"arn:aws:kms:{ region } :{ acct } :alias/{ ALARM_SNS_KEY_ALIAS } " ,
1000- acct ,
1001- region ,
1002- ALARM_SNS_KEY_ALIAS ,
1003- alarm_key_id ,
1004- )
982+ if DRY_RUN is False :
983+ # Add KMS resource records to sra state table
984+ add_state_table_record (
985+ "kms" ,
986+ "implemented" ,
987+ "alarms sns kms key" ,
988+ "key" ,
989+ f"arn:aws:kms:{ region } :{ acct } :key/{ alarm_key_id } " ,
990+ acct ,
991+ region ,
992+ alarm_key_id ,
993+ alarm_key_id ,
994+ )
995+ add_state_table_record (
996+ "kms" ,
997+ "implemented" ,
998+ "alarms sns kms alias" ,
999+ "alias" ,
1000+ f"arn:aws:kms:{ region } :{ acct } :alias/{ ALARM_SNS_KEY_ALIAS } " ,
1001+ acct ,
1002+ region ,
1003+ ALARM_SNS_KEY_ALIAS ,
1004+ alarm_key_id ,
1005+ )
10051006
10061007 # 4b) SNS topics for alarms
10071008 sns .SNS_CLIENT = sts .assume_role (acct , sts .CONFIGURATION_ROLE , "sns" , region )
@@ -1048,9 +1049,10 @@ def deploy_metric_filters_and_alarms(region: str, accounts: list, resource_prope
10481049 LOGGER .info (f"{ SOLUTION_NAME } -alarms SNS topic already exists." )
10491050 alarm_topic_arn = topic_search
10501051 # add SNS state table record
1051- add_state_table_record (
1052- "sns" , "implemented" , "sns topic for alarms" , "topic" , alarm_topic_arn , acct , region , f"{ SOLUTION_NAME } -alarms"
1053- )
1052+ if DRY_RUN is False :
1053+ add_state_table_record (
1054+ "sns" , "implemented" , "sns topic for alarms" , "topic" , alarm_topic_arn , acct , region , f"{ SOLUTION_NAME } -alarms"
1055+ )
10541056
10551057 # 4c) Cloudwatch metric filters and alarms
10561058 if DRY_RUN is False :
@@ -1141,7 +1143,8 @@ def deploy_central_cloudwatch_observability(event: dict) -> None: # noqa: CCR00
11411143 oam_sink_arn = search_oam_sink [1 ]
11421144 LOGGER .info (f"CloudWatch observability access manager sink found: { oam_sink_arn } " )
11431145 # add OAM sink state table record
1144- add_state_table_record ("oam" , "implemented" , "oam sink" , "sink" , oam_sink_arn , ssm_params .SRA_SECURITY_ACCT , sts .HOME_REGION , "oam_sink" )
1146+ if DRY_RUN is False :
1147+ add_state_table_record ("oam" , "implemented" , "oam sink" , "sink" , oam_sink_arn , ssm_params .SRA_SECURITY_ACCT , sts .HOME_REGION , "oam_sink" )
11451148
11461149 # 5b) OAM Sink policy in security account
11471150 cloudwatch .SINK_POLICY = CLOUDWATCH_OAM_SINK_POLICY ["sra-oam-sink-policy" ]
@@ -1225,16 +1228,17 @@ def deploy_central_cloudwatch_observability(event: dict) -> None: # noqa: CCR00
12251228 )
12261229 xacct_role_arn = search_iam_role [1 ]
12271230 # add cross account role state table record
1228- add_state_table_record (
1229- "iam" ,
1230- "implemented" ,
1231- "cross account sharing role" ,
1232- "role" ,
1233- xacct_role_arn ,
1234- bedrock_account ,
1235- iam .get_iam_global_region (),
1236- cloudwatch .CROSS_ACCOUNT_ROLE_NAME ,
1237- )
1231+ if DRY_RUN is False :
1232+ add_state_table_record (
1233+ "iam" ,
1234+ "implemented" ,
1235+ "cross account sharing role" ,
1236+ "role" ,
1237+ xacct_role_arn ,
1238+ bedrock_account ,
1239+ iam .get_iam_global_region (),
1240+ cloudwatch .CROSS_ACCOUNT_ROLE_NAME ,
1241+ )
12381242
12391243 # 5d) Attach managed policies to CloudWatch-CrossAccountSharingRole IAM role
12401244 cross_account_policies = [
@@ -1287,7 +1291,8 @@ def deploy_central_cloudwatch_observability(event: dict) -> None: # noqa: CCR00
12871291 LOGGER .info (f"CloudWatch observability access manager link found in { bedrock_account } in { bedrock_region } " )
12881292 oam_link_arn = search_oam_link [1 ]
12891293 # add OAM link state table record
1290- add_state_table_record ("oam" , "implemented" , "oam link" , "link" , oam_link_arn , bedrock_account , bedrock_region , "oam_link" )
1294+ if DRY_RUN is False :
1295+ add_state_table_record ("oam" , "implemented" , "oam link" , "link" , oam_link_arn , bedrock_account , bedrock_region , "oam_link" )
12911296
12921297
12931298def deploy_cloudwatch_dashboard (event : dict ) -> None :
@@ -1333,16 +1338,17 @@ def deploy_cloudwatch_dashboard(event: dict) -> None:
13331338 DRY_RUN_DATA ["CloudWatchDashboardCreate" ] = "DRY_RUN: Create CloudWatch observability dashboard"
13341339 else :
13351340 LOGGER .info (f"Cloudwatch dashboard already exists: { search_dashboard [1 ]} " )
1336- add_state_table_record (
1337- "cloudwatch" ,
1338- "implemented" ,
1339- "cloudwatch dashboard" ,
1340- "dashboard" ,
1341- search_dashboard [1 ],
1342- ssm_params .SRA_SECURITY_ACCT ,
1343- sts .HOME_REGION ,
1344- SOLUTION_NAME ,
1345- )
1341+ if DRY_RUN is False :
1342+ add_state_table_record (
1343+ "cloudwatch" ,
1344+ "implemented" ,
1345+ "cloudwatch dashboard" ,
1346+ "dashboard" ,
1347+ search_dashboard [1 ],
1348+ ssm_params .SRA_SECURITY_ACCT ,
1349+ sts .HOME_REGION ,
1350+ SOLUTION_NAME ,
1351+ )
13461352
13471353
13481354def remove_cloudwatch_dashboard () -> None :
@@ -1399,21 +1405,24 @@ def create_event(event: dict, context: Any) -> str:
13991405 execution_role_arn = lambdas .get_lambda_execution_role (os .environ ["AWS_LAMBDA_FUNCTION_NAME" ])
14001406 execution_role_name = execution_role_arn .split ("/" )[- 1 ]
14011407 LOGGER .info (f"Adding state table record for lambda IAM execution role: { execution_role_arn } " )
1402- add_state_table_record (
1403- "iam" , "implemented" , "lambda execution role" , "role" , execution_role_arn , sts .MANAGEMENT_ACCOUNT , sts .HOME_REGION , execution_role_name
1404- )
1405- # add lambda function state table record
1406- LOGGER .info (f"Adding state table record for lambda function: { context .invoked_function_arn } " )
1407- LAMBDA_RECORD_ID = add_state_table_record (
1408- "lambda" ,
1409- "implemented" ,
1410- "bedrock solution function" ,
1411- "lambda" ,
1412- context .invoked_function_arn ,
1413- sts .MANAGEMENT_ACCOUNT ,
1414- sts .HOME_REGION ,
1415- context .function_name ,
1416- )
1408+ if DRY_RUN is False :
1409+ # add lambda execution role state table record
1410+ LOGGER .info (f"Adding state table record for lambda execution role: { execution_role_name } " )
1411+ add_state_table_record (
1412+ "iam" , "implemented" , "lambda execution role" , "role" , execution_role_arn , sts .MANAGEMENT_ACCOUNT , sts .HOME_REGION , execution_role_name
1413+ )
1414+ # add lambda function state table record
1415+ LOGGER .info (f"Adding state table record for lambda function: { context .invoked_function_arn } " )
1416+ LAMBDA_RECORD_ID = add_state_table_record (
1417+ "lambda" ,
1418+ "implemented" ,
1419+ "bedrock solution function" ,
1420+ "lambda" ,
1421+ context .invoked_function_arn ,
1422+ sts .MANAGEMENT_ACCOUNT ,
1423+ sts .HOME_REGION ,
1424+ context .function_name ,
1425+ )
14171426
14181427 # 1) Stage config rule lambda code (global/home region)
14191428 deploy_stage_config_rule_lambda_code ()
@@ -1999,7 +2008,8 @@ def deploy_iam_role(account_id: str, rule_name: str) -> str: # noqa: CFQ001, CC
19992008 if role_arn is None :
20002009 role_arn = ""
20012010 # add IAM role state table record
2002- add_state_table_record ("iam" , "implemented" , "role for config rule" , "role" , role_arn , account_id , "Global" , rule_name )
2011+ if DRY_RUN is False :
2012+ add_state_table_record ("iam" , "implemented" , "role for config rule" , "role" , role_arn , account_id , "Global" , rule_name )
20032013
20042014 iam .SRA_POLICY_DOCUMENTS ["sra-lambda-basic-execution" ]["Statement" ][0 ]["Resource" ] = iam .SRA_POLICY_DOCUMENTS [ # noqa: ECE001
20052015 "sra-lambda-basic-execution"
@@ -2028,9 +2038,10 @@ def deploy_iam_role(account_id: str, rule_name: str) -> str: # noqa: CFQ001, CC
20282038 else :
20292039 LOGGER .info (f"{ rule_name } -lamdba-basic-execution IAM policy already exists" )
20302040 # add IAM policy state table record
2031- add_state_table_record (
2032- "iam" , "implemented" , "policy for config rule role" , "policy" , policy_arn , account_id , "Global" , f"{ rule_name } -lamdba-basic-execution"
2033- )
2041+ if DRY_RUN is False :
2042+ add_state_table_record (
2043+ "iam" , "implemented" , "policy for config rule role" , "policy" , policy_arn , account_id , "Global" , f"{ rule_name } -lamdba-basic-execution"
2044+ )
20342045
20352046 policy_arn2 = f"arn:{ sts .PARTITION } :iam::{ account_id } :policy/{ rule_name } "
20362047 iam_policy_search2 = iam .check_iam_policy_exists (policy_arn2 )
@@ -2047,7 +2058,8 @@ def deploy_iam_role(account_id: str, rule_name: str) -> str: # noqa: CFQ001, CC
20472058 else :
20482059 LOGGER .info (f"{ rule_name } IAM policy already exists" )
20492060 # add IAM policy state table record
2050- add_state_table_record ("iam" , "implemented" , "policy for config rule" , "policy" , policy_arn2 , account_id , "Global" , rule_name )
2061+ if DRY_RUN is False :
2062+ add_state_table_record ("iam" , "implemented" , "policy for config rule" , "policy" , policy_arn2 , account_id , "Global" , rule_name )
20512063
20522064 policy_attach_search1 = iam .check_iam_policy_attached (rule_name , policy_arn )
20532065 if policy_attach_search1 is False :
@@ -2128,7 +2140,8 @@ def deploy_lambda_function(account_id: str, rule_name: str, role_arn: str, regio
21282140 LOGGER .info (f"{ rule_name } already exists in { account_id } . Search result: { lambda_function_search } " )
21292141 lambda_arn = lambda_function_search
21302142 # add Lambda state table record
2131- add_state_table_record ("lambda" , "implemented" , "lambda for config rule" , "lambda" , lambda_arn , account_id , region , rule_name )
2143+ if DRY_RUN is False :
2144+ add_state_table_record ("lambda" , "implemented" , "lambda for config rule" , "lambda" , lambda_arn , account_id , region , rule_name )
21322145
21332146 return lambda_arn
21342147
@@ -2179,7 +2192,8 @@ def deploy_config_rule(account_id: str, rule_name: str, lambda_arn: str, region:
21792192 LOGGER .info (f"{ rule_name } config rule already exists." )
21802193 config_rule_arn = config_rule_search [1 ]["ConfigRules" ][0 ]["ConfigRuleArn" ]
21812194 # add Config rule state table record
2182- add_state_table_record ("config" , "implemented" , "config rule" , "rule" , config_rule_arn , account_id , region , rule_name )
2195+ if DRY_RUN is False :
2196+ add_state_table_record ("config" , "implemented" , "config rule" , "rule" , config_rule_arn , account_id , region , rule_name )
21832197
21842198
21852199def deploy_metric_filter (
@@ -2211,7 +2225,8 @@ def deploy_metric_filter(
22112225 else :
22122226 LOGGER .info (f"Metric filter { filter_name } already exists." )
22132227 # add metric filter state table record
2214- add_state_table_record ("cloudwatch" , "implemented" , "log metric filter" , "filter" , metric_filter_arn , acct , region , filter_name )
2228+ if DRY_RUN is False :
2229+ add_state_table_record ("cloudwatch" , "implemented" , "log metric filter" , "filter" , metric_filter_arn , acct , region , filter_name )
22152230
22162231
22172232def deploy_metric_alarm ( # noqa: CFQ002
@@ -2279,7 +2294,8 @@ def deploy_metric_alarm( # noqa: CFQ002
22792294 else :
22802295 LOGGER .info (f"Metric alarm { alarm_name } already exists." )
22812296 # add metric alarm state table record
2282- add_state_table_record ("cloudwatch" , "implemented" , "cloudwatch metric alarm" , "alarm" , alarm_arn , acct , region , alarm_name )
2297+ if DRY_RUN is False :
2298+ add_state_table_record ("cloudwatch" , "implemented" , "cloudwatch metric alarm" , "alarm" , alarm_arn , acct , region , alarm_name )
22832299
22842300
22852301def lambda_handler (event : dict , context : Any ) -> dict : # noqa: CCR001
0 commit comments