Skip to content

Commit 045358c

Browse files
authored
Merge pull request aws-samples#32 from jplock/jp-security-hub-policy
[fix] additional exception handling
2 parents 85874ae + eade44e commit 045358c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/activation_lambda/index.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,21 @@ def delete(event: dict, context: LambdaContext):
153153
logger.info(f"Disabled {policy_type} policy type")
154154

155155
logger.debug("Disabling organizations root sessions...")
156-
iam.disable_organizations_root_sessions()
156+
try:
157+
iam.disable_organizations_root_sessions()
158+
except iam.exceptions.ServiceAccessNotEnabledException:
159+
pass
160+
except iam.exceptions.OrganizationNotFoundException:
161+
pass
157162
logger.info("Disabled organizations root sessions")
158163

159164
logger.debug("Disabling organizations root credentials management...")
160-
iam.disable_organizations_root_credentials_management()
165+
try:
166+
iam.disable_organizations_root_credentials_management()
167+
except iam.exceptions.ServiceAccessNotEnabledException:
168+
pass
169+
except iam.exceptions.OrganizationNotFoundException:
170+
pass
161171
logger.info("Disabled organizations root credentials management")
162172

163173
for service_principal in SERVICE_PRINCIPALS:
@@ -166,6 +176,8 @@ def delete(event: dict, context: LambdaContext):
166176
organizations.disable_aws_service_access(ServicePrincipal=service_principal)
167177
except organizations.exceptions.ConcurrentModificationException:
168178
time.sleep(0.1)
179+
except organizations.exceptions.AWSOrganizationsNotInUseException:
180+
pass
169181
logger.info(f"Disabled AWS service access for {service_principal}")
170182

171183

0 commit comments

Comments
 (0)