AwsCredentials credentials() {
        if (!StringUtil.isNullOrEmptyAfterTrim(awsConfig.getAccessKey())) {
            return AwsCredentials.builder()
                .setAccessKey(awsConfig.getAccessKey())
                .setSecretKey(awsConfig.getSecretKey())
                .build();
        }
        if (!StringUtil.isNullOrEmptyAfterTrim(ec2IamRole)) {
            return fetchCredentialsFromEc2();
        }
        if (environment.isRunningOnEcs()) {
            return fetchCredentialsFromEcs();
        }
        throw new NoCredentialsException();
    }
@Test
    public void credentialsEc2IamRole() {
        // given
        String iamRole = "sample-iam-role";
        AwsConfig awsConfig = AwsConfig.builder()
            .setIamRole(iamRole)
            .build();
        given(awsMetadataApi.credentialsEc2(iamRole)).willReturn(CREDENTIALS);
        given(environment.isRunningOnEcs()).willReturn(false);
        AwsCredentialsProvider credentialsProvider = new AwsCredentialsProvider(awsConfig, awsMetadataApi, environment);

        // when
        AwsCredentials credentials = credentialsProvider.credentials();

        // then
        assertEquals(CREDENTIALS, credentials);
    }