Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set AWS Container Credentials using AWS Java SDK

I want to use ContainerCredentialsProvider(CredentialsEndpointProvider) instead of ContainerCredentialsProvider(), as the latter is deprecated.

Currently I am using deprecated constructor ContainerCredentialsProvider() as below:

AWSSimpleSystemsManagement ssm = 
    AWSSimpleSystemsManagementClientBuilder
        .standard()
        .withRegion(region)
        .withCredentials(new ContainerCredentialsProvider())
        .build();

CredentialsEndpointProvider is an abstract class. I need to use something like ECSCredentialsEndPointProvider in my docker and I am not sure how to do that. Any help is appreciated.

like image 214
nad87563 Avatar asked Nov 21 '18 20:11

nad87563


People also ask

How does AWS SDK get credentials?

Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/ . In the navigation pane, choose Users. Choose the name of the user whose access keys you want to create, and then choose the Security credentials tab. In the Access keys section, choose Create access key.

Can I use AWS SDK without credentials?

To make requests to Amazon Web Services using the AWS SDK for Java, you must use cryptographically-signed credentials issued by AWS. You can use programmatic access keys or temporary security credentials such as AWS IAM Identity Center (successor to AWS Single Sign-On) or IAM roles to grant access to AWS resources.

Can you interface with AWS using Java SDK?

Develop and deploy applications with the AWS SDK for Java. The SDK makes it easy to call AWS services using idiomatic Java APIs.


Video Answer


2 Answers

I know this is kind of late, but hopefully this will help others like me who came to this question even in 2020 :)

Using your code's example, you should try out this

AWSSimpleSystemsManagement ssm = 
    AWSSimpleSystemsManagementClientBuilder
        .standard()
        .withRegion(region)
        .withCredentials(new EC2ContainerCredentialsProviderWrapper())
        .build();

The EC2ContainerCredentialsProviderWrapper implements AWSCredentialsProvider, and loads credentials from Amazon Container (e.g. EC2) Credentials, solving them in the following order:

  1. If environment variable "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" is set (typically on EC2) it is used to hit the metadata service at the following endpoint: http://169.254.170.2
  2. If environment variable "AWS_CONTAINER_CREDENTIALS_FULL_URI" is set it is used to hit a metadata service at that URI. Optionally an authorization token can be included in the "Authorization" header of the request by setting the "AWS_CONTAINER_AUTHORIZATION_TOKEN" environment variable.
  3. If neither of the above environment variables are specified credentials are attempted to be loaded from Amazon EC2 Instance Metadata Service using the InstanceProfileCredentialsProvider.

This is similar to the deprecated ContainerCredentialsProvider() :

By default, the URI path is retrieved from the environment variable "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" in the container's environment.


Update: If you are not sure which mechanism will be used or want to be compatible with environment variables, system properties, profile credentials and container credentials, you could use the DefaultAWSCredentialsProviderChain which will make sure to try out all options (as @Imran pointed out in a comment):

AWSSimpleSystemsManagement ssm = 
    AWSSimpleSystemsManagementClientBuilder
        .standard()
        .withRegion(region)
        .withCredentials(new DefaultAWSCredentialsProviderChain())
        .build();

For example, the implementation for the 1.11 SDK looks like this (it basically tries all the options until it finds one that works):

public DefaultAWSCredentialsProviderChain() {
    super(new EnvironmentVariableCredentialsProvider(),
          new SystemPropertiesCredentialsProvider(),
          new ProfileCredentialsProvider(),
          new EC2ContainerCredentialsProviderWrapper());
}

This way you are compatible with new versions that may introduce another type of authentication or if one option gets deprecated.

like image 114
Jonathan Lee Avatar answered Nov 03 '22 00:11

Jonathan Lee


It seems to me you are meant to use the EC2ContainerCredentialsProviderWrapper:

val ecsCredProvider = new EC2ContainerCredentialsProviderWrapper()
like image 39
trudolf Avatar answered Nov 03 '22 00:11

trudolf