diff --git a/lib/plugins/aws/deploy/compile/events/dynamodb/index.js b/lib/plugins/aws/deploy/compile/events/dynamodb/index.js index 37b8b3e05..268d25bf4 100644 --- a/lib/plugins/aws/deploy/compile/events/dynamodb/index.js +++ b/lib/plugins/aws/deploy/compile/events/dynamodb/index.js @@ -15,12 +15,10 @@ class AwsCompileDynamoDbEvents { compileDynamoDbEvents() { this.serverless.service.getAllFunctions().forEach((functionName) => { const functionObj = this.serverless.service.getFunction(functionName); - let dynamoDbNumberInFunction = 0; if (functionObj.events) { functionObj.events.forEach(event => { if (event.dynamodb) { - dynamoDbNumberInFunction++; let EventSourceArn; let BatchSize = 10; let StartingPosition = 'TRIM_HORIZON'; @@ -99,9 +97,13 @@ class AwsCompileDynamoDbEvents { .PolicyDocument .Statement = statement.concat([dynamoDbStatement]); + let tableName = EventSourceArn.split('/')[1]; + // normalize the tableName + tableName = tableName[0].toUpperCase() + tableName.substr(1); + const newDynamoDbObject = { - [`${normalizedFunctionName}EventSourceMappingDynamoDb${ - dynamoDbNumberInFunction}`]: JSON.parse(dynamoDbTemplate), + [`${normalizedFunctionName}EventSourceMappingDynamoDbTable${ + tableName}`]: JSON.parse(dynamoDbTemplate), }; _.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources, diff --git a/lib/plugins/aws/deploy/compile/events/dynamodb/tests/index.js b/lib/plugins/aws/deploy/compile/events/dynamodb/tests/index.js index 16ddff518..663a47a4f 100644 --- a/lib/plugins/aws/deploy/compile/events/dynamodb/tests/index.js +++ b/lib/plugins/aws/deploy/compile/events/dynamodb/tests/index.js @@ -67,18 +67,18 @@ describe('AwsCompileDynamoDbEvents', () => { events: [ { dynamodb: { - streamArn: 'stream:arn:one', + streamArn: 'arn:aws:dynamodb:region:account:table/foo/stream/1', batchSize: 1, startingPosition: 'STARTING_POSITION_ONE', }, }, { dynamodb: { - streamArn: 'stream:arn:two', + streamArn: 'arn:aws:dynamodb:region:account:table/bar/stream/2', }, }, { - dynamodb: 'stream:arn:three', + dynamodb: 'arn:aws:dynamodb:region:account:table/baz/stream/3', }, ], }, @@ -88,25 +88,25 @@ describe('AwsCompileDynamoDbEvents', () => { // event 1 expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb1 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableFoo .Type ).to.equal('AWS::Lambda::EventSourceMapping'); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb1 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableFoo .Properties.EventSourceArn ).to.equal( awsCompileDynamoDbEvents.serverless.service.functions.first.events[0] .dynamodb.streamArn ); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb1 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableFoo .Properties.BatchSize ).to.equal( awsCompileDynamoDbEvents.serverless.service.functions.first.events[0] .dynamodb.batchSize ); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb1 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableFoo .Properties.StartingPosition ).to.equal( awsCompileDynamoDbEvents.serverless.service.functions.first.events[0] @@ -115,43 +115,43 @@ describe('AwsCompileDynamoDbEvents', () => { // event 2 expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb2 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBar .Type ).to.equal('AWS::Lambda::EventSourceMapping'); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb2 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBar .Properties.EventSourceArn ).to.equal( awsCompileDynamoDbEvents.serverless.service.functions.first.events[1] .dynamodb.streamArn ); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb2 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBar .Properties.BatchSize ).to.equal(10); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb2 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBar .Properties.StartingPosition ).to.equal('TRIM_HORIZON'); // event 3 expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb3 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBaz .Type ).to.equal('AWS::Lambda::EventSourceMapping'); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb3 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBaz .Properties.EventSourceArn ).to.equal( awsCompileDynamoDbEvents.serverless.service.functions.first.events[2] .dynamodb ); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb3 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBaz .Properties.BatchSize ).to.equal(10); expect(awsCompileDynamoDbEvents.serverless.service - .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDb3 + .provider.compiledCloudFormationTemplate.Resources.FirstEventSourceMappingDynamoDbTableBaz .Properties.StartingPosition ).to.equal('TRIM_HORIZON'); }); @@ -161,7 +161,7 @@ describe('AwsCompileDynamoDbEvents', () => { first: { events: [ { - dynamodb: 'stream:arn:one', + dynamodb: 'arn:aws:dynamodb:region:account:table/foo/stream/1', }, ], }, @@ -176,7 +176,7 @@ describe('AwsCompileDynamoDbEvents', () => { 'dynamodb:DescribeStream', 'dynamodb:ListStreams', ], - Resource: 'stream:arn:one', + Resource: 'arn:aws:dynamodb:region:account:table/foo/stream/1', }, ]; @@ -203,11 +203,6 @@ describe('AwsCompileDynamoDbEvents', () => { Object.keys(awsCompileDynamoDbEvents.serverless.service.provider .compiledCloudFormationTemplate.Resources).length ).to.equal(1); - - expect( - awsCompileDynamoDbEvents.serverless.service.provider.compiledCloudFormationTemplate - .Resources - ).to.not.include.keys('FirstEventSourceMappingDynamoDb1'); }); it('should not add the IAM role statements when dynamodb events are not given', () => {