AWS SDK 2 assume role
Asked Answered
F

1

6
@Bean
public DynamoDbClient amazonDynamoDB() {
    final AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder().roleSessionName(UUID.randomUUID().toString()).roleArn("roleArn").build();
    final StsAssumeRoleCredentialsProvider  stsAssumeRoleCredentialsProvider = StsAssumeRoleCredentialsProvider.builder().refreshRequest(assumeRoleRequest).build();
    return DynamoDbClient.builder().credentialsProvider(stsAssumeRoleCredentialsProvider).region(Region.EU_WEST_1)
            .build();
}

getting error

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [software.amazon.awssdk.services.dynamodb.DynamoDbClient]: Factory method 'amazonDynamoDB' threw exception; nested exception is java.lang.NullPointerException: STS client must not be null. at [email protected]/org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at [email protected]/org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ... 58 common frames omitted Caused by: java.lang.NullPointerException: STS client must not be null.

Fotinas answered 15/7, 2020 at 18:31 Comment(2)
Looks like you're passing a NULL value in one of the fields. You should debug it by checking the values of whatever you are passing on the line that generated the error.Bourgeon
It worked actually it's bit confusing but finally figured it out .. we have to setup sts client in request. @John RotensteinFotinas
F
14

In assume role request need to set sts client & it worked

@Bean
@Primary
public DynamoDbClient amazonDynamoDB() {
    final AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder().roleSessionName(UUID.randomUUID().toString()).roleArn("roleArn").build();
    final StsClient stsClient = StsClient.builder().region(Region.EU_WEST_1).build();
    final StsAssumeRoleCredentialsProvider  stsAssumeRoleCredentialsProvider = StsAssumeRoleCredentialsProvider.builder().stsClient(stsClient).refreshRequest(assumeRoleRequest).build();
    return DynamoDbClient.builder().credentialsProvider(stsAssumeRoleCredentialsProvider).region(Region.EU_WEST_1)
            .build();
}
Fotinas answered 16/7, 2020 at 8:36 Comment(2)
This was a requirement of the v1 SDK, and I was hoping that it was no longer required in the v2 SDK because it's not documented. +1 for doing the debugging.Adonai
but there is no stsClient method mentioned in the documentatiion for StsAssumeRoleCredentialsProvider.BuilderOrdinary

© 2022 - 2024 — McMap. All rights reserved.