in src/libs/deploy_utils/WorkspaceUtils.py [0:0]
def delete_workspace_role_and_bucket(self):
workspace = self.iottwinmaker_client.get_workspace(workspaceId=self.workspace_id)
ws_s3_bucket = workspace['s3Location'].split(":")[-1]
ws_s3_bucket_logs = ws_s3_bucket + "-logs"
ws_role = workspace['role'].split('/')[-1]
account_id = workspace['role'].split(":")[4]
print(f" ensuring all workspace content deleted...")
self.delete_workspace()
print(f" workspace {self.workspace_id} deleted...")
print(f" deleting workspace role and s3 buckets: ({ws_role}, {ws_s3_bucket})")
iam = boto3.resource('iam')
role = iam.Role(ws_role)
for policy in role.attached_policies.all():
policy_account_id = policy.arn.split(":")[4]
if account_id == policy_account_id:
role.detach_policy(PolicyArn=policy.arn)
policy.delete()
print(f" detach+deleting managed policy: {policy.arn}")
else:
role.detach_policy(PolicyArn=policy.arn)
print(f" detach AWS-managed policy: {policy.arn}")
for policy in role.policies.all():
policy.delete()
print(f" delete inline role policy: {policy.name}")
role.delete()
print(f" deleted role: {ws_role}")
s3 = self.session.resource('s3')
try:
# delete from data bucket
bucket = s3.Bucket(ws_s3_bucket)
bucket.object_versions.delete()
bucket.delete()
print(f" bucket emptied + deleted: {ws_s3_bucket}")
except botocore.exceptions.ClientError as e:
if "NoSuchBucket" in str(e):
print(f" bucket not found: {ws_s3_bucket}")
else:
raise e
try:
# delete from logs bucket (might not exist)
bucket = s3.Bucket(ws_s3_bucket_logs)
bucket.object_versions.delete()
bucket.delete()
print(f" bucket emptied + deleted: {ws_s3_bucket_logs}")
except botocore.exceptions.ClientError as e:
if "NoSuchBucket" in str(e):
pass
else:
print(e)
print(f"! Failed to delete logs bucket: {ws_s3_bucket_logs}, please manually delete in console at:\n https://s3.console.aws.amazon.com/s3/bucket/{ws_s3_bucket_logs}-logs/empty?region=us-east-1")