Extract AWS account number from access_key

This is a cool little snippet of code here, especially if you're trying to debug a terraform configuration with multiple AWS provider aliases, each of which connects to a different account number.

import base64
import binascii

def AWSAccount(AWSKeyID):
    trimmed_AWSKeyID = AWSKeyID[4:] #remove KeyID prefix
    x = base64.b32decode(trimmed_AWSKeyID) #base32 decode
    y = x[0:6]

    z = int.from_bytes(y, byteorder='big', signed=False)
    mask = int.from_bytes(binascii.unhexlify(b'7fffffffff80'), byteorder='big', signed=False)

    e = (z & mask)>>7
    return (e)

print ("account id:" + "{:012d}".format(AWSAccount("ASIAQNZGKIQY56JQ7WML")))

I wonder what else is encoded within the key.

Hat-tip to Jamey Owens for sharing this at work.