SeanRoy/lambda-maven-plugin

Triggers duplicated on each deploy

jeanblanchard opened this issue · 7 comments

Using version 2.2.1 or 2.2.0

My lambda is configured with a "CloudWatch Events - Schedule" trigger.
Each time I run mvn lambda:deploy-lambda, a new trigger is added to the lambda, pointing to the same event.

Configuration details (extract):

      <plugin>
        <groupId>com.github.seanroy</groupId>
        <artifactId>lambda-maven-plugin</artifactId>
        <version>${lambda-maven-plugin.version}</version>
        <configuration>
          <functionCode>${project.build.directory}/${project.build.finalName}.jar</functionCode>
          <version>dev</version>
          <s3Bucket>${s3-bucket}</s3Bucket>
          <lambdaRoleArn>${lambda-role}</lambdaRoleArn>
          <region>eu-west-1</region>
          <runtime>java8</runtime>
          <timeout>60</timeout>
          <memorySize>256</memorySize>
          <lambdaFunctionsJSON>
            [
              {
                "functionName": "EC2Backup",
                "handler": "com.example.EC2Backup",
                "triggers": [
                  {
                    "integration": "CloudWatch Events - Schedule",
                    "ruleName": "daily-weekday-5am",
                    "ruleDescription": "5am on weekdays",
                    "scheduleExpression": "cron(0 5 ? * MON-FRI *)"
                  }
                ]
              }
            ]
          </lambdaFunctionsJSON>
        </configuration>
      </plugin>

Maven logs

$  mvn lambda:deploy-lambda
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building backups 4.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- lambda-maven-plugin:2.2.1:deploy-lambda (default-cli) @ backups ---
[INFO] backups-4.0.0.jar exists in S3 with MD5 hash 000000000000000000
[INFO] backups-4.0.0.jar is up to date in AWS S3 bucket example. Not uploading...
[INFO] ---- Create or update EC2Backup -----
[INFO] Cleaning up orphaned triggers.
[INFO] About to update functionCode for EC2Backup
[INFO] About to update functionConfig for EC2Backup
[INFO] Alias dev updated for EC2Backup with version 1
[INFO] About to create or update CloudWatch Events - Schedule trigger for daily-weekday-5am
[INFO] Created CloudWatch Events - Schedule trigger arn:aws:events:eu-west-1:0000000000:rule/daily-weekday-5am
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.282 s
[INFO] Finished at: 2017-06-21T12:53:40+02:00
[INFO] Final Memory: 17M/123M
[INFO] ------------------------------------------------------------------------

Interesting, I can't reproduce this without setting -DforceUpdate=true. The log output (Created CloudWatch Events...) is misleading, it's not creating a new one, just updating the existing one. You only have one CloudWatch event rule pointed at your lambda function.

Turns out, only the info on the lambda trigger tab is wrong. The lambda is indeed triggered only once per event. The info is correct on the CloudWatch rule page, and with the CLI aws events commands.

So it's possibly just a display issue on the AWS side.

That said, maybe the issue could be mitigated by checking on deploy whether the event has changed, and skip the update if it is unchanged?

The code does actually check to see if the event has been changed. It compares the event rule name, the schedule expression and the description. If any of these have been changed, it updates the event rule.
I just ran a test of this functionality in 2.2.1 and the event was only updated after I changed one or more of those three things.

@jeanblanchard Can I close this issue?

After further investigating, it appears that, in createOrUpdateScheduledRule, isKeepAliveChanged() always returns true if there is no keepAlive defined. This forces an update of the rule.

After updating the rule itself, a new permission is added for the rule (the permissions are not cleaned up, that I could see). This is what is causing the duplicated "events" that I see in the triggers tab. I could confirm this by manually removing the duplicated permissions using the CLI.

PS: I would create a PR for this, now that I have the project sitting in my IDE, but I don't really understand how the keepAlive works (or what the keepalive check is doing at all in the schedule rule check), so I wouldn't want to break everything...

OK thanks for your investigation, I'll remove the keep alives from my configuration and retest.

Resolved in 2.2.2