Skip to content

Commit a7a5d73

Browse files
committed
Delete lambda functions and roles
1 parent 10deeb1 commit a7a5d73

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

scripts/list_workspace_resource.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ function _list_lambdas() {
5151
done
5252
}
5353

54+
function _delete_lambdas() {
55+
local workspace=$1
56+
57+
if [ -n "$workspace" ]; then
58+
FUNCTIONS=$(aws lambda list-functions | jq -r --arg SUBSTRING1 "${workspace}_" --arg SUBSTRING2 "${workspace}-" '.Functions[] | select((.FunctionName | contains($SUBSTRING1)) or (.FunctionName | contains($SUBSTRING2))) | .FunctionName')
59+
60+
if [ -z "$FUNCTIONS" ]; then
61+
echo -e "${RED}No Lambda functions found.${NC}"
62+
return 0
63+
fi
64+
65+
for FUNCTION_NAME in $FUNCTIONS; do
66+
echo -e "${GREEN}Deleting Lambda function: ${FUNCTION_NAME} ${NC}"
67+
aws lambda delete-function --function-name $FUNCTION_NAME
68+
done
69+
fi
70+
}
71+
5472
function _list_all_kms() {
5573
local workspace=$1
5674

@@ -300,6 +318,61 @@ function _list_iam() {
300318
fi
301319
}
302320

321+
function _delete_iam() {
322+
local workspace=$1
323+
local roles policies
324+
325+
if [ -n "$workspace" ]; then
326+
roles=$(aws iam list-roles --output json | jq -r --arg SUBSTRING1 "${workspace}_" --arg SUBSTRING2 "${workspace}-" '.Roles[] | select((.RoleName | contains($SUBSTRING1)) or (.RoleName | contains($SUBSTRING2))) | .RoleName')
327+
policies=$(aws iam list-policies --scope Local --output json | jq -r --arg SUBSTRING1 "${workspace}_" --arg SUBSTRING2 "${workspace}-" '.Policies[] | select((.PolicyName | contains($SUBSTRING1)) or (.PolicyName | contains($SUBSTRING2))) | .Arn')
328+
329+
if [ -z "$roles" ]; then
330+
echo -e "${RED}No IAM roles found.${NC}"
331+
return 0
332+
fi
333+
334+
if [ -z "$policies" ]; then
335+
echo -e "${RED}No IAM policies found.${NC}"
336+
else
337+
for policy_arn in $policies; do
338+
echo -e "${GREEN}Detaching and Deleting IAM policy: ${policy_arn} ${NC}"
339+
aws iam detach-role-policy --role-name MyRole --policy-arn $policy_arn
340+
aws iam delete-policy --policy-arn $policy_arn
341+
done
342+
fi
343+
344+
for role in $roles; do
345+
echo -e "${GREEN}Deleting IAM role: ${role} ${NC}"
346+
inline_policies=$(aws iam list-role-policies --role-name "$role" --query "PolicyNames" --output text)
347+
if [[ -n "$inline_policies" ]]; then
348+
for pol in $inline_policies; do
349+
echo -e "${GREEN} - Deleting inline policy: $pol ${NC}"
350+
aws iam delete-role-policy --role-name "$role" --policy-name "$pol"
351+
done
352+
fi
353+
354+
managed_policies=$(aws iam list-attached-role-policies --role-name "$role" --query "AttachedPolicies[].PolicyArn" --output text)
355+
if [[ -n "$managed_policies" ]]; then
356+
for polarn in $managed_policies; do
357+
echo -e "${GREEN} - Detaching managed policy: $polarn ${NC}"
358+
aws iam detach-role-policy --role-name "$role" --policy-arn "$polarn"
359+
done
360+
fi
361+
362+
profiles=$(aws iam list-instance-profiles-for-role --role-name "$role" --query "InstanceProfiles[].InstanceProfileName" --output text)
363+
if [[ -n "$profiles" ]]; then
364+
for profile in $profiles; do
365+
echo -e "${GREEN} - Removing role from instance profile: $profile ${NC}"
366+
aws iam remove-role-from-instance-profile --instance-profile-name "$profile" --role-name "$role"
367+
done
368+
fi
369+
370+
echo -e "${GREEN} - Deleting role: $role ${NC}"
371+
aws iam delete-role --role-name "$role"
372+
done
373+
fi
374+
}
375+
303376
function _list_firehose_delivery_streams() {
304377
local workspace=$1
305378
local streams
@@ -1068,6 +1141,8 @@ function _delete_workspace_resources() {
10681141

10691142
_delete_log_groups "$TERRAFORM_WORKSPACE"
10701143
_delete_lambda_layers "$TERRAFORM_WORKSPACE"
1144+
_delete_lambdas "$TERRAFORM_WORKSPACE"
1145+
_delete_iam "$TERRAFORM_WORKSPACE"
10711146
_delete_cloudwatch_alarms "$TERRAFORM_WORKSPACE"
10721147
_delete_sns_subscriptions "$TERRAFORM_WORKSPACE"
10731148
_delete_cloudwatch_dashboards "$TERRAFORM_WORKSPACE"

0 commit comments

Comments
 (0)