Accessing DAX across AWS accounts
Imagine that you have a DynamoDB Accelerator (DAX) cluster running in one AWS account (account A), and the DAX cluster needs to be accessible from an Amazon Elastic Compute Cloud (Amazon EC2) instance in another AWS account (account B). In this tutorial, you accomplish this by launching an EC2 instance in account B with an IAM role from account B. You then use temporary security credentials from the EC2 instance to assume an IAM role from account A. Finally, you use the temporary security credentials from assuming the IAM role in account A to make application calls over an Amazon VPC peering connection to the DAX cluster in account A. In order to perform these tasks you will need administrative access in both AWS accounts.
Important
It is not possible to have a DAX cluster access a DynamoDB table from a different account.
Set up IAM
-
Create a text file named
AssumeDaxRoleTrust.json
with the following content, which allows Amazon EC2 to work on your behalf.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
-
In account B, create a role that Amazon EC2 can use when launching instances.
aws iam create-role \ --role-name AssumeDaxRole \ --assume-role-policy-document file://AssumeDaxRoleTrust.json
-
Create a text file named
AssumeDaxRolePolicy.json
with the following content, which allows code running on the EC2 instance in account B to assume an IAM role in account A. ReplaceaccountA
with the actual ID of account A.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sts:AssumeRole", "Resource": "arn:aws:iam::
accountA
:role/DaxCrossAccountRole" } ] } -
Add that policy to the role you just created.
aws iam put-role-policy \ --role-name AssumeDaxRole \ --policy-name AssumeDaxRolePolicy \ --policy-document file://AssumeDaxRolePolicy.json
-
Create an instance profile to allow instances to use the role.
aws iam create-instance-profile \ --instance-profile-name AssumeDaxInstanceProfile
-
Associate the role with the instance profile.
aws iam add-role-to-instance-profile \ --instance-profile-name AssumeDaxInstanceProfile \ --role-name AssumeDaxRole
-
Create a text file named
DaxCrossAccountRoleTrust.json
with the following content, which allows account B to assume an account A role. ReplaceaccountB
with the actual ID of account B.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::
accountB
:role/AssumeDaxRole" }, "Action": "sts:AssumeRole" } ] } -
In account A, create the role that account B can assume.
aws iam create-role \ --role-name DaxCrossAccountRole \ --assume-role-policy-document file://DaxCrossAccountRoleTrust.json
-
Create a text file named
DaxCrossAccountPolicy.json
that allows access to the DAX cluster. Replacedax-cluster-arn
with the correct Amazon Resource Name (ARN) of your DAX cluster.{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "dax:GetItem", "dax:BatchGetItem", "dax:Query", "dax:Scan", "dax:PutItem", "dax:UpdateItem", "dax:DeleteItem", "dax:BatchWriteItem", "dax:ConditionCheckItem" ], "Resource": "
dax-cluster-arn
" } ] } -
In account A, add the policy to the role.
aws iam put-role-policy \ --role-name DaxCrossAccountRole \ --policy-name DaxCrossAccountPolicy \ --policy-document file://DaxCrossAccountPolicy.json
Set up a VPC
-
Find the subnet group of account A's DAX cluster. Replace
cluster-name
with the name of the DAX cluster that account B must access.aws dax describe-clusters \ --cluster-name
cluster-name
--query 'Clusters[0].SubnetGroup' -
Using that
subnet-group
, find the cluster's VPC.aws dax describe-subnet-groups \ --subnet-group-name
subnet-group
\ --query 'SubnetGroups[0].VpcId' -
Using that
vpc-id
, find the VPC's CIDR.aws ec2 describe-vpcs \ --vpc
vpc-id
\ --query 'Vpcs[0].CidrBlock' -
From account B, create a VPC using a different, non-overlapping CIDR than the one found in the previous step. Then, create at least one subnet. You can use either the VPC creation wizard in the AWS Management Console or the AWS CLI.
-
From account B, request a peering connection to the account A VPC as described in Creating and accepting a VPC peering connection. From account A, accept the connection.
-
From account B, find the new VPC's routing table. Replace
vpc-id
with the ID of the VPC you created in account B.aws ec2 describe-route-tables \ --filters 'Name=vpc-id,Values=
vpc-id
' \ --query 'RouteTables[0].RouteTableId' -
Add a route to send traffic destined for account A's CIDR to the VPC peering connection. Remember to replace each
user input placeholder
with the correct values for your accounts.aws ec2 create-route \ --route-table-id
accountB-route-table-id
\ --destination-cidraccountA-vpc-cidr
\ --vpc-peering-connection-idpeering-connection-id
-
From account A, find the DAX cluster's route table using the
vpc-id
you found previously.aws ec2 describe-route-tables \ --filters 'Name=vpc-id, Values=
accountA-vpc-id
' \ --query 'RouteTables[0].RouteTableId' -
From account A, add a route to send traffic destined for account B's CIDR to the VPC peering connection. Replace each
user input placeholder
with the correct values for your accounts.aws ec2 create-route \ --route-table-id
accountA-route-table-id
\ --destination-cidraccountB-vpc-cidr
\ --vpc-peering-connection-idpeering-connection-id
-
From account B, launch an EC2 instance in the VPC that you created earlier. Give it the
AssumeDaxInstanceProfile
. You can use either the launch wizard in the AWS Management Console or the AWS CLI. Take note of the instance's security group. -
From account A, find the security group used by the DAX cluster. Remember to replace
cluster-name
with the name of your DAX cluster.aws dax describe-clusters \ --cluster-name
cluster-name
\ --query 'Clusters[0].SecurityGroups[0].SecurityGroupIdentifier' -
Update the DAX cluster's security group to allow inbound traffic from the security group of the EC2 instance you created in account B. Remember to replace the
user input placeholders
with the correct values for your accounts.aws ec2 authorize-security-group-ingress \ --group-id
accountA-security-group-id
\ --protocol tcp \ --port 8111 \ --source-groupaccountB-security-group-id
\ --group-owneraccountB-id
At this point, an application on account B's EC2 instance is able to use the instance
profile to assume the
arn:aws:iam::
role and use the DAX cluster.accountA-id
:role/DaxCrossAccountRole
Modify the DAX client to allow cross-account access
Note
AWS Security Token Service (AWS STS) credentials are temporary credentials. Some clients handle refreshing automatically, while others require additional logic to refresh the credentials. We recommend that you follow the guidance of the appropriate documentation.