Skip to content

10 · Cleanup

Goal: zero billable resources left behind. Order matters: if you delete the cluster while our NodePools still exist, or the role while nodes still use it, you make the teardown harder than it needs to be.

Make sure your exports are still set (echo $CLUSTER_NAME $NODE_ROLE_NAME); if not, re-run the block from Prerequisites.

1. Delete the workloads

kubectl delete namespace demo-nodepools

This removes checkout-api, its PDB, the job, and anything from the labs. It can take a minute.

2. Delete our NodePools, and wait for their nodes to go

kubectl delete nodepool stable-static spot-burst graviton-first x86-spill spot-burst-legacy --ignore-not-found

Deleting a NodePool drains and terminates its nodes. Watch until none are left:

kubectl get nodes -l 'karpenter.sh/nodepool in (stable-static,spot-burst,graviton-first,x86-spill)' -w

When the list is empty (No resources found), press Ctrl+C.

3. Delete the NodeClass

kubectl delete nodeclass demo-default

If it hangs, something still references it: check kubectl get nodeclaims and kubectl get nodepools.

4. Remove the access entry and the IAM role

export NODE_ROLE_ARN=$(aws iam get-role --role-name "$NODE_ROLE_NAME" --query Role.Arn --output text)

aws eks delete-access-entry --cluster-name "$CLUSTER_NAME" --principal-arn "$NODE_ROLE_ARN"

aws iam detach-role-policy --role-name "$NODE_ROLE_NAME" \
  --policy-arn arn:aws:iam::aws:policy/AmazonEKSWorkerNodeMinimalPolicy
aws iam detach-role-policy --role-name "$NODE_ROLE_NAME" \
  --policy-arn arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryPullOnly
aws iam detach-role-policy --role-name "$NODE_ROLE_NAME" \
  --policy-arn arn:aws:iam::aws:policy/AmazonElasticContainerRegistryPublicReadOnly

Then delete the role:

aws iam delete-role --role-name "$NODE_ROLE_NAME"

Auto Mode removes the instance profile it created for the role when the NodeClass is deleted, so this normally just works. If delete-role fails with DeleteConflict, the role is still attached to an instance profile. List it:

aws iam list-instance-profiles-for-role --role-name "$NODE_ROLE_NAME" \
  --query 'InstanceProfiles[].InstanceProfileName' --output text

Detach the role from each profile it prints (paste the name), then run delete-role again:

aws iam remove-role-from-instance-profile --role-name "$NODE_ROLE_NAME" \
  --instance-profile-name <profile-name-from-above>

5. Delete the cluster

First remove the inline tagging policy you added to the cluster role in step 03, part 4. The role belongs to eksctl's CloudFormation stack, and CloudFormation can't delete a role that still carries a policy it doesn't know about:

export CLUSTER_ROLE_NAME=$(aws eks describe-cluster --name "$CLUSTER_NAME" \
  --query cluster.roleArn --output text | cut -d/ -f2-)
aws iam delete-role-policy --role-name "$CLUSTER_ROLE_NAME" --policy-name AutoModeCustomNodeTags

Then delete the cluster:

eksctl delete cluster -f cluster/cluster.yaml --profile "$AWS_PROFILE" --wait

This deletes the control plane, the built-in pools' nodes, the eksctl node role, the NAT gateway and the VPC: everything eksctl created, through its CloudFormation stacks. Allow 5–15 minutes (the test run took 3). It ends with:

[✔]  all cluster resources were deleted

6. Verify nothing is left

The cluster is gone:

aws eks describe-cluster --name "$CLUSTER_NAME" 2>&1 | grep -o ResourceNotFoundException

Expect ResourceNotFoundException.

No instances launched by our NodeClass, or tagged for the cluster, are still running:

aws ec2 describe-instances \
  --filters "Name=tag:Talk,Values=mastering-custom-node-pools" \
            "Name=instance-state-name,Values=pending,running,stopping,stopped" \
  --query 'Reservations[].Instances[].InstanceId' --output text

aws ec2 describe-instances \
  --filters "Name=tag-key,Values=kubernetes.io/cluster/$CLUSTER_NAME" \
            "Name=instance-state-name,Values=pending,running,stopping,stopped" \
  --query 'Reservations[].Instances[].InstanceId' --output text

Both should print nothing.

No eksctl CloudFormation stacks remain:

aws cloudformation list-stacks \
  --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE DELETE_FAILED ROLLBACK_COMPLETE \
  --query "StackSummaries[?starts_with(StackName, 'eksctl-$CLUSTER_NAME')].[StackName,StackStatus]" \
  --output table

Should be empty. A DELETE_FAILED stack means something blocked deletion, usually a leftover network interface or security group; open it in the CloudFormation console, read the failed resource, remove it, and delete the stack again.

No NAT gateway is still billing:

aws ec2 describe-nat-gateways \
  --filter "Name=tag:alpha.eksctl.io/cluster-name,Values=$CLUSTER_NAME" "Name=state,Values=pending,available" \
  --query 'NatGateways[].NatGatewayId' --output text

The IAM role is gone:

aws iam get-role --role-name "$NODE_ROLE_NAME" 2>&1 | tail -1

Expect NoSuchEntity.

All empty? You're done, and nothing is billing. Thanks for building along.