Teardown
Progress checklist
Overview
Section titled “Overview”This page tears down everything created in the EC2 walkthrough, in dependency order: instance → security groups → mount targets → file system → IAM → mount target SG → bucket (optional).
-
Set environment variables. Required
Re-run these in your current shell. Replace values to match what you created:
Terminal window export AWS_REGION=ap-southeast-6export BUCKET=my-s3-files-bucket # ← replace with your real bucket nameexport BUCKET_ARN=arn:aws:s3:::${BUCKET}export ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)export VPC_ID=vpc-0123456789abcdef0 # ← replace with your VPC IDexport FS_ID=$(aws s3files list-file-systems \--region $AWS_REGION \--bucket $BUCKET_ARN \--query 'fileSystems[0].fileSystemId' \--output text)echo "FS_ID=$FS_ID Account: $ACCOUNT_ID" -
Unmount and clean
/etc/fstabon the instance.Connect via Session Manager, then inside the session:
Terminal window sudo umount /mnt/s3filessudo sed -i '/s3files/d' /etc/fstabexitIf the instance is already terminated or unreachable, skip this step.
-
Terminate the EC2 instance. Required
Look up the instance ID by name tag and terminate it:
Terminal window INSTANCE_ID=$(aws ec2 describe-instances \--filters "Name=tag:Name,Values=s3files-ec2-demo" \"Name=instance-state-name,Values=running,stopped" \--query 'Reservations[0].Instances[0].InstanceId' \--output text \--region $AWS_REGION)echo "Instance: $INSTANCE_ID"aws ec2 terminate-instances \--instance-ids $INSTANCE_ID \--region $AWS_REGIONWait for
terminatedstate:Terminal window aws ec2 wait instance-terminated \--instance-ids $INSTANCE_ID \--region $AWS_REGIONecho "Instance terminated" -
Delete the compute security group. Required
The compute SG must be deleted before the mount target SG (which has an inbound rule referencing it).
Terminal window COMPUTE_SG_ID=$(aws ec2 describe-security-groups \--filters "Name=group-name,Values=s3files-ec2-compute-sg" \"Name=vpc-id,Values=$VPC_ID" \--region $AWS_REGION \--query 'SecurityGroups[0].GroupId' \--output text)echo "COMPUTE_SG_ID=$COMPUTE_SG_ID"aws ec2 delete-security-group \--group-id $COMPUTE_SG_ID \--region $AWS_REGIONecho "Compute SG deleted" -
Delete all mount targets. Required
The file system cannot be deleted while mount targets exist.
Terminal window MT_IDS=$(aws s3files list-mount-targets \--region $AWS_REGION \--file-system-id $FS_ID \--query 'mountTargets[*].mountTargetId' \--output text)echo "Mount targets: $MT_IDS"for MT_ID in $MT_IDS; doaws s3files delete-mount-target \--region $AWS_REGION \--mount-target-id $MT_IDecho "Deleted mount target $MT_ID"doneWait until the list is empty:
Terminal window until [ "$(aws s3files list-mount-targets \--region $AWS_REGION \--file-system-id $FS_ID \--query 'length(mountTargets)' \--output text)" = "0" ]; doecho "Waiting for mount targets to delete…"sleep 10doneecho "All mount targets deleted" -
Delete the file system. Required
Terminal window aws s3files delete-file-system \--region $AWS_REGION \--file-system-id $FS_IDecho "File system deletion initiated"Confirm deletion:
Terminal window aws s3files list-file-systems \--region $AWS_REGION \--bucket $BUCKET_ARN \--query 'fileSystems[*].{id:fileSystemId,status:status}' \--output table -
Delete IAM roles and instance profile. Required
Remove the role from the instance profile first, then delete in dependency order:
Terminal window aws iam remove-role-from-instance-profile \--instance-profile-name s3files-ec2-instance-profile \--role-name s3files-compute-role-ec2aws iam delete-instance-profile \--instance-profile-name s3files-ec2-instance-profileDetach managed policies from the EC2 compute role:
Terminal window aws iam detach-role-policy \--role-name s3files-compute-role-ec2 \--policy-arn arn:aws:iam::aws:policy/AmazonS3FilesClientFullAccessaws iam detach-role-policy \--role-name s3files-compute-role-ec2 \--policy-arn arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCoreDelete the inline policy, then the EC2 compute role:
Terminal window aws iam delete-role-policy \--role-name s3files-compute-role-ec2 \--policy-name S3FilesComputeS3ReadPolicyaws iam delete-role --role-name s3files-compute-role-ec2echo "EC2 compute role deleted"Detach managed policies from the file system role:
Terminal window aws iam delete-role-policy \--role-name s3files-filesystem-role \--policy-name S3FilesFileSystemPolicyaws iam delete-role --role-name s3files-filesystem-roleecho "File system role deleted" -
Delete the mount target security group. Required
Now that mount targets and compute resources are gone, the mount target SG has no active references:
Terminal window MT_SG_ID=$(aws ec2 describe-security-groups \--filters "Name=group-name,Values=s3files-mount-target-sg" \"Name=vpc-id,Values=$VPC_ID" \--region $AWS_REGION \--query 'SecurityGroups[0].GroupId' \--output text)echo "MT_SG_ID=$MT_SG_ID"aws ec2 delete-security-group \--group-id $MT_SG_ID \--region $AWS_REGIONecho "Mount target SG deleted" -
Empty and delete the S3 bucket (optional).
Delete all versioned objects:
Terminal window aws s3api list-object-versions \--bucket $BUCKET \--output json \--query '{Objects: Versions[].{Key:Key,VersionId:VersionId}}' \| jq -c '.Objects // [] | _nwise(1000) | {Objects: ., Quiet: true}' \| while read batch; doaws s3api delete-objects --bucket $BUCKET --delete "$batch"doneDelete all delete markers:
Terminal window aws s3api list-object-versions \--bucket $BUCKET \--output json \--query '{Objects: DeleteMarkers[].{Key:Key,VersionId:VersionId}}' \| jq -c '.Objects // [] | _nwise(1000) | {Objects: ., Quiet: true}' \| while read batch; doaws s3api delete-objects --bucket $BUCKET --delete "$batch"doneDelete any incomplete multipart uploads:
Terminal window aws s3api list-multipart-uploads --bucket $BUCKET \--query 'Uploads[*].{Key:Key,UploadId:UploadId}' \--output json \| jq -r '.[] | "\(.Key) \(.UploadId)"' \| while read key upload_id; doaws s3api abort-multipart-upload \--bucket $BUCKET --key "$key" --upload-id "$upload_id"doneDelete the bucket:
Terminal window aws s3api delete-bucket --bucket $BUCKET --region $AWS_REGIONecho "Bucket $BUCKET deleted"
All EC2 walkthrough resources have been removed.