Skip to content

Commit

Permalink
docs: updated example with eventbridge & cw events
Browse files Browse the repository at this point in the history
  • Loading branch information
theburningmonk committed Jan 1, 2020
1 parent 7229d04 commit a2f6de3
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 3 deletions.
Binary file modified example/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 34 additions & 2 deletions example/functions/api-a.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const Datadog = require('@dazn/datadog-metrics')
const HTTP = require('@dazn/lambda-powertools-http-client')
const Kinesis = require('@dazn/lambda-powertools-kinesis-client')
const Firehose = require('@dazn/lambda-powertools-firehose-client')
const apiGateway = require('@dazn/lambda-powertools-pattern-basic')
const EventBridge = require('@dazn/lambda-powertools-eventbridge-client')
const CloudWatchEvents = require('@dazn/lambda-powertools-cloudwatchevents-client')
const wrap = require('@dazn/lambda-powertools-pattern-basic')
const uuid = require('uuid/v4')

const { FIREHOSE_STREAM, KINESIS_STREAM } = process.env

module.exports.handler = apiGateway(async (event, context) => {
module.exports.handler = wrap(async (event, context) => {
Datadog.gauge('api-a', 1)
const host = event.headers.Host

Expand Down Expand Up @@ -58,6 +60,36 @@ module.exports.handler = apiGateway(async (event, context) => {
Log.error('failed to put record to Firehose', { streamName: FIREHOSE_STREAM }, err)
}

try {
await Datadog.trackExecTime(
() => EventBridge.putEvents({
Entries: [{
Source: 'dazn-lambda-powertools-example',
'Detail-Type': 'eventbridge',
Detail: JSON.stringify({ message: 'hello eventbridge' })
}]
}).promise(),
'EventBridge.putEvents'
)
} catch (err) {
Log.error('failed to put events to EventBridge', err)
}

try {
await Datadog.trackExecTime(
() => CloudWatchEvents.putEvents({
Entries: [{
Source: 'dazn-lambda-powertools-example',
'Detail-Type': 'cloudwatchevents',
Detail: JSON.stringify({ message: 'hello cloudwatchevents' })
}]
}).promise(),
'CloudWatchEvents.putEvents'
)
} catch (err) {
Log.error('failed to put events to CloudWatchEvents', err)
}

return {
statusCode: 200,
body: JSON.stringify({ message: 'all done' })
Expand Down
17 changes: 17 additions & 0 deletions example/functions/cloudwatchevents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const SNS = require('@dazn/lambda-powertools-sns-client')
const wrap = require('@dazn/lambda-powertools-pattern-basic')
const Log = require('@dazn/lambda-powertools-logger')
const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')

module.exports.handler = wrap(async (event, context) => {
console.log(JSON.stringify(event))

CorrelationIds.set('sns-sender', 'cloudwatchevents')
Log.debug('publishing cloudwatchevents event as SNS message...', { event })

const req = {
Message: JSON.stringify(event),
TopicArn: process.env.TOPIC_ARN
}
return SNS.publish(req).promise()
})
17 changes: 17 additions & 0 deletions example/functions/eventbridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const SNS = require('@dazn/lambda-powertools-sns-client')
const wrap = require('@dazn/lambda-powertools-pattern-basic')
const Log = require('@dazn/lambda-powertools-logger')
const CorrelationIds = require('@dazn/lambda-powertools-correlation-ids')

module.exports.handler = wrap(async (event, context) => {
console.log(JSON.stringify(event))

CorrelationIds.set('sns-sender', 'eventbridge')
Log.debug('publishing eventbridge event as SNS message...', { event })

const req = {
Message: JSON.stringify(event),
TopicArn: process.env.TOPIC_ARN
}
return SNS.publish(req).promise()
})
2 changes: 2 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"author": "Yan Cui",
"dependencies": {
"@dazn/datadog-metrics": "^1.3.1",
"@dazn/lambda-powertools-cloudwatchevents-client": "^1.15.6",
"@dazn/lambda-powertools-correlation-ids": "^1.8.2",
"@dazn/lambda-powertools-dynamodb-client": "^1.9.0",
"@dazn/lambda-powertools-eventbridge-client": "^1.15.6",
"@dazn/lambda-powertools-firehose-client": "^1.8.0",
"@dazn/lambda-powertools-http-client": "^1.8.2",
"@dazn/lambda-powertools-kinesis-client": "^1.9.0",
Expand Down
37 changes: 37 additions & 0 deletions example/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ custom:
pseudoParameters:
allowReferences: true

package:
exclude:
- node_modules/aws-sdk/**
- node_modules/@dazn/**/node_modules/aws-sdk/**

functions:
api-a:
handler: functions/api-a.handler
Expand Down Expand Up @@ -98,6 +103,38 @@ functions:
Action: sns:Publish
Resource: !Ref SnsTopic

eventbridge:
handler: functions/eventbridge.handler
events:
- eventBridge:
pattern:
source:
- dazn-lambda-powertools-example
detail-type:
- eventbridge
environment:
TOPIC_ARN: !Ref SnsTopic
iamRoleStatements:
- Effect: Allow
Action: sns:Publish
Resource: !Ref SnsTopic

cloudwatchevents:
handler: functions/cloudwatchevents.handler
events:
- cloudwatchEvent:
event:
source:
- dazn-lambda-powertools-example
detail-type:
- cloudwatchevents
environment:
TOPIC_ARN: !Ref SnsTopic
iamRoleStatements:
- Effect: Allow
Action: sns:Publish
Resource: !Ref SnsTopic

stand-alone:
handler: functions/stand-alone.handler

Expand Down
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"lerna": "3.10.8",
"packages": [
"layer/*",
"packages/*"
"packages/*",
"example"
],
"version": "1.15.6",
"command": {
Expand Down

0 comments on commit a2f6de3

Please sign in to comment.