Know who you are

Are you frequently changing your AWS user accounts? Default regions? How do you keep track of where you are?

I just wasted 15 minutes wondering why are my EC2 instances not providong expected output. Once I called the commandlet with -Region eu-west-1 it was somewhat better. Once I also figured out I should probably get out of the restricted profile it was so much better.

So, how do you know who you are? No, whoami and your passport won’t do the trick here.

But thanks to this awesome blog post by Steve Roberts we can all have our identities displayed clearly. You know, just in case we forget who we are 🙂

It’s a long post, so let me highlight the area we’ll focus on:

It’s a beautiful function, I agree. But can you tell what it does?

Ignoring Exit Code lines, as we go through the code it becomes clear we’re slowly constructing $prompt variable.
PS + $StoredAWSCredentials + @ + $StoredAWSRegion + $pwd.ProviderPath + >
Oh, so sexy!

Let’s try it! Copy, paste, enter. Call it.

Errr… what?

Empty variables.
Why? Because defaults are not initialized! Let’s do so:

There we go!

Now let’s see what happens after changing the profile. Get available ones:

And activate a different one:

Woot! Updated immediately!

Now if I again get into same problem which prompted 🙂 me to search for prompt function, I’ll blame my alcohol intake.

If you frequently change accounts, you may want to have this in your PowerShell startup. Check $PROFILE variable for the script location and paste the script in it. Restart PowerShell session and you’re all green.

Well, blue. You know what I mean.

“But wait,” I hear you ask, “aren’t you going to tell us what $LASTEXITCODE is for?”

Of course not! *evil laugh*

It shouldn’t take you more than a few minutes to figure that one out. Send me an email if you have issues, but I think the investigation will yield useful experience.

Until next time, may the Force guide you.

Leave a Comment