EC2 Scripts

December 9th, 2024
cli, tech
I do a lot of work on ec2 instances, and one thing that makes this easier is having a few scripts for manipulating them. These are very small, small enough to go in my .bashrc, but still make my life a lot easier.

The first one is start_ec2:

alias start_ec2='aws ec2 start-instances --instance-ids i-NNNN'

I have an instance I do most of my work on, and this command starts it. Way better than logging into the AWS Console like I used to do.

After a few seconds I run:

function ssh_ec2() {
  ADDR="$(aws ec2 describe-instances \
           --instance-ids i-NNNN \
           --query 'Reservations[].Instances[].PublicDnsName'
           --output text)"
  if [ $? != 0 ] || [ -z "$ADDR" ]; then
    echo "Instance not running."
    return
  fi

  scp "ec2-user@$ADDR:.full_history" \
      /path/to/ec2-full-history-backup.txt

  ssh "ec2-user@$ADDR"
}

This figures out the IP of the instance, copies down my (very important) full shell history, and logs me in over ssh.

I don't have a command for shutting down remotely: I just run sudo shutdown -h now while logged in.

The last command, and probably my favorite, is resize_ec2:

function resize_ec2j() {
  aws ec2 modify-instance-attribute \
   --instance-id i-NNNN \
   --instance-type "$1"
}

For example, resize_ec2 c6a.xlarge or resize_ec2 c6a.32xlarge. Depending on what I'm doing I might need very different specs, and I don't want to pay $4.90/hr when I only need a $0.15/hr machine. It does take a mildly annoying few minutes for a machine that has just shut down to transition into a state where you can resize it, but it's not too bad.

Comment via: facebook, lesswrong, mastodon, bluesky

Recent posts on blogs I like:

Where I Donated In 2024

All Grants Fund, Rethink, EA Funds Animal Welfare Fund

via Thing of Things January 17, 2025

2024-25 New Year review

This is an annual post reviewing the last year and setting intentions for next year. I look over different life areas (work, health, parenting, effectiveness, travel, etc) and analyze my life tracking data. Overall this was a pretty good year. Highlights …

via Victoria Krakovna January 15, 2025

The ugly sides of two approaches to charity

What's neglected by "magnificent" philanthropy, and by Singerian global poverty focus The post The ugly sides of two approaches to charity appeared first on Otherwise.

via Otherwise January 13, 2025

more     (via openring)