diff --git a/lib/plugins/aws/package/compile/events/schedule.js b/lib/plugins/aws/package/compile/events/schedule.js index 2cdbb799d06..2a34cf0d029 100644 --- a/lib/plugins/aws/package/compile/events/schedule.js +++ b/lib/plugins/aws/package/compile/events/schedule.js @@ -136,9 +136,11 @@ class AwsCompileScheduledEvents { Name = event.schedule.name; timezone = event.schedule.timezone; Description = event.schedule.description; - roleArn = { - 'Fn::GetAtt': ['IamRoleLambdaExecution', 'Arn'], - }; + + const functionLogicalId = this.provider.naming.getLambdaLogicalId(functionName); + const functionResource = resources[functionLogicalId]; + + roleArn = functionResource.Properties.Role; method = event.schedule.method || METHOD_EVENT_BUS; diff --git a/test/unit/lib/plugins/aws/package/compile/events/schedule.test.js b/test/unit/lib/plugins/aws/package/compile/events/schedule.test.js index 11eb0d3ce33..66cb1505135 100644 --- a/test/unit/lib/plugins/aws/package/compile/events/schedule.test.js +++ b/test/unit/lib/plugins/aws/package/compile/events/schedule.test.js @@ -10,7 +10,7 @@ const METHOD_EVENT_BUS = 'eventBus'; chaiUse(chaiAsPromised); -async function run(events) { +async function run(events, options = {}) { const params = { fixture: 'function', command: 'package', @@ -19,6 +19,7 @@ async function run(events) { test: { handler: 'index.handler', events, + role: options.functionRole, }, }, }, @@ -414,4 +415,24 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', () it('should not create schedule resources when no scheduled event is given', async () => { expect((await run([])).scheduleCfResources).to.be.empty; }); + + it('should pass the custom roleArn to method:schedule resources', async () => { + const events = [ + { + schedule: { + rate: 'rate(15 minutes)', + method: METHOD_SCHEDULER, + name: 'scheduler-scheduled-event', + description: 'Scheduler Scheduled Event', + input: '{"key":"array"}', + }, + }, + ]; + + const { scheduleCfResources } = await run(events, { functionRole: 'customRole' }); + + expect(scheduleCfResources[0].Properties.Target.RoleArn).to.deep.equal({ + 'Fn::GetAtt': ['customRole', 'Arn'], + }); + }); });