I need to fetch data from DynamoDB tables with Spark using Java. It works fine with user’s access key and secret key:
final JobConf jobConf = new JobConf(sc.hadoopConfiguration());
jobConf.set("dynamodb.servicename", "dynamodb");
jobConf.set("dynamodb.input.tableName", tableName);
jobConf.set("mapred.output.format.class", "org.apache.hadoop.dynamodb.write.DynamoDBOutputFormat");
jobConf.set("mapred.input.format.class", "org.apache.hadoop.dynamodb.read.DynamoDBInputFormat");
jobConf.set("dynamodb.awsAccessKeyId", accessKey);
jobConf.set("dynamodb.awsSecretAccessKey", secretKey);
jobConf.set("dynamodb.endpoint", endpoint);
I need to use AWS assumed role and STS (at least by security reasons) for fetching data from DynamoDB exactly with spark. Is it possible? I found that it possible to use assumed role to access AWS S3 with spark (https://issues.apache.org/jira/browse/HADOOP-12537, https://hadoop.apache.org/docs/current3/hadoop-aws/tools/hadoop-aws/index.html), but haven’t found similar idea for DynamoDB.
For receiving STS temporary credentials I use the following code:
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.defaultClient();
AssumeRoleRequest assumeRequest = new AssumeRoleRequest()
.withRoleArn(roleArn) // arn:aws:iam::XXXXXXX:role/assume-role-DynamoDB-ReadOnly
.withDurationSeconds(3600)
.withRoleSessionName("assumed-role-session");
AssumeRoleResult assumeResult = stsClient.assumeRole(assumeRequest);
Credentials credentials = assumeResult.getCredentials();
Invoking credentials.getAccessKeyId(), credentials.getSecretAccessKey() and credentials.getSessionToken()
return generated temporary credentials. With these credentials I successfully could take data from DynamoDB using java aws sdk AmazonDynamoDBClient (non-spark approach).
Is it possible with spark? Does spark allow to use something like the following:
jobConf.set("dynamodb.awsSessionToken”, sessionToken)
?
jobConf.set("dynamodb.awsAccessKeyId", accessKey); jobConf.set("dynamodb.awsSecretAccessKey", secretKey);
– Vasyl Sarzhynskyi