No space left on your Cloud9 environment? Here's how to solve that

While using Cloud9, and installing node dependencies, my instance ran out of space. I couldn't finish setting up my project, and the terminal's autocompletion was dead in the water.

No space left on your Cloud9 environment? Here's how to solve that

Introduction

Personally, I don't like setting up my clients projects on my machine. They often require different versions of different tools, most of which I'd rather not have. And they take up valuable space.

So how do we solve that? I thought about using different user accounts. I've found it's a good way to have all the different browser tabs segregated by client or project. Much better than a browser window for each (which quickly eats up memory and becomes a mess). But some tools are installed globally, it's slow to setup on a mac, and it takes up some space.

Next, I thought about using a VM. Those are slower than the actual machine, but why not? One machine per client or project. Each cleanly, perfectly and absolutely separated in it's own little window. Nice. Except for the space. A full OS install each.

At some point I created a second macOS install, just for work, but realized that was actually worse than VMs.

Issue

Now... if only I could get rid of the space problem... Oh, how about setting up the VM on someone else's machine? That's how I ended up using Cloud9. Not perfect. Can't make iOS apps with it. But at least I can rid my machine of of my clients' web projects. Awesome!

Until I started installing all their node modules on it and ran into No space left on device. Dammit. The terminal's autocompletion wouldn't even work anymore. What was that?

Cloud9 instances rely on AWS EBS volumes for storage, and the default one is moderately small (10GiB). It appears installing a 'few' dependencies (28 dev dependencies & 8 'regular' ones) fills it up pretty quickly. Especially given node's habit of bringing in thousands of packages, and their grandmothers, when you ask for one or two. But that's another story.

I needed more space.

Meme: The amount of space in this Cloud9 environment IS TOO DAMN LOW!

Solution

So thank you Richard for your kind StackOverflow answer linking to the exact AWS Documentation page I needed, that somehow didn't pop up in the Google results (the following is a shameless paraphasing of said documentation page, mostly intended for my future self).

Basically, we need to resize our EBS volume.

For that, we create a .sh file at the root of our environment (ie resize.sh).

Then we edit it and write this script, kindly provided by AWS, into it:

#!/bin/bash

# Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}

# Get the ID of the environment host Amazon EC2 instance.
INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id)

# Get the ID of the Amazon EBS volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances \
  --instance-id $INSTANCEID \
  --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
  --output text)

# Resize the EBS volume.
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE

# Wait for the resize to finish.
while [ \
  "$(aws ec2 describe-volumes-modifications \
    --volume-id $VOLUMEID \
    --filters Name=modification-state,Values="optimizing","completed" \
    --query "length(VolumesModifications)"\
    --output text)" != "1" ]; do
sleep 1
done

#Check if we're on an NVMe filesystem
if [ $(readlink -f /dev/xvda) = "/dev/xvda" ]
then
  # Rewrite the partition table so that the partition takes up all the space that it can.
  sudo growpart /dev/xvda 1

  # Expand the size of the file system.
  # Check if we are on AL2
  STR=$(cat /etc/os-release)
  SUB="VERSION_ID=\"2\""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/xvda1
  fi

else
  # Rewrite the partition table so that the partition takes up all the space that it can.
  sudo growpart /dev/nvme0n1 1

  # Expand the size of the file system.
  # Check if we're on AL2
  STR=$(cat /etc/os-release)
  SUB="VERSION_ID=\"2\""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/nvme0n1p1
  fi
fi
Yes, this is shamelessly copy-pasted from the documentation

And, finally, we make it executable and run it with the desired new size for our EBS volume! (in my case, 15GiB)

chmod +x resize.sh
./resize.sh 15
This too

And that's all! Hope it can help some other poor soul out there the way it helped me.

Welcome to the tropical paradise of "You now have enough disk space" island! - Photo by Emiel Molenaar / Unsplash

Epilogue

For now, I have stopped using Cloud9 as a development environment, mostly because of some CORS and Docker issues, but I'm pretty sure those can be ascribed to my inexperience with frontend development and containers.