diff --git a/README.md b/README.md index ccb2899..fbdb42a 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,9 @@ Note that the ok and lastUpdated are reserved and will automatically be populate o ok is true when statuscode is 200, false otherwise o lastUpdated is the date.time at which the check was ran -* healthcheck to be able to `invoke` lambdas requires the following Policy Statement in `iamRoleStatements`: +* healthcheck needs to be able to `invoke` lambdas + +If your project relies on the IAM Role created by the Serverless Framework, you add the following Policy Statement in `iamRoleStatements`: ```yaml iamRoleStatements: @@ -88,6 +90,8 @@ iamRoleStatements: - function:${self:service}-${opt:stage, self:provider.stage}-* ``` +Alternatively, you may specify the IAM Role explicitly, which will need to allow lambda invocation (`lambda:InvokeFunction`). See options. + If using pre-check, the deployment user also needs a similar policy so it can run the healthcheck lambda. * All done! healthcheck will run on SLS `deploy` and `package` commands @@ -96,6 +100,7 @@ If using pre-check, the deployment user also needs a similar policy so it can ru * **cleanFolder** (default `true`) * **memorySize** (default `128`) +* **role** (default `undefined` – if undefined, falls back to default IAM Role generated by Serverless Framework) * **name** (default `${service}-${stage}-healthcheck-plugin`) * **schedule** (default `rate(5 minutes)`) * **timeout** (default `10` seconds) @@ -105,8 +110,9 @@ If using pre-check, the deployment user also needs a similar policy so it can ru ```yml custom: healthcheck: - cleanFolder: false, + cleanFolder: false memorySize: 256 + role: healthCheckHandlerRole name: 'make-them-pop' schedule: 'rate(15 minutes)' timeout: 20 diff --git a/src/index.js b/src/index.js index dfc5817..63a64fe 100644 --- a/src/index.js +++ b/src/index.js @@ -48,6 +48,7 @@ class HealthCheck { this.healthcheck = { cleanFolder: true, memorySize: 128, + role: undefined, name: this.serverless.service.service + '-' + this.options.stage + '-healthcheck-plugin', schedule: ['rate(5 minutes)'], timeout: 10, @@ -70,6 +71,11 @@ class HealthCheck { this.healthcheck.memorySize = this.custom.healthcheck.memorySize } + /** Role */ + if (typeof this.custom.healthcheck.role === 'string') { + this.healthcheck.role = this.custom.healthcheck.role + } + /** Function name */ if (typeof this.custom.healthcheck.name === 'string') { this.healthcheck.name = this.custom.healthcheck.name @@ -211,6 +217,12 @@ class HealthCheck { timeout: this.healthcheck.timeout } + if (typeof this.healthcheck.role === 'string') { + this.serverless.service.functions.healthCheckPlugin.role = { + 'Fn::GetAtt': [ this.healthcheck.role, 'Arn' ] + } + } + return this.serverless.service.functions.healthCheckPlugin }