Skip to content

Commit

Permalink
fix(AWS EventBridge): Ensure proper support for deadLetterQueueArn
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Sep 30, 2021
1 parent abbb18d commit 846cfa1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 45 deletions.
13 changes: 6 additions & 7 deletions docs/providers/aws/events/event-bridge.md
Expand Up @@ -189,7 +189,7 @@ functions:

## Adding a DLQ to an event rule

DeadLetterConfig is not available for custom resources, only for native CloudFormation.
DeadLetterQueueArn is not available for custom resources, only for native CloudFormation.

```yml
functions:
Expand All @@ -201,11 +201,10 @@ functions:
pattern:
source:
- saas.external
deadLetterConfig:
targetArn:
Fn::GetAtt:
- QueueName
- Arn
deadLetterQueueArn:
Fn::GetAtt:
- QueueName
- Arn
```

## Adding a retry policy to an event rule
Expand All @@ -222,7 +221,7 @@ functions:
pattern:
source:
- saas.external
deadLetterConfig:
deadLetterQueueArn:
Fn::GetAtt:
- QueueName
- Arn
Expand Down
7 changes: 7 additions & 0 deletions docs/providers/aws/guide/serverless.yml.md
Expand Up @@ -568,6 +568,13 @@ functions:
inputPathsMap:
eventTime: '$.time'
inputTemplate: '{"time": <eventTime>, "key1": "value1"}'
retryPolicy:
maximumEventAge: 3600
maximumRetryAttempts: 3
deadLetterQueueArn:
Fn::GetAtt:
- QueueName
- Arn
- cloudFront:
eventType: viewer-response
includeBody: true
Expand Down
42 changes: 16 additions & 26 deletions lib/plugins/aws/package/compile/events/eventBridge/index.js
Expand Up @@ -102,15 +102,7 @@ class AwsCompileEventBridgeEvents {
},
},
},
deadLetterConfig: {
type: 'object',
properties: {
targetArn: {
$ref: '#/definitions/awsArn',
},
},
additionalProperties: false,
},
deadLetterQueueArn: { $ref: '#/definitions/awsArn' },
},
anyOf: [{ required: ['pattern'] }, { required: ['schedule'] }],
});
Expand Down Expand Up @@ -142,7 +134,7 @@ class AwsCompileEventBridgeEvents {
const InputPath = event.eventBridge.inputPath;
let InputTransformer = event.eventBridge.inputTransformer;
let RetryPolicy = event.eventBridge.retryPolicy;
let DeadLetterConfig = event.eventBridge.deadLetterConfig;
let DeadLetterConfig;

const RuleName = makeAndHashRuleName({
functionName: FunctionName,
Expand Down Expand Up @@ -185,15 +177,15 @@ class AwsCompileEventBridgeEvents {
};
}

if (DeadLetterConfig) {
if (event.eventBridge.deadLetterQueueArn) {
if (!shouldUseCloudFormation) {
throw new ServerlessError(
'Configuring DeadLetterConfig is not supported for EventBrigde integration backed by Custom Resources. Please use "provider.eventBridge.useCloudFormation" setting to use native CloudFormation support for EventBridge.',
'ERROR_INVALID_DEAD_LETTER_CONFIG_TO_EVENT_BUS_CUSTOM_RESOURCE'
);
}
DeadLetterConfig = {
TargetArn: DeadLetterConfig.targetArn,
Arn: event.eventBridge.deadLetterQueueArn,
};
}

Expand Down Expand Up @@ -509,35 +501,33 @@ class AwsCompileEventBridgeEvents {
}

configureTarget({ target, Input, InputPath, InputTransformer, RetryPolicy, DeadLetterConfig }) {
if (Input) {
if (RetryPolicy) {
target = Object.assign(target, {
Input: JSON.stringify(Input),
RetryPolicy,
});
return target;
}
if (InputPath) {

if (DeadLetterConfig) {
target = Object.assign(target, {
InputPath,
DeadLetterConfig,
});
return target;
}
if (InputTransformer) {

if (Input) {
target = Object.assign(target, {
InputTransformer,
Input: JSON.stringify(Input),
});
return target;
}

if (RetryPolicy) {
if (InputPath) {
target = Object.assign(target, {
RetryPolicy,
InputPath,
});
return target;
}

if (DeadLetterConfig) {
if (InputTransformer) {
target = Object.assign(target, {
DeadLetterConfig,
InputTransformer,
});
return target;
}
Expand Down
Expand Up @@ -307,7 +307,7 @@ describe('EventBridgeEvents', () => {
);
});

it('should fail when trying to set DeadLetterConfig', async () => {
it('should fail when trying to set DeadLetterQueueArn', async () => {
await expect(
runServerless({
fixture: 'function',
Expand All @@ -318,10 +318,8 @@ describe('EventBridgeEvents', () => {
events: [
{
eventBridge: {
deadLetterConfig: {
targetArn: {
'Fn::GetAtt': ['not-supported', 'Arn'],
},
deadLetterQueueArn: {
'Fn::GetAtt': ['not-supported', 'Arn'],
},
pattern: {
source: ['aws.something'],
Expand Down Expand Up @@ -398,10 +396,8 @@ describe('EventBridgeEvents', () => {
maximumRetryAttempts: 9,
};

const deadLetterConfig = {
targetArn: {
'Fn::GetAtt': ['test', 'Arn'],
},
const deadLetterQueueArn = {
'Fn::GetAtt': ['test', 'Arn'],
};

const getRuleResourceEndingWith = (resources, ending) =>
Expand Down Expand Up @@ -475,7 +471,7 @@ describe('EventBridgeEvents', () => {
eventBus: eventBusName,
schedule,
pattern,
deadLetterConfig,
deadLetterQueueArn,
},
},
],
Expand Down Expand Up @@ -547,10 +543,10 @@ describe('EventBridgeEvents', () => {
});
});

it('should support deadLetterConfig configuration', () => {
it('should support deadLetterQueueArn configuration', () => {
const deadLetterConfigRuleTarget = getRuleResourceEndingWith(cfResources, '7').Properties
.Targets[0];
expect(deadLetterConfigRuleTarget.DeadLetterConfig).to.have.property('TargetArn');
expect(deadLetterConfigRuleTarget.DeadLetterConfig).to.have.property('Arn');
});

it('should create a rule that depends on created EventBus', () => {
Expand Down

0 comments on commit 846cfa1

Please sign in to comment.