Using AWS SDK V2 for Java to read DynamoDB cross-account 
Tags: aws, java, dynamodb, cloudcomputing
Published: 2021-12-24 03:03:15

For medium and large size companies, is usual to have multiple AWS accounts grouped by functionality, environment, business goals, etc.
Sometimes, when is required cross-account access for resources, could be a problem due to the number of steps involved to grant access. And in the case of DynamoDB, additionally steps appears (different from services like KMS where you configure cross-account access and it is enough, for services like DynamoDB it is needed first assume the role and then procede with API calls).

In this sample, I will describe step by step how to configure permissions to allow DynamoDB cross account access and test it using AWS SDK V2 for Java deployed in EC2 (however, the same concept could be applied to AWS Lambda).

**Scenario**
Access from an Java Application on Account "A" to and DynamoDB Table on Account "B".

**Diagram of resources to be created**

![Solution Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vx8ogqvc26lvhe8ysbq.jpg)


**Considerations**

- Account owner of DynamoDB Tables will be referenced as "Account A".
- Account in which Java Application will be deployed and need access to Tables from "Account A", will be reference as "Account B".
- Complete Java Code can be found on this [repository](https://github.com/omarrosadio/aws-java-sdk-v2-dynamo-cross-account.git). It uses maven as dependency manager and AWS SDK V2 for Java.

**Steps**

## 1. Create Role on Account A (DynamoDB resources owner)

On account A, create a Role and choose "Another AWS Account" as type of trusted entity. Enter the Account ID from Account B:

![Role Creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgqm265mlqc2bp7q1iaj.png)

For permissions policies, I am attaching "AmazonDynamoDBReadOnlyAccess" managed policy only for demo purposes, but on real usage it should follow the Principle of Least Privilege to limit access.
![Policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89lgyqjo9888nfq7ve73.png)

Create the role, choose a suitable role name:
![Role name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gex0tfs91t43wrjv99mu.png)

Copy the Role ARN because it will be used in the next step:
`arn:aws:iam::<ID_Account_B>:role/CrossAccountReadDynamo`

## 2. Create policy on Account B with permissions to assume role created on step 1
On Account B, create a Policy to grant permissions to assume the role from Account A. For policy JSON use:
`{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "arn:aws:iam::<ID_Account_A>:role/CrossAccountReadDynamo"
    }
}`

And choose a name for the policy:
`assume_role_account_A`

![Policy name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6vqcygmp88082ry0d3i.png)

## 3. Create role on Account B and attach the policy created on step 2
On Account B, create a role and select EC2 as type of trusted entity:
![EC2 role](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtfetbzxbc52emjkk5y0.png)

Attach the policy recently created:
![Policy attachment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dq01illdvoaqfi5dmjuw.png)

Name: EC2_AssumeRole_Account_A
![Role name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u2dkfqwgv1itfhhowbq4.png)

## 4. Test cross account access through Java Application
The Java Application will be deployed on and EC2 Instance in Account B. This EC2 Instance needs to has attached the Role created previously.
The first step on code requires make an API Call to the Security Token Service (AWS STS) and in this way generate temporary credentials to be able to assume the role from Account A.
Modify the variables accordingly:
- roleARN: substitute with Role ARN created on step 1
- roleSessionName: an identifier to the temporary session
- region: region where the DynamoDB resources are deployed

`/*Change this variables according to the role created*/
		String roleARN = "arn:aws:iam::<ACCOUNT_A>:role/<ROLE_NAME>";
		String roleSessionName = "DynamoCrossAccount";
		Region region = Region.US_EAST_1;`

Once the temporary credentials are generated, is needed to set the AccessKey, SecretAccessKey and SessionToken:
`AwsSessionCredentials awsCreds = AwsSessionCredentials.create(myCreds.accessKeyId(), myCreds.secretAccessKey(),
				myCreds.sessionToken());`

With the new credentials configured, the subsequents API Calls will be used the role from Account A. So, now we can consume and do operations over DynamoDB Tables of Account A. To test it, I will list all the existing tables:

```
ListTablesResponse response = null;
				if (lastName == null) {
					ListTablesRequest request = ListTablesRequest.builder().limit(10).build();

					response = ddb.listTables(request);
				} else {
					ListTablesRequest request = ListTablesRequest.builder().exclusiveStartTableName(lastName).build();
					response = ddb.listTables(request);
				}

				List<String> tableNames = response.tableNames();

				if (tableNames.size() > 0) {
					for (String curName : tableNames) {
						System.out.format("* %s\n", curName);
					}
				} else {
					System.out.println("No tables found!");
					System.exit(0);
				}

				lastName = response.lastEvaluatedTableName();
				if (lastName == null) {
					moreTables = false;
				}

```


And the expected output should list resources from the other account. Achieving cross-account access:
![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pt5ze6a020hd346kghal.png)


Again, the complete code is on GitHub and it is based on Official AWS documentation, specifically:

- https://docs.aws.amazon.com/code-samples/latest/catalog/javav2-sts-src-main-java-com-example-sts-AssumeRole.java.html
- https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-dynamodb-tables.html