Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IAM role with AWS Java SDK

My use case is as follows:

I need to push some data into AWS SQS queue using JAVA SDK and by help of IAM role (not using credential provider implementation).

Is there any way to do that?

Thanks for help in advance.

like image 653
Lovey Avatar asked Aug 16 '16 20:08

Lovey


People also ask

How do I use IAM role in AWS?

To attach an IAM role to an instanceOpen the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . In the navigation pane, choose Instances. Select the instance, choose Actions, Instance Settings, Attach/Replace IAM role. Select the IAM role to attach to your instance, and choose Apply.

How do I use AWS SDK without credentials?

To prevent the SDK from requiring the credentials, you need to use the makeUnauthenticatedRequest method to place any calls. This allows you to call "an operation on a service with the given input parameters, without any authentication data". var AWS = require('aws-sdk'); AWS.

Do IAM Roles use STS?

STS is just a method of getting IAM credentials. You can't use STS without IAM. STS is most useful in orgs with multiple AWS accounts.


2 Answers

You can use role based authentication only on EC2 Instances, ECS Containers and Lambda functions. It is not possible to use them locally or on on premise servers.

DefaultAWSCredentialsProviderChain will automatically pick the EC2 Instance Role if it can't find the credentials via any of other methods. You can also create a custom AWSCredentialsProviderChain object with only injecting a instance of InstanceProfileCredentialsProvider to it like here

AWSCredentialsProviderChain myCustomChain = new AWSCredentialsProviderChain(new InstanceProfileCredentialsProvider());

For more info: https://docs.aws.amazon.com/java-sdk/latest/developer-guide/java-dg-roles.html

like image 140
Çağatay Gürtürk Avatar answered Oct 25 '22 02:10

Çağatay Gürtürk


It's been a while, but this is not currently the case, it is now possible to use assume role with the Java SDK with a user. You can configure credentials in your .aws/credentials file as follows:

[useraccount]
aws_access_key_id=<key>
aws_secret_access_key=<secret>

[somerole]
role_arn=<the ARN of the role you want to assume>
source_profile=useraccount

Then, when you launch, set an environment variable: AWS_PROFILE=somerole

The SDK will use the credentials defined in useraccount to call assumeRole with the role_arn you provided. You'll of course need to be sure that the user with those credentials has the permissions to assume that role.

Note that if you're not including the full Java SDK in your project (i.e. you're including just the libraries for the services you need), you also need to include the aws-java-sdk-sts library in your classpath for this to work.

It is also possible to do all of this programmatically using STSAssumeRoleSessionCredentialsProvider, but this would require you to directly configure all of the services so it might not be as convenient as the profile approach which should just work for all services.

like image 43
Jon Nichols Avatar answered Oct 25 '22 03:10

Jon Nichols