Running Java app on EC2 AWS instance

Yesterday I bumped into a problem while writing an app that runs in Amazon EMR. For those of you that don’t know, EMR stands for ElasticMapReduce – it’s the Hadoop-ecosystem solution provided by Amazon. I needed to test this app I’m building, first locally and then in the cloud as well. To do so, I needed to provide the app with AWS credentials so it could effortlessly communicate with other AWS services (in my case with S3 – Simple Storage Service, as I intended to store some computation results for later processing).

Now, if you take a look at this article, you’ll find out that AWS SDK for Java provides several ways of credentials managing, to name some: by using Environment Variables, by using Java System Properties or if you are running in cloud, by using Instance profile credentials.

Which of these strategies will you use is all up to you. By default, if you choose to stay with the default one (and here I mean if you decide to initialize AmazonS3Client by using parameterless constructor), you’ll get a combination of:

  • InstanceProfileCredentialsProvider
  • EnvironmentVariableCredentialsProvider and
  • SystemPropertiesCredentialsProvider

as they are defined in DefaultAWSCredentialsProviderChain class. This combination is fine, unless you run your application on your local machine as well, and you actually define your credentials through default credential profiles file – typically located at ~/.aws/credentials.

At this moment I advice you to create your own CredentailsProviderChain that will fit your needs the best. The one I defined for application I’m working on looks like this:

And that’s it, you can start using combined credentials providers in your app!

Leave a Reply

Your email address will not be published. Required fields are marked *