3
votes

I'm using the Serverless framework to deploy my functions on the AWS Lambda

I'm trying to create a trigger automatically for each version of my Lambda functions published.

When I deploy my serverless app, the Lambda function and the triggers are created (in this case my AWS IOT trigger), as we can see on the following image:

enter image description here

But for my published version of the lambda function the trigger doesn't exist, only the resources:

enter image description here

I don't want to create new triggers every time I publish a new lambda version.

So, there is any way to create the triggers for my versioned lambdas too? And if is possible, disable the old ones using the Serverless framework?

my serverless.yml file:

service: serverless-lambdas
provider:
  name: aws
  runtime: nodejs6.10
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "ses:*"
        - "iot:*"
      Resource:
        - "*"

functions:
   function1:
    name: "function1"
    handler: function1/handler.function1
    events:
      - iot:
          name: "iotEvent1"
          sql: "SELECT EXAMPLE"
          sqlVersion: "2016-03-23"
          enabled: true
1

1 Answers

0
votes

UPDATE

I got a similar problem when I was trying to create triggers programmatically using my own AWS Lambda.

I got stuck on this when I saw that the problem was with my trigger that which had no permission to invoke the published Lambda function. So I needed to add the permission for the trigger first with the method add-permission. (This is not clearly written on the AWS docs :/).

So, before I added the the trigger on the Lambda, I used the following method (in node.js):

const addPermission = (ruleName) => {
  const thingArn = `arn:aws:iot:${IOT_REGION}:${SOURCE_ACCOUNT}:rule/${ruleName}`;
  const params = {
    Action: "lambda:InvokeFunction",
    FunctionName: LAMBDA_NAME,
    Principal: "iot.amazonaws.com",
    SourceAccount: SOURCE_ACCOUNT,
    SourceArn: thingArn,
    StatementId: `iot-sd-${Math.random().toString(36).substring(2) + Date.now().toString(36)}`
  };

  return new Promise((resolve, reject) => {
    lambda.addPermission(params).promise().then(result => {
      resolve(result)
    }).catch(err => reject(err))
  });
};

I tested the same function for the Serverless framework, and Shazam! my triggers were published! We can do something like this for now while the Serverless code is not updated.

In this way, this problem will need to be solved on the Serverless source-code and I will try to do it ASAP.


From what I checked this is the default behavior for the AWS Lambda functions, so there is no issue with the Serverless framework.

Every time I publish a Lambda function, there is no way to create the trigger events automatically.

For further information, we can read the documentation of Versioning aliases.