Last active
November 9, 2016 20:19
-
-
Save openback/bd94538b8a203253ffe4f6ada3a4ebe2 to your computer and use it in GitHub Desktop.
Serverless 1.1.0 Dynamo Stream attachment (Until getting outputs within serverless.yml works)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jshint node: true, esversion: 6 | |
'use strict'; | |
const OUTPUT_NAME = 'MyTableStreamArn'; | |
const FUNCTION_NAME = 'FUNCTIONNAMETOATTACHSTREAMTO'; | |
class Deploy { | |
constructor(serverless, options) { | |
this.provider = 'aws'; | |
this.serverless = serverless; | |
this.options = options; | |
let AWS = this.serverless.getProvider('aws').sdk; | |
this.cloudformation = new AWS.CloudFormation({region: this.options.region}); | |
this.lambda = new AWS.Lambda({region: this.options.region}); | |
this.hooks = { | |
'after:deploy:deploy': this.afterDeploy.bind(this), | |
}; | |
} | |
afterDeploy() { | |
let stackName = `${this.serverless.service.service}-${this.options.stage}`; | |
let lambdaName = `${stackName}-${FUNCTION_NAME}`; | |
this.cloudformation.describeStacks({StackName: stackName}, (err, data) => { | |
if (err) { | |
throw Error('Could not find the stack to retrieve the Dynamo Stream' + err); | |
} | |
let output = data.Stacks[0].Outputs.find((output) => { | |
return output.OutputKey === OUTPUT_NAME; | |
}); | |
if (output === undefined) { | |
throw Error(`Could not find the ${OUTPUT_NAME} output`); | |
} | |
let eventSourceArn = output.OutputValue; | |
console.log('Checking for existence of Event Source Mapping...'); | |
this.lambda.listEventSourceMappings( | |
{ | |
EventSourceArn: eventSourceArn, | |
FunctionName: lambdaName, | |
MaxItems: 1 | |
}, | |
(err, data) => { | |
if (err) { | |
throw Error('Could not listEventSourceMappings', err); | |
} | |
if (data.EventSourceMappings.length == 1) { | |
console.log('Event Source already set.'); | |
return; | |
} | |
console.log('Assigning Stream Arn: ', eventSourceArn); | |
this.lambda.createEventSourceMapping({ | |
EventSourceArn: eventSourceArn, | |
FunctionName: lambdaName, | |
StartingPosition: 'LATEST', | |
BatchSize: 1, | |
Enabled: true | |
}, (err, data) => { | |
if (err) { | |
throw Error('Could not createEventSourceMappings', err); | |
} | |
console.log('Done.'); | |
}); | |
} | |
); | |
}); | |
} | |
} | |
module.exports = Deploy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
resources: | |
Resources: | |
MyTable: | |
Type: AWS::DynamoDB::Table | |
# ... | |
StreamSpecification: | |
StreamViewType: NEW_AND_OLD_IMAGES | |
Outputs: | |
MyTableStreamArn: | |
Description: Arn of the DynamoDB Table Stream | |
Value: | |
Fn::GetAtt: | |
- MyTable | |
- StreamArn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment