diff --git a/example/architecture.png b/example/architecture.png index 34b88e9c..9dc2dca3 100644 Binary files a/example/architecture.png and b/example/architecture.png differ diff --git a/example/functions/api-a.js b/example/functions/api-a.js index 9ec6e718..98caf442 100644 --- a/example/functions/api-a.js +++ b/example/functions/api-a.js @@ -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 @@ -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' }) diff --git a/example/functions/cloudwatchevents.js b/example/functions/cloudwatchevents.js new file mode 100644 index 00000000..d6cbf8b3 --- /dev/null +++ b/example/functions/cloudwatchevents.js @@ -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() +}) diff --git a/example/functions/eventbridge.js b/example/functions/eventbridge.js new file mode 100644 index 00000000..f80a9eef --- /dev/null +++ b/example/functions/eventbridge.js @@ -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() +}) diff --git a/example/package.json b/example/package.json index 8f3b072b..3f681f3b 100644 --- a/example/package.json +++ b/example/package.json @@ -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", diff --git a/example/serverless.yml b/example/serverless.yml index 8f69d8f6..6bebe3b3 100644 --- a/example/serverless.yml +++ b/example/serverless.yml @@ -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 @@ -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 diff --git a/lerna.json b/lerna.json index c21344a6..08e8a6e1 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,8 @@ "lerna": "3.10.8", "packages": [ "layer/*", - "packages/*" + "packages/*", + "example" ], "version": "1.15.6", "command": {