diff --git a/packages/@aws-cdk/aws-iotevents/README.md b/packages/@aws-cdk/aws-iotevents/README.md index fe071d7baecc6..760b6098d9d41 100644 --- a/packages/@aws-cdk/aws-iotevents/README.md +++ b/packages/@aws-cdk/aws-iotevents/README.md @@ -51,7 +51,7 @@ import * as iotevents from '@aws-cdk/aws-iotevents'; const input = new iotevents.Input(this, 'MyInput', { inputName: 'my_input', // optional - attributeJsonPaths: ['payload.temperature'], + attributeJsonPaths: ['payload.deviceId', 'payload.temperature'], }); const onlineState = new iotevents.State({ @@ -64,6 +64,9 @@ const onlineState = new iotevents.State({ new iotevents.DetectorModel(this, 'MyDetectorModel', { detectorModelName: 'test-detector-model', // optional + description: 'test-detector-model-description', // optional property, default is none + evaluationMethod: iotevents.EventEvaluation.SERIAL, // optional property, default is iotevents.EventEvaluation.BATCH + detectorKey: 'payload.deviceId', // optional property, default is none and single detector instance will be created and all inputs will be routed to it initialState: onlineState, }); ``` diff --git a/packages/@aws-cdk/aws-iotevents/lib/detector-model.ts b/packages/@aws-cdk/aws-iotevents/lib/detector-model.ts index 2a5d270fb0cde..5ef50fd871d75 100644 --- a/packages/@aws-cdk/aws-iotevents/lib/detector-model.ts +++ b/packages/@aws-cdk/aws-iotevents/lib/detector-model.ts @@ -16,6 +16,22 @@ export interface IDetectorModel extends IResource { readonly detectorModelName: string; } +/** + * Information about the order in which events are evaluated and how actions are executed. + */ +export enum EventEvaluation { + /** + * When setting to SERIAL, variables are updated and event conditions are evaluated in the order + * that the events are defined. + */ + BATCH = 'BATCH', + /** + * When setting to BATCH, variables within a state are updated and events within a state are + * performed only after all event conditions are evaluated. + */ + SERIAL = 'SERIAL', +} + /** * Properties for defining an AWS IoT Events detector model */ @@ -27,6 +43,38 @@ export interface DetectorModelProps { */ readonly detectorModelName?: string; + /** + * A brief description of the detector model. + * + * @default none + */ + readonly description?: string; + + /** + * Information about the order in which events are evaluated and how actions are executed. + * + * When setting to SERIAL, variables are updated and event conditions are evaluated in the order + * that the events are defined. + * When setting to BATCH, variables within a state are updated and events within a state are + * performed only after all event conditions are evaluated. + * + * @default EventEvaluation.BATCH + */ + readonly evaluationMethod?: EventEvaluation; + + /** + * The value used to identify a detector instance. When a device or system sends input, a new + * detector instance with a unique key value is created. AWS IoT Events can continue to route + * input to its corresponding detector instance based on this identifying information. + * + * This parameter uses a JSON-path expression to select the attribute-value pair in the message + * payload that is used for identification. To route the message to the correct detector instance, + * the device must send a message payload that contains the same attribute-value. + * + * @default - none (single detector instance will be created and all inputs will be routed to it) + */ + readonly detectorKey?: string; + /** * The state that is entered at the creation of each detector. */ @@ -70,6 +118,9 @@ export class DetectorModel extends Resource implements IDetectorModel { const resource = new CfnDetectorModel(this, 'Resource', { detectorModelName: this.physicalName, + detectorModelDescription: props.description, + evaluationMethod: props.evaluationMethod, + key: props.detectorKey, detectorModelDefinition: { initialStateName: props.initialState.stateName, states: [props.initialState._toStateJson()], diff --git a/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts b/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts index d6fbadd5baf9b..a15ba6a986049 100644 --- a/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts +++ b/packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts @@ -76,10 +76,15 @@ test('can get detector model name', () => { }); }); -test('can set physical name', () => { +test.each([ + ['physical name', { detectorModelName: 'test-detector-model' }, { DetectorModelName: 'test-detector-model' }], + ['description', { description: 'test-detector-model-description' }, { DetectorModelDescription: 'test-detector-model-description' }], + ['evaluationMethod', { evaluationMethod: iotevents.EventEvaluation.SERIAL }, { EvaluationMethod: 'SERIAL' }], + ['detectorKey', { detectorKey: 'payload.deviceId' }, { Key: 'payload.deviceId' }], +])('can set %s', (_, partialProps, expected) => { // WHEN new iotevents.DetectorModel(stack, 'MyDetectorModel', { - detectorModelName: 'test-detector-model', + ...partialProps, initialState: new iotevents.State({ stateName: 'test-state', onEnter: [{ @@ -90,9 +95,7 @@ test('can set physical name', () => { }); // THEN - Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { - DetectorModelName: 'test-detector-model', - }); + Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', expected); }); test('can set multiple events to State', () => { diff --git a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json index f97d40bc6da25..3b1b598427701 100644 --- a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json +++ b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json @@ -5,6 +5,9 @@ "Properties": { "InputDefinition": { "Attributes": [ + { + "JsonPath": "payload.deviceId" + }, { "JsonPath": "payload.temperature" } @@ -70,7 +73,10 @@ "Arn" ] }, - "DetectorModelName": "test-detector-model" + "DetectorModelDescription": "test-detector-model-description", + "DetectorModelName": "test-detector-model", + "EvaluationMethod": "SERIAL", + "Key": "payload.deviceId" } } } diff --git a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts index 8eeef110d5b8a..dc90a7d505dbf 100644 --- a/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts +++ b/packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts @@ -7,7 +7,7 @@ class TestStack extends cdk.Stack { const input = new iotevents.Input(this, 'MyInput', { inputName: 'test_input', - attributeJsonPaths: ['payload.temperature'], + attributeJsonPaths: ['payload.deviceId', 'payload.temperature'], }); const onlineState = new iotevents.State({ @@ -27,6 +27,9 @@ class TestStack extends cdk.Stack { new iotevents.DetectorModel(this, 'MyDetectorModel', { detectorModelName: 'test-detector-model', + description: 'test-detector-model-description', + evaluationMethod: iotevents.EventEvaluation.SERIAL, + detectorKey: 'payload.deviceId', initialState: onlineState, }); }