Skip to content

Commit c222b12

Browse files
authored
feat(iotevents): support SetVariable action (#19305)
This PR implememts `SetVariable` actions. This action allow to set variable to detector instanse. If use this action as following and deploy it: ```ts actions: [ new actions.IoteventsSetVariableAction( 'MyVariable', iotevents.Expression.inputAttribute(input, 'payload.temperature'), ), ], ``` And send message via cli as following: ``` aws iotevents-data batch-put-message --messages=messageId=(date | md5),inputName=test_input,payload=(echo '{"payload":{"deviceId":"000","temperature":18.5}}' | base64) ``` You can see the created detector instanse on web console: ![image](https://user-images.githubusercontent.com/11013683/157444640-c93e4229-ba75-46dd-8d35-cd171fed6145.png) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1875c28 commit c222b12

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed

packages/@aws-cdk/aws-iotevents-actions/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,37 @@ AWS IoT Events can trigger actions when it detects a specified event or transiti
2424

2525
Currently supported are:
2626

27+
- Set variable to detector instanse
2728
- Invoke a Lambda function
2829

30+
## Set variable to detector instanse
31+
32+
The code snippet below creates an Action that set variable to detector instanse
33+
when it is triggered.
34+
35+
```ts
36+
import * as iotevents from '@aws-cdk/aws-iotevents';
37+
import * as actions from '@aws-cdk/aws-iotevents-actions';
38+
39+
declare const input: iotevents.IInput;
40+
41+
const state = new iotevents.State({
42+
stateName: 'MyState',
43+
onEnter: [{
44+
eventName: 'test-event',
45+
condition: iotevents.Expression.currentInput(input),
46+
actions: [
47+
actions: [
48+
new actions.SetVariableAction(
49+
'MyVariable',
50+
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
51+
),
52+
],
53+
],
54+
}],
55+
});
56+
```
57+
2958
## Invoke a Lambda function
3059

3160
The code snippet below creates an Action that invoke a Lambda function
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './set-variable-action';
12
export * from './lambda-invoke-action';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as iotevents from '@aws-cdk/aws-iotevents';
2+
import { Construct } from 'constructs';
3+
4+
/**
5+
* The action to create a variable with a specified value.
6+
*/
7+
export class SetVariableAction implements iotevents.IAction {
8+
/**
9+
* @param variableName the name of the variable
10+
* @param value the new value of the variable
11+
*/
12+
constructor(private readonly variableName: string, private readonly value: iotevents.Expression) {
13+
}
14+
15+
bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig {
16+
return {
17+
configuration: {
18+
setVariable: {
19+
variableName: this.variableName,
20+
value: this.value.evaluate(),
21+
},
22+
},
23+
};
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"Resources": {
3+
"MyInput08947B23": {
4+
"Type": "AWS::IoTEvents::Input",
5+
"Properties": {
6+
"InputDefinition": {
7+
"Attributes": [
8+
{
9+
"JsonPath": "payload.deviceId"
10+
},
11+
{
12+
"JsonPath": "payload.temperature"
13+
}
14+
]
15+
},
16+
"InputName": "test_input"
17+
}
18+
},
19+
"MyDetectorModelDetectorModelRoleF2FB4D88": {
20+
"Type": "AWS::IAM::Role",
21+
"Properties": {
22+
"AssumeRolePolicyDocument": {
23+
"Statement": [
24+
{
25+
"Action": "sts:AssumeRole",
26+
"Effect": "Allow",
27+
"Principal": {
28+
"Service": "iotevents.amazonaws.com"
29+
}
30+
}
31+
],
32+
"Version": "2012-10-17"
33+
}
34+
}
35+
},
36+
"MyDetectorModel559C0B0E": {
37+
"Type": "AWS::IoTEvents::DetectorModel",
38+
"Properties": {
39+
"DetectorModelDefinition": {
40+
"InitialStateName": "MyState",
41+
"States": [
42+
{
43+
"OnEnter": {
44+
"Events": [
45+
{
46+
"Actions": [
47+
{
48+
"SetVariable": {
49+
"Value": {
50+
"Fn::Join": [
51+
"",
52+
[
53+
"$input.",
54+
{
55+
"Ref": "MyInput08947B23"
56+
},
57+
".payload.temperature"
58+
]
59+
]
60+
},
61+
"VariableName": "MyVariable"
62+
}
63+
}
64+
],
65+
"Condition": {
66+
"Fn::Join": [
67+
"",
68+
[
69+
"currentInput(\"",
70+
{
71+
"Ref": "MyInput08947B23"
72+
},
73+
"\")"
74+
]
75+
]
76+
},
77+
"EventName": "enter-event"
78+
}
79+
]
80+
},
81+
"StateName": "MyState"
82+
}
83+
]
84+
},
85+
"RoleArn": {
86+
"Fn::GetAtt": [
87+
"MyDetectorModelDetectorModelRoleF2FB4D88",
88+
"Arn"
89+
]
90+
},
91+
"Key": "payload.deviceId"
92+
}
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Stack verification steps:
3+
* * put a message
4+
* * aws iotevents-data batch-put-message --messages=messageId=(date | md5),inputName=test_input,payload=(echo '{"payload":{"temperature":31.9,"deviceId":"000"}}' | base64)
5+
*/
6+
import * as iotevents from '@aws-cdk/aws-iotevents';
7+
import * as cdk from '@aws-cdk/core';
8+
import * as actions from '../../lib';
9+
10+
class TestStack extends cdk.Stack {
11+
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
12+
super(scope, id, props);
13+
14+
const input = new iotevents.Input(this, 'MyInput', {
15+
inputName: 'test_input',
16+
attributeJsonPaths: ['payload.deviceId', 'payload.temperature'],
17+
});
18+
19+
const state = new iotevents.State({
20+
stateName: 'MyState',
21+
onEnter: [{
22+
eventName: 'enter-event',
23+
condition: iotevents.Expression.currentInput(input),
24+
actions: [
25+
new actions.SetVariableAction(
26+
'MyVariable',
27+
iotevents.Expression.inputAttribute(input, 'payload.temperature'),
28+
),
29+
],
30+
}],
31+
});
32+
33+
new iotevents.DetectorModel(this, 'MyDetectorModel', {
34+
detectorKey: 'payload.deviceId',
35+
initialState: state,
36+
});
37+
}
38+
}
39+
40+
const app = new cdk.App();
41+
new TestStack(app, 'iotevents-set-variable-action-test-stack');
42+
app.synth();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Template } from '@aws-cdk/assertions';
2+
import * as iotevents from '@aws-cdk/aws-iotevents';
3+
import * as cdk from '@aws-cdk/core';
4+
import * as actions from '../../lib';
5+
6+
let stack: cdk.Stack;
7+
let input: iotevents.IInput;
8+
beforeEach(() => {
9+
stack = new cdk.Stack();
10+
input = iotevents.Input.fromInputName(stack, 'MyInput', 'test-input');
11+
});
12+
13+
test('Default property', () => {
14+
// WHEN
15+
new iotevents.DetectorModel(stack, 'MyDetectorModel', {
16+
initialState: new iotevents.State({
17+
stateName: 'test-state',
18+
onEnter: [{
19+
eventName: 'test-eventName',
20+
condition: iotevents.Expression.currentInput(input),
21+
actions: [new actions.SetVariableAction('MyVariable', iotevents.Expression.fromString('foo'))],
22+
}],
23+
}),
24+
});
25+
26+
// THEN
27+
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', {
28+
DetectorModelDefinition: {
29+
States: [{
30+
OnEnter: {
31+
Events: [{
32+
Actions: [{
33+
SetVariable: {
34+
VariableName: 'MyVariable',
35+
Value: 'foo',
36+
},
37+
}],
38+
}],
39+
},
40+
}],
41+
},
42+
});
43+
});

0 commit comments

Comments
 (0)