Search code examples
amazon-web-servicesaws-lambdaserverless-frameworkserverless

Serverless / AWS Lambda - Create the triggers for the published lambda versions


I'm using the Serverless framework to deploy my functions on 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 in the following image:

Lambda function with trigger

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

Published version without trigger

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

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

Here is 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

Solution

  • UPDATE

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

    I realized the issue was that my trigger did not have permission to invoke the published Lambda function. So, I needed to add the permission for the trigger first using the add-permission method. (This is not clearly stated in the AWS documentation :/).

    Before adding the trigger to 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 with the Serverless framework, and Shazam! my triggers were published! We can use this workaround for now while the Serverless code is not updated.

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


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

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

    For further information, you can read the documentation on Versioning aliases.