Skip to content

Commit

Permalink
fix(dynamodb): stacks created by GlobalTable correctly inherit their …
Browse files Browse the repository at this point in the history
…account. (#5202)

If an account was specified on the main stack,
the child stacks created by GlobalTable were env-agnostic,
which means it wasn't possible to have a reference between a global table instance,
and anything in the main stack, even if they were in the same region.

Fixes #4882
  • Loading branch information
skinny85 authored and mergify[bot] committed Nov 27, 2019
1 parent 3a7975c commit 5ad5407
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ export class GlobalTable extends cdk.Construct {

this.lambdaGlobalTableCoordinator = new GlobalTableCoordinator(scope, id + "-CustomResource", props);

const scopeStack = cdk.Stack.of(scope);
// here we loop through the configured regions.
// in each region we'll deploy a separate stack with a DynamoDB Table with identical properties in the individual stacks
for (const reg of props.regions) {
const regionalStack = new cdk.Stack(this, id + "-" + reg, { env: { region: reg } });
const regionalTable = new dynamodb.Table(regionalStack, `${id}-GlobalTable-${reg}`, regionalTableProps);
for (const region of props.regions) {
const regionalStack = new cdk.Stack(this, id + "-" + region, { env: { region, account: scopeStack.account } });
const regionalTable = new dynamodb.Table(regionalStack, `${id}-GlobalTable-${region}`, regionalTableProps);
this._regionalTables.push(regionalTable);

// deploy the regional stack before the Lambda coordinator stack
Expand Down
55 changes: 39 additions & 16 deletions packages/@aws-cdk/aws-dynamodb-global/test/test.dynamodb.global.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { Attribute, AttributeType, StreamViewType, Table } from '@aws-cdk/aws-dynamodb';
import { Stack } from '@aws-cdk/core';
import * as assert from 'assert';
import { App, CfnOutput, Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import {
GlobalTable,
GlobalTableProps
} from '../lib';
import { GlobalTable, GlobalTableProps } from '../lib';

// tslint:disable:object-literal-key-quotes

Expand Down Expand Up @@ -64,26 +60,53 @@ export = {
test.done();
},
},

'GlobalTable generated stacks inherit their account from the parent stack'(test: Test) {
const app = new App();
const stack = new Stack(app, 'GlobalTableStack', { env: { account: '123456789012', region: 'us-east-1' } });

const globalTable = new GlobalTable(stack, CONSTRUCT_NAME, {
tableName: TABLE_NAME,
partitionKey: TABLE_PARTITION_KEY,
regions: ['us-east-1', 'us-west-2'],
stream: StreamViewType.NEW_AND_OLD_IMAGES,
});

new CfnOutput(stack, 'DynamoDbOutput', {
// this works, because both `stack` and `regionTables[0]` stack are in the same account & region
value: globalTable.regionalTables[0].tableStreamArn!,
});

expect(stack).toMatch({
"Outputs": {
"DynamoDbOutput": {
"Value": {
"Fn::ImportValue": "GlobalTableStackawscdkdynamodbglobalawscdkdynamodbglobaluseast19C1C8A14:awscdkdynamodbglobalawscdkdynamodbglobaluseast1ExportsOutputFnGetAttawscdkdynamodbglobalGlobalTableuseast1FC03DD69StreamArn28E90DB8",
},
},
},
});

test.done();
},

'Enforce StreamSpecification': {
'global dynamo should only allow NEW_AND_OLD_IMAGES'(test: Test) {
const stack = new Stack();
try {

test.throws(() => {
new GlobalTable(stack, CONSTRUCT_NAME, {
tableName: TABLE_NAME,
stream: StreamViewType.KEYS_ONLY,
partitionKey: TABLE_PARTITION_KEY,
regions: [ 'us-east-1', 'us-east-2', 'us-west-2' ]
});
// We are expecting the above line to throw a TypeError since
// the streamSpecification is wrong. Force a failure on this
// line if we get there.
expect(stack).to(haveResource('Fail::this::test::IfWeGetThisFar', {}));
} catch ( TypeError ) {
expect(stack);
}
}, /dynamoProps.stream MUST be set to dynamodb.StreamViewType.NEW_AND_OLD_IMAGES/);

test.done();
},
},

'Check getting tables': {
'global dynamo should only allow NEW_AND_OLD_IMAGES'(test: Test) {
const stack = new Stack();
Expand All @@ -92,9 +115,9 @@ export = {
partitionKey: TABLE_PARTITION_KEY,
regions: [ 'us-east-1', 'us-east-2', 'us-west-2' ]
});
assert(regTables.regionalTables.length === 3);
test.equal(regTables.regionalTables.length, 3);
for (const table of regTables.regionalTables) {
assert(table instanceof Table);
test.ok(table instanceof Table);
}
test.done();
},
Expand Down

0 comments on commit 5ad5407

Please sign in to comment.