I want an awesome Elastic IP!

Warning: Long post ahead!
TL;DR = Download the script here: https://s3-eu-west-1.amazonaws.com/mypowershell.space/ps/New-NiceEC2Address.ps1

Well, so do I 🙂
In fact, I wanted a nice EIP so badly I was spamming New-EC2Address in my shell.

But IP addresses I got were so horrible (think and alike) I had to delete them almost instantly.

Argh! This works only for EC2 Classic. Okay, let’s use AllocationId, then.

Works. Good. But now I have 50 unwanted EIPs! There’s no way I’m going to remove then one by one.

“Select All -> Release Address” in AWS console may do the trick, but we’ll steer away from it. After all, the entire point of using PowerShell is that you don’t need console at all.

Let start with the premise that all unassigned Elastic IP addresses we have in the account are not wanted there. “Seek and destroy” time.

But how to know which ones are unassigned? Run Get-EC2Address and see if you can spot them.

Right, they don’t have AssociationId , InstanceId, NetworkInterfaceId, etc etc.

We’ll use AssociationId for our checks, as any EIP that doesn’t have it clearly not associated with anything 🙂

You may use NetworkInterfaceId if you want, but don’t rely on InstanceId. EIPs without it are not associated to an instance, but may be to a NAT Gateway and you’ll just generate unnecessary errors.

Get all rogue EIPs in a variable:

And loop through them and remove them one by one. If you’re in EC2 Classic, call Remove-EC2Address with -PublicIp parameter, as above:

“But wait!” I hear you cry, “I want an awesome Elastic IP, not delete old ones!”

I know, I know. And now we’re ready for it!

Keep in mind that if this helps you get a really awesome IP like I fully expect you to transfer it over to my account!

Just kidding, you cannot do that. But you can buy me a beer or lunch. Or, you know, send $1000 to my PayPal or something. I’m flexible like that 🙂

OK! IP grabbing time!

We’ll do this so often we may actually make a function for it. And if something goes wrong, like we hit maximum number of allowed EIPs for our account or API gateway starts throttling us because we’re being unreasonable, we may want a try/catch check.

Something like this:

What did we do here?

First we cleared the error check variable, so we don’t get a failure when we call the script for the second time. And then allocate a new Elastic IP and place it into $NewIP varible.

Note the $Script: notation! It will change the scope of our variable to script, meaning it will be accessible from any other function or a call. Very important.

And I forgot about it… Lost 15 minutes trying to figure out what’s wrong 🙂

Then we check if our EIP allocation failed. If it did with [System.InvalidOperationException] , that means we hit the max EIP number limit for our account. If it’s something else, well… it’s something else 🙂 Test and let me know if there’s something interesting.

First part done. We have an IP-gathering-error-checking function. What’s next?

Good question. What to do?

I suppose we can keep the address or drop it. And end the script right there or make it run once more. Add a default action so we can spam Enter key and perhaps also add some help for people who didn’t read this novel post.

And let’s go super-imaginative here and really call the function WhatToDo 🙂

Instead of going with a bunch of if commands:

I decided to use Switch here. Not because it’s quicker than if , which it is, but because I wanted to show something different and if is so 2010. And I like it, it looks neat.

Code is self-explanatory, isn’t it? We allocated new EIP for the user, and now ask him what he wants to do.

This is a perfect opportunity to go into him vs. her but the post is already longer than necessary so I’ll let it slide 😉

Options are what we described before, and it’s easy to add new ones.

Note recursive call to WhatToDo function inside itself. Why do we need it?

If a user calls help with ?, it will effectively exit the loop and end the script. We need to present him with options again and let him choose.

We’ll pass variables $RemoveEIP and $Continue outside of the function again. It may look clunky this time, and we should probably do it differently, but since we know what $Script: does, we’re happy to move on 🙂

Now what?

Now user submitted a choice whether to keep or drop the EIP. If it will be kept, there’s nothing to do. If it’s to be removed, we should – you got it – call yet another function to do it!

Why? Well, why not? In GetNewIP function we have both the skeleton for this one and error checking in place. So let’s be lazy clever and re-use it.

Same logic applies. Only this time we’re not allocating EIP but removing it. If something goes wrong – let the script scream at us in yellow/red combination.

Speaking of color combinations, I used almost exclusively Write-Host in this script. Why? Three reasons.

We’re not manipulating objects here, so it doesn’t really matter. We’re also not logging anything and everything is output to console only, so again it doesn’t matter. Third, we get to use colors! Script is interactive, so it makes it look nicer, right?

See? You agree, too. I knew you were awesome!

We have our three functions now, so what’s left to do is to put them to work together. The last part will actually be the shortest since we’ll more or less just be calling them. While also using shiny colors!

And there it is.

Start the loop and get the IP. Present it to the user and ask him what to do. If he doesn’t like the IP remove it, if he does display it. If he chose to run the script again, reload the loop.

Pretty simple, right?

Thank you for bearing with me through another longer-than-really-necessary blog post!

I really enjoyed writing it, and hope it didn’t require you to channel your inner masochist in order to survive 🙂

If you read more than one of my posts, I’m afraid I may have some bad news for you 😀

Here’s the entire script, so you can copy it if you like (5th icon in the script header) or click here to get it from Amazon S3:

Leave a Comment