Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support lambda actions for IoT topic rules #5420

Closed
JoshM1994 opened this issue Dec 13, 2019 · 5 comments
Closed

support lambda actions for IoT topic rules #5420

JoshM1994 opened this issue Dec 13, 2019 · 5 comments
Labels
@aws-cdk/aws-iot Related to AWS IoT effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. p1

Comments

@JoshM1994
Copy link

Very similar to #555

There does not appear to be an automatic way to add permissions (the "Function Policy") to the Lambda which is invoked from an IoT Topic Rule.

I obviously was not expecting the below to work since there is no explicit link between the IoT rule and the Lambda (since IoT does not appear to be supported by lambda-event-sources)

  • The IoT rule is created with Lambda as its target
  • The Lambda is created with no trigger (nothing visible on the console)
  • No Function Policy exists on the Lambda

Upon "editing" the IoT rule through the console, AWS helpfully says "we'll handle the Lambda permissions for you" - would be nice if this happened through the CDK

Reproduction Steps

const motaAckLambda = new lambda.Function(this, 'MotaGwAck', {
    code: lambda.Code.fromAsset('apps/stacks/hw-mgmt/fota/dist/'),
    handler: 'fotaAck.handler',
    runtime: lambda.Runtime.NODEJS_10_X,
});
const lambdaIotAction: LambdaActionProperty = {
    functionArn: motaAckLambda.functionArn,
};
const iotFwdRule = new iot.CfnTopicRule(this, 'IotLambFwdRule', {
    topicRulePayload: {
        actions: [
            {
                lambda: lambdaIotAction,
            },
        ],
        ruleDisabled: false,
        sql: `SELECT soemthing FROM 'somewhere'`,
        awsIotSqlVersion: '2016-03-23',
    },
});

Error Log

The IoT rule does NOT trigger the lambda and, in fact, I don't think the rule triggers at all (no error is logged in the verbose IoT logs in CloudWatch)

Environment

  • CLI Version :1.18.0
  • Framework Version: 1.18.0
  • OS : Mac
  • Language : TypeScript

Other

Removing the policy

  • Whilst experimenting with this (to reproduce), if you need to remove the policy (you cannot do it through the console)
➜  ~ aws lambda get-policy --function-name functionArn
# Get the statement SID from here
➜  ~ aws lambda remove-permission --function-name functionArn --statement-id retrievedAbove

Current approach

My current approach (which I can confirm does work)

Everything as above and append:

motaAckLambda.addPermission('AllowIoTInvoke', {
    principal: new ServicePrincipal('iot.amazonaws.com'),
    sourceArn: iotFwdRule.attrArn,
});

It's unclear if this is the suggested fix (because IoT has notoriously never had a huge amount of documentation) but its working. Still, would be nice if IoT worked in the same way as almost every other Lambda event trigger


This is 🐛 Bug Report

@JoshM1994 JoshM1994 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 13, 2019
@SomayaB SomayaB added @aws-cdk/aws-iot Related to AWS IoT @aws-cdk/aws-lambda Related to AWS Lambda labels Dec 14, 2019
@shivlaks shivlaks assigned nija-at and unassigned shivlaks Dec 14, 2019
@nija-at
Copy link
Contributor

nija-at commented Jan 3, 2020

As you've noted, using Lambda's addPermission() method is the best workaround.

This issue is not a bug, but a feature gap. Ideally, we would have a way to configure a lambda function, dynamo table or SNS topic directly into an IoT rule as an action.

Unfortunately, we don't yet have full support of IoT in the CDK, and only support Cfn* constructs.
Any required permissions would be correctly modeled and granted when we have full support.

If this was built out, it would be modeled as a secondary module of IoT, named something like @aws-cdk/aws-iot-actions and the code would look something like -

const fn = new lambda.Function(this, 'myfunction',  { ... });
const rule = new iot.Rule(this, 'myrule', { ... });
rule.addAction(new LambdaAction({
  handler: fn,
  ...
}));

The addAction() and the LambdaAction classes in conjunction would take care of assigning the right permission.

@shivlaks - does this seem reasonable to you? Would you consider this a gap with the IoT construct library support?

@nija-at nija-at changed the title InvokePermission not automatically added to Lambda when triggered via IoT Topic Rule support lambda actions for IoT topic rules Jan 3, 2020
@nija-at nija-at added gap and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 3, 2020
@shivlaks shivlaks added the effort/large Large work item – several weeks of effort label Feb 5, 2020
@SomayaB SomayaB added feature-request A feature should be added or improved. and removed gap labels Feb 25, 2020
@AniketDani
Copy link

How to implement the same using Java ?

@nija-at nija-at removed the @aws-cdk/aws-iot Related to AWS IoT label Aug 18, 2020
@nija-at nija-at assigned shivlaks and unassigned nija-at Aug 18, 2020
@nija-at nija-at added @aws-cdk/aws-iot Related to AWS IoT and removed @aws-cdk/aws-lambda Related to AWS Lambda labels Aug 18, 2020
@shivlaks shivlaks added the p1 label Aug 21, 2020
@NGL321 NGL321 assigned skinny85 and unassigned shivlaks Jan 25, 2021
@tomcatvr
Copy link

I've the same problem in Java
This code create a rule on IotCore which essentially use a lambda to log everything is published on the topic 'mytopic'
CDK deploy the stack without error but the lambda is never called
To let the lambda be called I need to log on the console, select the rule -> Actions-> Edit and re select the lambda

``
Function lambdaFunction = Function.Builder.create(this, "logData")
.code(Code.fromAsset("./asset/lambda-log-1.0-jar-with-dependencies.jar"))
.handler("com.plussrl.devel.lamda.log.App")
.runtime(Runtime.JAVA_11)
.timeout(Duration.seconds(30))
.memorySize(128)
.build();

    CfnTopicRule.LambdaActionProperty lambdaActionProperty = CfnTopicRule.LambdaActionProperty
            .builder().functionArn(lambdaFunction.getFunctionArn()).build();

    CfnTopicRule.ActionProperty ap = CfnTopicRule.ActionProperty.builder().lambda(lambdaActionProperty).build();

    List<CfnTopicRule.ActionProperty> actions = new ArrayList<>();
    actions.add(ap);

    CfnTopicRule.TopicRulePayloadProperty topicRulePayloadProperty = CfnTopicRule.TopicRulePayloadProperty
            .builder()
            .actions(actions)
            .sql("SELECT * FROM 'mytopic'")
            .description("Log on lambda logData")
            .awsIotSqlVersion("2016-03-23")
            .ruleDisabled(false)
            .build();

    CfnTopicRule.Builder.create(this, "topicRule")
            .topicRulePayload(topicRulePayloadProperty)
            .ruleName("log_on_logData")
            .build();
}

``

Step to reproduce

  1. cdk deploy
  2. test is not working publishing message (Iot Core-> test ->Publish topic: mytopic
  3. nothing happens
  4. edit the rule Iot Core-> Rules-> log_on_logData-> Actions-> Edit, select again IotCoreCdkStack-logDataXXX and then update
  5. publish another message
  6. wait a couple of seconds to see the log in cloudwatch
  7. Sample project is in the attachment

@yamatatsu
Copy link
Contributor

This is resolved by #17110 😃

@skinny85 skinny85 closed this as completed Nov 9, 2021
@github-actions
Copy link

github-actions bot commented Nov 9, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-iot Related to AWS IoT effort/large Large work item – several weeks of effort feature-request A feature should be added or improved. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants