Skip to content

Instantly share code, notes, and snippets.

@posilva
Created November 13, 2019 11:45
Show Gist options
  • Save posilva/0343ac6a9a102da0e874d1ea18519a22 to your computer and use it in GitHub Desktop.
Save posilva/0343ac6a9a102da0e874d1ea18519a22 to your computer and use it in GitHub Desktop.
Try Dax
#!/bin/bash
pip install amazon-dax-client
#!/bin/bash
# In an Account B Ec2 instance do the following
./install.sh
# use credentials from DaxUserAccountA created in AccountA
export AWS_ACCESS_KEY_ID=AKIA000000000000
export AWS_SECRET_ACCESS_KEY=dasdasdsadsdasdsadsadsadas
export AWS_DEFAULT_REGION=eu-central-1
python ./test_dax.py clusterendpoint.somevalue.clustercfg.dax.euc1.cache.amazonaws.com:8111
#!/bin/bash
REGION=eu-central-1
ACCOUNT_A_PROFILE=account-a-profile
ACCOUNT_B_PROFILE=account-b-profile
ACCOUNT_A_USER=DaxUserAccountA
ACCOUNT_B_USER=DaxUserAccountB
ACCOUNT_A_ID=666666666666
ACCOUNT_B_ID=999999999999
CLUSTER_NAME=DaxClusterAccountB
TABLE_NAME=DaxDDBTableAccountB
DAX_ROLE='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"dax.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
DAX_ROLE_POLICY='{"Version":"2012-10-17","Statement":[{"Action":["dynamodb:*"],"Effect":"Allow","Resource":["arn:aws:dynamodb:'${REGION}':'${ACCOUNT_B_ID}':*"]}]}'
aws iam --region ${REGION} --profile $ACCOUNT_A_PROFILE create-user --user-name ${ACCOUNT_A_USER}
aws iam --region ${REGION} --profile $ACCOUNT_A_PROFILE create-access-key --user-name ${ACCOUNT_A_USER} > ${ACCOUNT_A_USER}.credentials
aws iam --region ${REGION} --profile $ACCOUNT_A_PROFILE attach-user-policy --policy-arn "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess" --user-name ${ACCOUNT_A_USER}
aws iam --region ${REGION} --profile $ACCOUNT_B_PROFILE create-user --user-name ${ACCOUNT_B_USER}
aws iam --region ${REGION} --profile $ACCOUNT_B_PROFILE create-access-key --user-name ${ACCOUNT_B_USER} > ${ACCOUNT_B_USER}.credentials
aws iam --region ${REGION} --profile $ACCOUNT_B_PROFILE attach-user-policy --policy-arn "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess" --user-name ${ACCOUNT_B_USER}
aws iam --region ${REGION} --profile $ACCOUNT_B_PROFILE create-role --role-name DAXServiceRoleForDynamoDBAccess --assume-role-policy-document ${DAX_ROLE}
aws iam --region ${REGION} --profile $ACCOUNT_B_PROFILE create-policy --policy-name DAXServicePolicyForDynamoDBAccess --policy-document ${DAX_ROLE_POLICY}
aws iam --region ${REGION} --profile $ACCOUNT_B_PROFILE attach-role-policy --role-name DAXServiceRoleForDynamoDBAccess --policy-arn "arn:aws:iam::${ACCOUNT_B_ID}:policy/DAXServicePolicyForDynamoDBAccess"
aws dax --region ${REGION} --profile $ACCOUNT_B_PROFILE create-cluster --cluster-name ${CLUSTER_NAME} --node-type dax.t2.small --replication-factor 3 --iam-role-arn "arn:aws:iam::${ACCOUNT_B_ID}:role/DAXServiceRoleForDynamoDBAccess"
aws dynamodb --region ${REGION} --profile $ACCOUNT_B_PROFILE create-table --table-name ${TABLE_NAME} --attribute-definitions AttributeName=pk,AttributeType=S AttributeName=sk,AttributeType=S --key-schema AttributeName=pk,KeyType=HASH AttributeName=sk,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
#
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
#!/usr/bin/env python
from __future__ import print_function
import os, sys, time
import amazondax
import botocore.session
region = os.environ.get('AWS_DEFAULT_REGION', 'eu-central-1')
session = botocore.session.get_session()
dynamodb = session.create_client('dynamodb', region_name=region) # low-level client
table_name = "DaxDDBTableAccountB"
if len(sys.argv) > 1:
endpoint = sys.argv[1]
dax = amazondax.AmazonDaxClient(session, region_name=region, endpoints=[endpoint])
client = dax
else:
client = dynamodb
pk = 10
sk = 10
iterations = 50
# Write all keys
for i in range(iterations):
for ipk in range(1, pk+1):
for isk in range(1, sk+1):
params = {
'TableName': table_name,
'Item': {
"pk": {'S': str(ipk)},
"sk": {'S': str(isk)}
}
}
client.put_item(**params)
start = time.time()
# Read Keys
for i in range(iterations):
for ipk in range(1, pk+1):
for isk in range(1, sk+1):
params = {
'TableName': table_name,
'Key': {
"pk": {'S': str(ipk)},
"sk": {'S': str(isk)}
}
}
result = client.get_item(**params)
print('.', end='', file=sys.stdout); sys.stdout.flush()
print()
end = time.time()
print('Total time: {} sec - Avg time: {} sec'.format(end - start, (end-start)/iterations))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment