Skip to content

Commit 2d0bae7

Browse files
author
Christian Herrera
committed
Add EMR deletion to script
1 parent 4cdd58a commit 2d0bae7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/main/com/codely/lesson_06__deploy_to_cloud/video_01__infra_aws/scripts/1-setup-resources.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ EMR_STEP_FUNCTIONS_POLICY="EMR_StepFunctions_Policy"
1414
EMR_STEP_FUNCTIONS_ROLE="EMR_StepFunctions_Role"
1515

1616
# Define AWS Account ID
17-
AWS_ACCOUNT_ID="010928190667"
17+
AWS_ACCOUNT_ID="CHANGE_FOR_YOUR_AWS_ACCOUNT_ID"
1818

1919
# Create S3 bucket
2020
echo "Creating S3 bucket: $S3_BUCKET_NAME"

src/main/com/codely/lesson_06__deploy_to_cloud/video_01__infra_aws/scripts/2-cleanup-resources.sh

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@ EMR_STEP_FUNCTIONS_POLICY="EMR_StepFunctions_Policy"
1414
EMR_STEP_FUNCTIONS_ROLE="EMR_StepFunctions_Role"
1515

1616
# Define AWS Account ID
17-
AWS_ACCOUNT_ID="010928190667"
17+
AWS_ACCOUNT_ID="CHANGE_FOR_YOUR_AWS_ACCOUNT_ID"
18+
19+
# Function to terminate active EMR clusters
20+
function terminate_active_emr_clusters() {
21+
echo "Listing active EMR clusters"
22+
local cluster_ids=$(aws emr list-clusters --active --query 'Clusters[*].Id' --output text --profile $AWS_PROFILE)
23+
24+
if [ -n "$cluster_ids" ]; then
25+
echo "Terminating active EMR clusters: $cluster_ids"
26+
aws emr terminate-clusters --cluster-ids $cluster_ids --profile $AWS_PROFILE
27+
else
28+
echo "No active EMR clusters found."
29+
fi
30+
}
1831

1932
# Function to detach and delete IAM policies
2033
function detach_and_delete_policy() {
@@ -28,6 +41,9 @@ function detach_and_delete_policy() {
2841
aws iam delete-policy --policy-arn arn:aws:iam::$AWS_ACCOUNT_ID:policy/$policy_name --profile $AWS_PROFILE
2942
}
3043

44+
# Call the function to terminate active EMR clusters
45+
terminate_active_emr_clusters
46+
3147
# Delete EMR Step Functions State Machine
3248
echo "Deleting Step Functions state machine"
3349
STATE_MACHINE_ARN=$(aws stepfunctions list-state-machines --profile $AWS_PROFILE --query "stateMachines[?name=='EMR_StepFunctions_Machine'].stateMachineArn" --output text)

0 commit comments

Comments
 (0)