Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AWS Schedule): Support deadLetterTargetArn #12363

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/providers/aws/events/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,5 @@ functions:
input:
key1: value1
key2: value2
deadLetterTargetArn: !GetAtt DLQ.Arn
```
18 changes: 18 additions & 0 deletions lib/plugins/aws/package/compile/events/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class AwsCompileScheduledEvents {
type: 'string',
pattern: '[\\w\\-\\/]+',
},
deadLetterTargetArn: { $ref: '#/definitions/awsArn' },
},
required: ['rate'],
additionalProperties: false,
Expand Down Expand Up @@ -122,6 +123,7 @@ class AwsCompileScheduledEvents {
let method;
let roleArn;
let timezone;
let DeadLetterTargetArn;

if (typeof event.schedule === 'object') {
ScheduleExpressions = event.schedule.rate;
Expand All @@ -136,6 +138,7 @@ class AwsCompileScheduledEvents {
Name = event.schedule.name;
timezone = event.schedule.timezone;
Description = event.schedule.description;
DeadLetterTargetArn = event.schedule.deadLetterTargetArn;

const functionLogicalId = this.provider.naming.getLambdaLogicalId(functionName);
const functionResource = resources[functionLogicalId];
Expand Down Expand Up @@ -188,6 +191,13 @@ class AwsCompileScheduledEvents {
State = 'ENABLED';
}

if (DeadLetterTargetArn && method !== METHOD_SCHEDULER) {
throw new ServerlessError(
'Cannot setup "schedule" event: "deadLetterTargetArn" is only supported with "scheduler" mode',
'SCHEDULE_PARAMETER_NOT_SUPPORTED'
);
}

const lambdaTarget = resolveLambdaTarget(functionName, functionObj);
const lambdaTargetJson = JSON.stringify(lambdaTarget);
const dependsOn =
Expand All @@ -209,6 +219,13 @@ class AwsCompileScheduledEvents {
scheduleNumberInFunction
);

let DeadLetterConfig;
if (DeadLetterTargetArn) {
DeadLetterConfig = {
Arn: DeadLetterTargetArn,
};
}

resources[scheduleLogicalId] = {
Type: 'AWS::Scheduler::Schedule',
DependsOn: dependsOn,
Expand All @@ -219,6 +236,7 @@ class AwsCompileScheduledEvents {
Arn: lambdaTarget,
RoleArn: roleArn,
Input,
DeadLetterConfig,
},
FlexibleTimeWindow: {
Mode: 'OFF',
Expand Down
32 changes: 32 additions & 0 deletions test/unit/lib/plugins/aws/package/compile/events/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
name: 'scheduler-scheduled-event',
description: 'Scheduler Scheduled Event',
input: '{"key":"array"}',
deadLetterTargetArn: { 'Fn::GetAtt': ['SomeQueue', 'Arn'] },
},
},
{
Expand Down Expand Up @@ -219,6 +220,21 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
expect(scheduleCfResources[9].Properties.Description).to.be.undefined;
});

it('should respect the "deadLetterTargetArn" variable', () => {
expect(scheduleCfResources[0].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[1].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[2].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[3].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[4].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[5].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[6].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[7].Properties.Targets[0].DeadLetterConfig).to.be.undefined;
expect(scheduleCfResources[8].Properties.Target.DeadLetterConfig).to.deep.equal({
Arn: { 'Fn::GetAtt': ['SomeQueue', 'Arn'] },
});
expect(scheduleCfResources[9].Properties.Target.DeadLetterConfig).to.be.undefined;
});

it('should respect the "inputPath" variable', () => {
expect(scheduleCfResources[0].Properties.Targets[0].InputPath).to.be.undefined;
expect(scheduleCfResources[1].Properties.Targets[0].InputPath).to.equal('$.stageVariables');
Expand Down Expand Up @@ -317,6 +333,22 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
);
});

it('should throw when passing "deadLetterTargetArn" to method:eventBus resources', async () => {
const events = [
{
schedule: {
rate: 'rate(15 minutes)',
method: METHOD_EVENT_BUS,
deadLetterTargetArn: { 'Fn::GetAtt': ['SomeQueue', 'Arn'] },
},
},
];

await expect(run(events))
.to.be.eventually.rejectedWith(ServerlessError)
.and.have.property('code', 'SCHEDULE_PARAMETER_NOT_SUPPORTED');
});

it('should throw when passing "inputPath" to method:schedule resources', async () => {
const events = [
{
Expand Down