Creating a Basic AWS Credential Provider in Java
@kylewbanks on Jun 27, 2013.
Written by Hey, if you didn't already know, I'm currently working on an open world stealth exploration game called Farewell North in my spare time, which is available to wishlist on Steam!
Amazon recommends using a properties file to supply AWS credentials to your Java application, and while this is generally the most appropriate way to do it, what happens if you want to dynamically connect to different AWS accounts, or for whatever reason you need to provide the credentials in code?
The solution to this is to implement the AWSCredentials interface and override the getAWSAccessKeyId() and getAWSSecretKey() methods, like so:
public class AWSCredentialProvider implements AWSCredentials { @Override public String getAWSAccessKeyId() { return "AWS-Access-Key"; } @Override public String getAWSSecretKey() { return "AWS-Secret-Key"; } }
Because this is a Java class, you can obtain and return the keys however you like. When it comes time to create an AWS client, such as an AmazonEC2Client for example, you just pass an instance of AWSCredentialProvider:
AmazonEC2Client ec2Client = new AmazonEC2Client(new AWSCredentialProvider())
Let me know if this post was helpful on Twitter @kylewbanks or down below!