How to notify/email yourself when an EC2 instance terminates

Edit: I’ve come to believe that one premise of this article is wrong. You definitely can send email from an EC2 instance; Django will send error emails from a production server. So instead of using SNS, it would probably be easier to trigger some kind of simple email sending Python (or whatever) script when the termination event happens.

I make pretty heavy use of EC2 spot instances, which as you know can terminate at any time with no warning.

In order to get my spots back up ASAP, I’d like be notified when they terminate.

This turned out to be much harder than I expected. I thought I’d be able to add a simple script that would send me an email when the instance shuts down (Amazon is nice enough to send a shutdown command on termination instead of just pulling the plug.)

But that approach has a couple of problems.

First, you can’t easily send email from EC2 instances (because of spammers) and have to manually get instances whitelisted by elastic IP which is a pain.

Second, it’s not the easiest thing to write a shutdown hook in Linux.

So, here’s a solution for both of those problems.

The email problem

Amazon has a service called CloudWatch that seems great for this, except that it can only monitor metrics emitted by running instances. So you can’t set it to alert you on a system shutdown, nor on a metric polling failure because it randomly misses packets all the time.

So the solution is a different Amazon service called SNS (simple notification service) that will let you trigger an event that can be configured to send you an email. So we’re going to write a script that tells SNS to send us an email.

(SNS is free for ~200k requests/month, so unless you’re planning on doing something nuts this approach should have no marginal costs.)

To do that, you first need to set up a “topic” in SNS. Go to the SNS dashboard and

1) click “create topic”.
2) Create a topic called “instance_down” or whatever you like.
3) Click the topic and click “create subscription”
4) Choose protocol “email” and enter your email address as the endpoint

To use SNS in a script, we need the AWS command line tools.

If you already have pip installed, just

pip install awscli

Then:

touch ~/.awsconfig
emacs ~/.awsconfig

Make it say:

[default]
AWS_ACCESS_KEY_ID=<< YOUR AWS ID>>
AWS_SECRET_ACCESS_KEY=<< YOUR AWS SECRET KEY>>
region=<< YOUR REGION >>

Create an init.d script:

sudo emacs /etc/init.d/ec2-shutdown

Make it say:

#! /bin/sh
### BEGIN INIT INFO
# Provides: ec2-terminate
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: restart
# Description: send termination email
### END INIT INFO
#

export AWS_CONFIG_FILE={{ YOUR CONFIG FILE }}
export AWS_DEFAULT_REGION={{ YOUR REGION }} # config file not picking up region for some reason
sudo -E aws sns publish –topic-arn {{ YOUR SNS ARN }} –message “ec2 ser
ver {{ YOUR IDENTIFIER }} went down at $(date)”
sleep 3 # make sure the message has time to send

exit 0

That script will tell SNS to send you a email saying your server is down and giving the time. You can customize the message however you like.

The Shutdown Script Problem

So how do we make this run on shutdown?

We’re going to use init.d scripts. It’s taken me a little while to get my head around how this works, but in a nutshell…

Linux has a concept of “runstates”, which include things like “shutdown” and “logged in”. You can tell Ubuntu to run shell scripts when it changes into a runstate by placing scripts in certain folders in side of /etc. The two states that concern us are rc0 and rc6, i.e. “shutdown” and “reboot”, which correspond to folders /etc/rc0.d and /etc/rc6.d respectively.

Ubuntu has a command line tool, update-rc.d that will automatically symlink scripts into the appropriate folders depending on the CL paramaters you pass it. It’s all a bit complicated, but all you need to do here is:

sudo update-rc.d ec2-shutdown start 10 0 6 .

(This says run the script on entering run states 0 and 6, shutdown and reboot. And put it 10th in the list of things to do)

And that should do it! Now your instance should send you an email whenever it shuts down, terminates or reboots. I’m not sure how limit to just EC2 termination, but please comment if you know!

If you have any problems, leave a comment.

And if you find this useful, consider following me on Twitter.

Links to all the parts of my “Everything About Economics” Blog Series

Back linking has gotten unwieldy, so I’m going to just update links on this page to all the parts from now on:

IntroThe future of the economy, in bite-sized chunks.

Part 1What is An Economy?

Part 2(a)Rich Hypothetical Society, Poor Hypothetical Society

Part 2(b)Productivity is Prosperity

Part 3: How Productivity Grows

Part 4: Computers and Productivity

Part 5: Lateral Productivity Growth

More Thoughts on Free Will

A couple of weeks ago, I left a comment on Albert Wenger’s excellent blog “Continuations” about Free Will. The comment was rather long, so I reposed it here on my blog. Somewhat amazingly, Albert read my comment and left a thoughtful reply.

I’m not sure of the etiquette of reposting Albert’s comment, but to summarize he said that my argument was off-base because the “information layer” I described is just an abstraction and doesn’t interact with the physical world. No interaction means no influence, and that means that the stimulus->thought->action sequence remains a deterministically closed loop.

I, logorrheic that I am, responded with another rather long comment. This is that comment.

Well, actually this is:

If it’s true that the “information layer is entirely abstract” (i.e. completely non-interacting with the tangible world) then your whole argument is clearly right.

But I’m not sure that premise is true. It seems that the informational world is constantly interacting with the physical world in the form of “physical laws being followed.”

Metaphorically, particles in the world are “data” and the laws of physics (i.e. the information layer) are “source code.” Every particle interaction is (metaphorically) a computation. Now all of our observation to date suggests that the “source code” of the universe is stable and fixed. But any honest physicist will tell you that our (extremely elegant) mathematically descriptions of how objects have behaved in the past tell us nothing about why they behaved that way.

I raised GEB to show that “laws” aren’t necessarily well-behaved in edge cases.[1] And so similarly, the fact that we’ve observed that some parts of the “Ur-program” work one way does NOT demonstrate conclusively that there aren’t other parts of the program that introspectively alter the Ur-program’s source code and lead to measurable, experimentally verifiable physical effects.

You raise one potential “point of strangeness” in your PS – quantum waveform collapse. My knowledge of quantum mechanics and neuroanatomy is limited, but my best understanding is that neural interactions are sensitive enough that quantum effects can make the difference between whether a neuron fires or not. And we know from computers (cf. my Caesar’s cipher example above) that changing one bit in a computation chain can radically alter the downstream result, so any slight (non-random!) bias in the quantum coin-flip could be enough to completely alter behavior. (And as far as I know would not violate the conservation of energy or any other physical laws except our observation that quantum collapse is usually random.)

Could patterns of thought directly alter the wave-form collapse? Well, we already know that the quantum world is altered by observation per se. And conscious introspection is fundamentally self-observation. So it is possible that when the wetware finds itself in physical states that mirror certain “information patterns” (e.g. “contemplating two choices”), the presence of those patterns triggers an alteration in the source code that directly influences the way quantum collapse happens in connected synapses, leading a neuron to fire that otherwise wouldn’t. And from the GEB problem, we might expect the “meta program” to be especially poorly behaved (perhaps even fundamentally indeterminate, i.e. “free”) in this circumstance.

So is this explanation likely? That’s hard to say. It’s neither confirmed nor contradicted by existing data. It’s counter-intuitive, but so is every other part of quantum mechanics.

But best of all, this theory is testable. We just need to carefully measure the statistical in vivo quantum behavior of synapses during decision-making and I can be proven wrong once and for all. 🙂

[1] I admit I’m not sufficiently demonstrating that “logic laws” work the same ways as physical laws but there is certainly a remarkable symmetry. It doesn’t seem at all fundamentally necessary that physical laws should be mathematically expressible and elegant. I’m happy to write more on this point if anyone is curious.

Everything you need to know about economics in 400 words, Part 3

It seems I only write on this topic while flying. So I guess you can expect this series to wrap up sometime in 2015.

But to recap, last time I told you that productivity is prosperity. So all increases in per capita wealth come from increases in the amount of economic goods a single worker can produce in an hour.[1]

So how does productivity increase? Let’s start with the obvious. We get the stuff we have by making complicated stuff out of simpler stuff. We start with iron and chemicals, apply some process over a period of time, and end up with steel. Visually:

(1 unit of Time + 1 unit of Iron + 1 unit of Chemicals) => 1 unit Steel

The productivity of this process is simply the ratio of inputs to outputs. If we rejiggered the above process to produce 2 units of steel from the same input, we would have doubled productivity.  Ditto if we halved the amount of time.

Physical productive processes are closely analogous to computer algorithms. We could just as easily talk about the “productivity” of a bitcoin mining operation in terms of how many BC a computer can generate per hour.

So how do we get more productivity out of our processes (which, to reiterate, is the only way we get richer overall)? Well, just like with computer algorithms we have two options:

Do the same thing faster (e.g. get a faster CPU)

Swap in a better algorithm (e.g. use quick sort instead of bubble sort)

“Do more faster” is an appealing tagline, but unfortunately human land-speed doesn’t follow Moore’s law. So our best option is to find better algorithms for our productive processes.

So transitively, the best way for society to get richer is to devise and select better processes. By and large, we call process improvements technology. The cotton gin and the Bessemer process were technologies that allowed radically more efficient transformation of a raw commodity into a finished product.

One of the most important stories of the last 30 years has been the way that computers have inserted themselves deeply into our economy’s various productive processes. But most of what we’ve seen so far are “do the same thing faster” improvements. The real productivity revolution will come once computers consistently help us choose better algorithms for our processes and for our lives.

 

[1] Okay, all increases except one-off “gold strikes” that shower us with random natural riches.

Is Free Will a Buffer Overflow?

[Once again, I’ve been inspired by a post on Albert Wenger’s blog, this time about whether or not free will exists.]

I’ve spent a long time thinking about this one as well. I think you’re probably right, but here’s the best alternative I’ve been able to come up with. These ideas are heavily inspired by having finally finished Gödel, Escher, Bach.

Basically, it’s clearly true that determinism holds over the space of physical objects if you assume that a continuous set of physical laws governs all physical interactions. If my brain is just a physical object, then the arrangement of its atoms at present is exclusively a function of their arrangement in the past.

But how justified are we in making that assumption about the continuity and coverage of physical laws?

Let’s start with coverage: it’s actually not hard to think of something that doesn’t interact with the physical world in quite the same way as an apple – a computer program.

Yes, a program is represented by marks on a disk, and yes it is executed by a set of magnets that write and erase those marks using electricity routed through a CPU. But ultimately those purely physical components produce an informational state projected through my screen. And as I write I am interacting not only with the keys of my keyboard but also but with the abstract state of the system. And the state per se has causal power: perform a Caesar cipher on your blog post and it becomes virtually meaningless to me despite having extremely similar statistical properties and on-disk representation.

So then where does the meaningfulness of the program state reside? Can we point to a memory address that distinguishes “meaningful” from not meaningful (even in principle)? Can we point to an atom in my brain that makes the text meaningful to me, even though it would remain meaningful to you even if my brain we destroyed (and vice versa) but become meaningless if all brains were destroyed?

The point here is that even in closed physical systems, there is an information “layer” that has both its own rules (i.e. “logic”) and yet tangibly influences the physical world. This is the layer where the program-ness of a program resides, or the law-ness of physical laws, or the meaningful-ness of a sentence.

Normally this layer is well behaved – I encode my experiences into words that you understand; when fundamental particles interact they “check” which laws to follow and then they follow them.

But the layer has known flaws, specifically when self-reference enters the picture. Gödel’s incompleteness theorem establishes that there are questions which can be precisely formulated but not decided, e.g whether the statement “this statement is a lie” is true or not (or the somewhat related question of whether an arbitrary computer program will ever terminate.) These questions aren’t undecidable because of a deficit in our ability to measure (a la Heisenberg uncertainty) but rather because of an irresolvable quirk in the laws of the information layer.

The interesting thing about consciousness (the domain within which free will would presumably operate) is that it is deeply and inherently self-referential. The very exercise of writing this comment is an example of an information state reflecting on its own statefulness.

Now let’s circle back to the question of consistency in physical laws. Usually it seems like the surest bet: no one has ever been fired for predicting that an apple will obey gravity.

But when we start talking about the way that particles in our brain interact with the information layer (in ways we understand only loosely, at best) and we focus in on an area (self-reference) where the information layer is already know to misbehave, how confident can we be that the rules we’ve observed elsewhere operate the same way here?

Or more colorfully, how certain can we free will isn’t some sort of buffer overflow in the operating system of the universe?

[Edit: if you like this post, please upvote on Hacker News! Look for the post titled “Is Free Will a Buffer Overflow?”]

The Game Theory of Google Reader

Google Reader is one of top five most-used websites. I literally always have a reader tab open in Chrome. So when I heard that Reader is shutting down on July 1st, I was like

alt text

But after a while I was like

alt text

And now I’m like…

alt text

…because I’ve concluded that I’m glad Google is shutting down Reader.

Why, dear reader? Because of Game Theory (obviously)! By shutting down Reader, Google is breaking a bad Nash Equilibrium which has been hobbling the progress of web content distribution.

Whoa, what does that mean?

Well, “Game Theory” is simply a mathematical examination of the ideal strategy for playing games. Unsurprisingly it often applies to business and other competitive situations.

Let’s consider the canonical example Game Theorists love to use: “the prisoner’s dilema”. In this scenario, the cookie monster and his brother (Charleston) have both been arrested for, you guessed it, home invasion and murder. The cops put them into two separate cells and start questioning them.

“Now look, cookie monster, I’m not going to lie. Our evidence here is weak. If neither of you confess then we’re going to have to take this to trial, and you might both get off. But I’ll offer you a deal. If you testify against Charleston, we’ll give you immunity and five cookies. Unless, of course, he testifies against you in which case you’ll be learning to count to twenty to life.”

The police say the same thing to Charleston. So what is the cookie monster to do? He has two choices: “cooperate” with Charleston and go to trial, or “defect” and rat him out in exchange for sweet cookies (or long jail time if Charleston also defects.) Charleston has the same choices.

To figure out what both should do, we can make a simple matrix:

alt text

Cookie Monster is Player A, Charleston is Player B, and the numbers in each square are the number of cookies each gets in each situation, respectively. (No, the numbers don’t match up; I’m borrowing the diagram.)

Obviously, both monsters are better off if they both cooperate. But that won’t happen, at least not assuming that both care exclusively about maximizing their individual cookie intake.

Why not? Remember that our monsters are in separate rooms and cannot coordinate. Also, notice that each monster is individually best off in the situation where the other monster cooperates but he defects. So if either monster suspects that the other one will be generous and cooperate, his individual narrowly rational best option is to defect. And so he will.

alt text

Now the phrase “Game Theory” might quite appropriately make you think of the movie “A Beautiful Mind”. And if you saw that movie, you might think that Russell Crowe (a.k.a. “John Nash”) got academically famous for figuring out how to use game theory to pick up women in bars. But actually he got famous for inventing the concept of a “Nash Equilibrium”.

To over-simplify, a Nash Equilibrium is a set of player choices in a game (e.g. “Cookie monster -> defect”, Charleston -> defect" in the prisoner’s dilema) that will produce an outcome which neither player can improve by changing his choice unilaterally.

Take another look at the prisoner’s dilema diagram. The bottom right square is a Nash Equilibrium because, even though it is the worst outcome it’s the best that either player can do for himself. If Charleston thinks cookie monster is going to defect, he makes himself even worse off by cooperating. Same for cookie monster.

So in games where a Nash Equilibrium exists, we can generally expect that it will come to pass.[1][2]

So how does this all apply to Google Reader? Well, the existence of Google Reader has created and trapped us in a bad Nash Equilibrium. In this case, Google isn’t actually a player. Google is literally and figuratively out of the game – they haven’t cared about Google Reader in years and have let it wither on the vine.[3]

Instead, the players are consumers of RSS content and developers of alternative RSS readers.

The developers have two choices,

  • develop a new, better reader
  • do nothing, and let people continue to use Google

And the consumers have two choices:

  • switch to an alternative
  • keep using Google reader

alt text

We end up with something that looks an awful lot like the prisoner’s dilema, but with a few key differences.

It’s still the case that everyone is better off if consumers and developers cooperate on moving to a new reader. But it’s no longer the case that one side gains because the other loses. This is actually a game with two Nash Equilibria (develop, switch) and (don’t develop, don’t switch.) And instead of being played once in a jailhouse, this decision process is repeated every day.

Games with multiple equilibria (which I am of course oversimplifying here) introduce a fascinating question: “given that there’s a better state which both sides can agree is better for them individually how can move to that better world?” Remember that in a Nash Equilibrium, neither side can improve her outcome by changing her move unilaterally. But if the sides can coordinate then it’s as simple as agreeing to change their decisions at the same time.

But in the real world of RSS readers, developers can’t practically coordinate en masse with consumers. Each has to speculate in isolation about what the other will do. So we end up in a world where fear reigns and consumers don’t investigate alternative RSS readers because they fear that the other options are even more primitive. And although developers could make something much better, they don’t want to risk investing the development time and having no one show up (since GR is “good enough” for most people.)

And so for years we’ve been muddling along in the bad Nash Equilibrium of continuing to use GR and letting RSS become “uncool” relative to social sharing (despite being, IMHO, about 10x more useful.)

But when Google shuts down Reader, the whole game changes. Suddenly the bad Nash Equilibrium square is out of play. That means that coordination between developers and consumers is no longer necessary because “staying put” is no longer an option.

The immediate aftermath might be painful, but I predict that we’ll all end up with a much better system.

Also, note that this dynamic is hardly exclusive to GR. It’s all over the tech world: some company develops a product which gets popular; they lose interest and stop improving. But they keep the product alive, and the network effects and switching costs keep people using it even though much better mechanisms are theoretically possible. The two worst offenders I can think of are:

  1. Craigslist
  2. Javascript

Both are terrible but both are extremely widely used. And so we’re stuck in a bad Nash Equilibrium where everyone suffers because it’s too hard to get people to sell their broken furniture on a new website when they can just use Craigslist and it’s too risky for Mozilla to implement Python in the browser when developers might just keep kludging JS.

If Craigslist would just take a lesson from Google and shut down completely, it would be one of the best days ever in tech.

Can you think of better examples of “okay” products that could help the world by just disappearing? Please comment and let me know!

[1] Game theory is complicated, and there are a lot of exceptions depending on circumstances. But simplification usually gets us close enough.
[2] The prisoner’s dilema is one of the saddest abstract constructs I know. In a painful nutshell, it explains why “we can’t all just get along.”
[3] Not because they don’t care per se, but because it’s just not impactful enough relative to their other lines of business

LinerNotes Liveblog #7 — SXSWi

Things in Austin got weird. There was carousing, there was trespassing,  and yes, there was even laser tag. But after seven crazy days in Austin, my SXSW adventure is over. Instead of sharing a small Austin house with ten people (including two different bands), I’m now just sharing a house with my parents as I visit the Bay Area for some meetings.

SXSW was definitely fun, but it was also probably a waste of time.[1] I probably could have seen that coming – one of the clearer lessons of my time working on LinerNotes is to be very skeptical of conferences. I’ve been to several of the bigger ones (mostly because I managed to snag free tickets), and none of them have delivered enough value to clearly justify the time it took to attend, much less the the jaw-dropping ticket prices (two thousand dollars for TC Disrupt? Seriously?)

Still, out of all the hundreds of tech conferences SXSW retains a somewhat mystical reputation for “serendipity,” i.e. that simply by walking around and talking to people one might encounter the investor or business partner one needs to “kick it up a notch.” That’s a tempting offer, since I do believe in serendipity (or rather the darker flip-side that many endeavors will be destroyed by the absence of serendipity.)

My skepticism almost made me follow Danielle Morrill’s advice and skip SXSW, but a few things fell in place at the last minute (i.e. I found a place to stay for free) and so I decided to take the plunge.

And…I was disappointed.

Serendipity did not strike. I’m sure that it did for some people, but I didn’t meet even one new person with whom I plan to stay in close contact. To be fair, I didn’t do much to maximize my luck surface – I only went for the last two days, I didn’t buy a conference badge ($800?!) and I only struck up conversations with 20% of the people I randomly stood next to in lines instead of 100%. Still, the conversations I did have didn’t give me the sense that my digital soul mate was just one forced conversation away.

The basic problem is with the odds. When SXSWi was smaller, it may have been viable to talk to a large percentage of attendees and either meet someone useful or meet someone who could introduce you to someone useful they just met. But now there are 30,000 people at SXSWi. Let’s assume that’s 1.5% of the entire startup world, and let’s assume that the population is representative of the general startup population.  [2]

What are my (very rough) odds of meeting someone useful? Well, I’d estimate that there are probably less than 500 viable co-founders and 500 viable investors for me in the whole world, and LinerNotes is not really at a point where I’d benefit much from any business development relationships or marketing hires. If 1.5% of those people are at SXSW, that’s approximately 30 people at SXSW worth meeting.

If I’m there for two days, I probably come into physical proximity with 300 people in a day or 600 in total. That’s 4% of the conference. 4% of 30 is 1.2, meaning that in the whole two days, I was probably near someone useful about twice.

Now how many conversations with strangers can I reasonably have in a day? If I’m really forcing it, maybe 20. So that’s (approximately) 40 dice rolls with a 599/600 odds per roll of not meeting someone useful. That’s an 93% chance of failure, which implies a 7% chance of meeting someone useful, if I’m aggressively social and if the population is really representative (which it probably isn’t).

So after all that expense, travel, and foregone working time, the odds of serendipity striking are about 1 in 15. Compared to everything else I could do with those resources, going to SXSW for networking is pretty clearly not worth it.

So why does SXSW continue to have a reputation for magical serendipity?

Simple, because only the 1 in 15 people who benefit bother to write about it or tell their friends. No one likes a party pooper and so other than me and Danielle Morrill, few people want to loudly admit to wasting their time in Austin.

To be clear, I don’t regret going. The main purpose of my trip was actually to go to SXSW Music and do market research for LinerNotes, and it was worth sacrificing a weekend of work to have some fun and confirm my suspicions about SXSW. And I did meet up with a few old friends at SXSWi and got to strengthen those relationships.

There are some people for whom SXSW still does make sense (specifically startups that sell to marketers or app makers, or VC’s and later stage startup employees who just want to relax and party.) But now I can say from experience that there are better ways to make new friends than shouting at strangers at a loud, crowded Taco Bell sponsored party.

 [1]  This post only applies to SXSW Interactive. The dynamics of SXSW Music were much different and probably need their own blog post.

[2] This is obviously a rough calculation. These numbers are mostly made up with the goal of being ballpark correct and having errors cancel out. And even very generous assumptions don’t change the overall picture much.