Skip to content

Commit

Permalink
fix(iot): unable to add the same lambda function to two TopicRule Act…
Browse files Browse the repository at this point in the history
…ions (#17521)

fix: #17508

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
yamatatsu authored Nov 18, 2021
1 parent 7b2d31e commit eda1640
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as iam from '@aws-cdk/aws-iam';
import * as iot from '@aws-cdk/aws-iot';
import * as lambda from '@aws-cdk/aws-lambda';
import { Names } from '@aws-cdk/core';

/**
* The action to invoke an AWS Lambda function, passing in an MQTT message.
Expand All @@ -12,7 +13,7 @@ export class LambdaFunctionAction implements iot.IAction {
constructor(private readonly func: lambda.IFunction) {}

bind(topicRule: iot.ITopicRule): iot.ActionConfig {
this.func.addPermission('invokedByAwsIotRule', {
this.func.addPermission(`${Names.nodeUniqueId(topicRule.node)}:IotLambdaFunctionAction`, {
action: 'lambda:InvokeFunction',
principal: new iam.ServicePrincipal('iot.amazonaws.com'),
sourceAccount: topicRule.env.account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"MyFunctionServiceRole3C357FF2"
]
},
"MyFunctioninvokedByAwsIotRule5581F304": {
"MyFunctionteststackTopicRule1CB8242FIotLambdaFunctionAction37A1A89F": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,27 @@ test('create a topic rule with lambda action and a lambda permission to be invok
},
});
});

test('create two different permissions, when two topic rules have the same action', () => {
// GIVEN
const stack = new cdk.Stack();
const func = new lambda.Function(stack, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromInline('console.log("foo")'),
});
const action = new actions.LambdaFunctionAction(func);

// WHEN
new iot.TopicRule(stack, 'MyTopicRule1', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
actions: [action],
});
new iot.TopicRule(stack, 'MyTopicRule2', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id FROM 'device/+/data'"),
actions: [action],
});

// THEN
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Permission', 2);
});

0 comments on commit eda1640

Please sign in to comment.