in source/cfn-global-table/cfn-global-table.py [0:0]
def global_table_update(event, context):
logger.debug(json.dumps(event))
global_table_name = event['ResourceProperties']['GlobalTableName']
try:
resp = {'Status': 'FAILED', 'Data': {'GlobalTableName': global_table_name, 'LogGroup': context.log_group_name }}
client = boto3.client('dynamodb')
# Describe the existing global table, as we cannot trust the provided
# event['OldResourceProperties']['ReplicationGroupList']) to show the
# current actual replication group list for a stack update failure / rollback.
response = {}
response = client.describe_global_table(
GlobalTableName=global_table_name
)
logger.debug(response)
existing_replication_groups = set()
for region in response['GlobalTableDescription']['ReplicationGroup']:
existing_replication_groups.add(region['RegionName'])
new_rep_groups = set(event['ResourceProperties']['ReplicationGroupList'])
old_rep_groups = set(event['OldResourceProperties']['ReplicationGroupList'])
changeset_rep_group_discrepancies = old_rep_groups - existing_replication_groups
if len(changeset_rep_group_discrepancies):
logger.info("Using the results of describe_global_table rather than the value from OldResourceProperties in the event")
# Use reality, not the value from OldResourceProperties in the event
old_rep_groups = existing_replication_groups
if new_rep_groups == old_rep_groups:
logger.info('No replication group changes detected')
resp['Reason'] = 'No replication group changes detected'
resp['Status'] = 'SUCCESS'
return resp
else:
logger.info('Replication group changes detected. Updating Global Table')
AddReplicationGroups = new_rep_groups - old_rep_groups
RemoveReplicationGroups = old_rep_groups - new_rep_groups
if len(AddReplicationGroups):
for region in AddReplicationGroups:
update_global_table(global_table_name, region, 'Create')
logger.info('Add replication group region: ' + region)
if len(RemoveReplicationGroups):
for region in RemoveReplicationGroups:
update_global_table(global_table_name, region, 'Delete')
logger.info('Remove replication group region: ' + region)
resp['Reason'] = 'Updated Global Table: ' + global_table_name
resp['Status'] = 'SUCCESS'
except Exception as e:
logger.error('Failed to update DynamoDB Global Table: ' + global_table_name)
resp['Reason'] = 'Failed to update Global Table: ' + e.message
resp['Status'] = 'FAILED'
return resp