AWS has a provider class that does something like this: "CredentialProvider", there's one for basic credential provision.