diff --git a/packages/@aws-cdk/aws-iotevents-actions/README.md b/packages/@aws-cdk/aws-iotevents-actions/README.md index eb88dc82bb3c3..486ddd8124c34 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/README.md +++ b/packages/@aws-cdk/aws-iotevents-actions/README.md @@ -24,8 +24,37 @@ AWS IoT Events can trigger actions when it detects a specified event or transiti Currently supported are: +- Set variable to detector instanse - Invoke a Lambda function +## Set variable to detector instanse + +The code snippet below creates an Action that set variable to detector instanse +when it is triggered. + +```ts +import * as iotevents from '@aws-cdk/aws-iotevents'; +import * as actions from '@aws-cdk/aws-iotevents-actions'; + +declare const input: iotevents.IInput; + +const state = new iotevents.State({ + stateName: 'MyState', + onEnter: [{ + eventName: 'test-event', + condition: iotevents.Expression.currentInput(input), + actions: [ + actions: [ + new actions.SetVariableAction( + 'MyVariable', + iotevents.Expression.inputAttribute(input, 'payload.temperature'), + ), + ], + ], + }], +}); +``` + ## Invoke a Lambda function The code snippet below creates an Action that invoke a Lambda function diff --git a/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts b/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts index 4b2ec39329315..e51394d301376 100644 --- a/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts +++ b/packages/@aws-cdk/aws-iotevents-actions/lib/index.ts @@ -1 +1,2 @@ +export * from './set-variable-action'; export * from './lambda-invoke-action'; diff --git a/packages/@aws-cdk/aws-iotevents-actions/lib/set-variable-action.ts b/packages/@aws-cdk/aws-iotevents-actions/lib/set-variable-action.ts new file mode 100644 index 0000000000000..1d2596ddb866b --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/lib/set-variable-action.ts @@ -0,0 +1,25 @@ +import * as iotevents from '@aws-cdk/aws-iotevents'; +import { Construct } from 'constructs'; + +/** + * The action to create a variable with a specified value. + */ +export class SetVariableAction implements iotevents.IAction { + /** + * @param variableName the name of the variable + * @param value the new value of the variable + */ + constructor(private readonly variableName: string, private readonly value: iotevents.Expression) { + } + + bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig { + return { + configuration: { + setVariable: { + variableName: this.variableName, + value: this.value.evaluate(), + }, + }, + }; + } +} diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.expected.json b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.expected.json new file mode 100644 index 0000000000000..0119afc958c1f --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.expected.json @@ -0,0 +1,95 @@ +{ + "Resources": { + "MyInput08947B23": { + "Type": "AWS::IoTEvents::Input", + "Properties": { + "InputDefinition": { + "Attributes": [ + { + "JsonPath": "payload.deviceId" + }, + { + "JsonPath": "payload.temperature" + } + ] + }, + "InputName": "test_input" + } + }, + "MyDetectorModelDetectorModelRoleF2FB4D88": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "iotevents.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + } + } + }, + "MyDetectorModel559C0B0E": { + "Type": "AWS::IoTEvents::DetectorModel", + "Properties": { + "DetectorModelDefinition": { + "InitialStateName": "MyState", + "States": [ + { + "OnEnter": { + "Events": [ + { + "Actions": [ + { + "SetVariable": { + "Value": { + "Fn::Join": [ + "", + [ + "$input.", + { + "Ref": "MyInput08947B23" + }, + ".payload.temperature" + ] + ] + }, + "VariableName": "MyVariable" + } + } + ], + "Condition": { + "Fn::Join": [ + "", + [ + "currentInput(\"", + { + "Ref": "MyInput08947B23" + }, + "\")" + ] + ] + }, + "EventName": "enter-event" + } + ] + }, + "StateName": "MyState" + } + ] + }, + "RoleArn": { + "Fn::GetAtt": [ + "MyDetectorModelDetectorModelRoleF2FB4D88", + "Arn" + ] + }, + "Key": "payload.deviceId" + } + } + } +} \ No newline at end of file diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.ts b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.ts new file mode 100644 index 0000000000000..e6256e14710b2 --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iot/integ.set-variable-action.ts @@ -0,0 +1,42 @@ +/** + * Stack verification steps: + * * put a message + * * aws iotevents-data batch-put-message --messages=messageId=(date | md5),inputName=test_input,payload=(echo '{"payload":{"temperature":31.9,"deviceId":"000"}}' | base64) + */ +import * as iotevents from '@aws-cdk/aws-iotevents'; +import * as cdk from '@aws-cdk/core'; +import * as actions from '../../lib'; + +class TestStack extends cdk.Stack { + constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { + super(scope, id, props); + + const input = new iotevents.Input(this, 'MyInput', { + inputName: 'test_input', + attributeJsonPaths: ['payload.deviceId', 'payload.temperature'], + }); + + const state = new iotevents.State({ + stateName: 'MyState', + onEnter: [{ + eventName: 'enter-event', + condition: iotevents.Expression.currentInput(input), + actions: [ + new actions.SetVariableAction( + 'MyVariable', + iotevents.Expression.inputAttribute(input, 'payload.temperature'), + ), + ], + }], + }); + + new iotevents.DetectorModel(this, 'MyDetectorModel', { + detectorKey: 'payload.deviceId', + initialState: state, + }); + } +} + +const app = new cdk.App(); +new TestStack(app, 'iotevents-set-variable-action-test-stack'); +app.synth(); diff --git a/packages/@aws-cdk/aws-iotevents-actions/test/iot/set-variable-action.test.ts b/packages/@aws-cdk/aws-iotevents-actions/test/iot/set-variable-action.test.ts new file mode 100644 index 0000000000000..1f779644b1a94 --- /dev/null +++ b/packages/@aws-cdk/aws-iotevents-actions/test/iot/set-variable-action.test.ts @@ -0,0 +1,43 @@ +import { Template } from '@aws-cdk/assertions'; +import * as iotevents from '@aws-cdk/aws-iotevents'; +import * as cdk from '@aws-cdk/core'; +import * as actions from '../../lib'; + +let stack: cdk.Stack; +let input: iotevents.IInput; +beforeEach(() => { + stack = new cdk.Stack(); + input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input'); +}); + +test('Default property', () => { + // WHEN + new iotevents.DetectorModel(stack, 'MyDetectorModel', { + initialState: new iotevents.State({ + stateName: 'test-state', + onEnter: [{ + eventName: 'test-eventName', + condition: iotevents.Expression.currentInput(input), + actions: [new actions.SetVariableAction('MyVariable', iotevents.Expression.fromString('foo'))], + }], + }), + }); + + // THEN + Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', { + DetectorModelDefinition: { + States: [{ + OnEnter: { + Events: [{ + Actions: [{ + SetVariable: { + VariableName: 'MyVariable', + Value: 'foo', + }, + }], + }], + }, + }], + }, + }); +});