Skip to content

Adds creation of dlq from resources #183

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

Open
wants to merge 2 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/serverless-offline-sqs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,24 @@ resources:
Type: AWS::SQS::Queue
Properties:
QueueName: MyFourthQueue
RedrivePolicy:
deadLetterTargetArn: # Support only this format for autoCreate
Fn::GetAtt:
- MyFourthQueueDlq
- Arn
maxReceiveCount: 6

MyFifthQueue: # Support for Fifo queue creation starts from 3.1 only
Type: AWS::SQS::Queue
Properties:
QueueName: MyFifthQueue.fifo
FifoQueue: true
ContentBasedDeduplication: true

MyFourthQueueDlq:
Type: AWS::SQS::Queue
Properties:
QueueName: MyFourthQueueDlq
```

### SQS
Expand Down
2 changes: 2 additions & 0 deletions packages/serverless-offline-sqs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class ServerlessOfflineSQS {

this.sqs = new SQS(this.lambda, resources, this.options);

await this.sqs.createDlq(get(['service', 'resources', 'Resources'], this.serverless));

await this.sqs.create(events);

if (!skipStart) {
Expand Down
46 changes: 44 additions & 2 deletions packages/serverless-offline-sqs/src/sqs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
const {default: PQueue} = require('p-queue');
const SQSClient = require('aws-sdk/clients/sqs');
// eslint-disable-next-line no-shadow
const {pipe, get, values, matches, find, mapValues, isPlainObject, toString} = require('lodash/fp');
const {
pipe,
get,
values,
matches,
find,
mapValues,
isPlainObject,
// eslint-disable-next-line no-shadow
toString,
map,
compact
} = require('lodash/fp');
const {logWarning} = require('serverless-offline/dist/serverlessLog');
const SQSEventDefinition = require('./sqs-event-definition');
const SQSEvent = require('./sqs-event');
Expand Down Expand Up @@ -30,6 +41,10 @@ class SQS {
return Promise.all(events.map(({functionKey, sqs}) => this._create(functionKey, sqs)));
}

createDlq(resources) {
return this._createDlq(resources);
}

start() {
this.queue.start();
}
Expand All @@ -48,6 +63,33 @@ class SQS {
return this._sqsEvent(functionKey, sqsEvent);
}

_createDlq(resources) {
if (!this.options.autoCreate) return;
const dlqNames = this._getDlqNames(resources);
return Promise.all(
dlqNames.map(queueName => {
return this._createQueue({queueName});
})
);
}

// eslint-disable-next-line class-methods-use-this
_getDlqNames(resources) {
return pipe(
values,
map(value => {
const dlq = get(['Properties', 'RedrivePolicy', 'deadLetterTargetArn'], value);
if (!dlq) return;
const [resourceName, attribute] = dlq['Fn::GetAtt'];
const type = get(['Type'], resources[resourceName]);
if (attribute !== 'Arn') return;
if (type !== 'AWS::SQS::Queue') return;
return get(['Properties', 'QueueName'], resources[resourceName]);
}),
compact
)(resources);
}

async _getQueueUrl(queueName) {
try {
return await this.client.getQueueUrl({QueueName: queueName}).promise();
Expand Down