Array | The configuration of one or more SNS Topics to which the queue will be subscribed to. (See SNSHelper to know how to create an SNS Topic)
Only with a name can create everything except for the Delay hooks (queue and consumer) and DLQ Consumer function
Consumer Properties:
All consumerProperties
, delayConsumerProperties
and dlqConsumerProperties
fields can be customized with the following properties:
timeout
: default: 15 | Change the Function timeout (in seconds).handler
: default: src/sqs-consumer/[name in lowerCase]-consumer.handler
| Change the location of the file.description
: default: [name] SQS Queue Consumer
| Change the function description.batchSize
: default: 1 (only for main consumer) | Change the SQS consumer batch Size.maximumBatchingWindow
: default: 10 (only for main consumer) | Change the SQS consumer maximum batching window.prefixPath
: String: To add optional prefix path after src/sqs-consumer
. e.g. src/sqs-consumer/[prefixPath]/[name in lowerCase]-consumer.handler
Some other properties
functionProperties
: Object | To add other properties to the function (the same one in function
hook).rawProperties
: Object | To add rawProperties to the function for example changed a DependsOn
.eventProperties
: Object | To add extra Properties to the sqs event configuration, for example functionResponseType
Delay Consumer and DLQ Consumer properties
useMainHandler
: boolean | To use the main consumer and not creating other function.
Queue Properties:
All mainQueueProperties
, delayQueueProperties
and dlqQueueProperties
fields can be customized with the following properties:
maxReceiveCount
: default: 5 (only for MainQueue and DelayQueue) | Change the max receive count properties before sent the message to DelayQueue or DLQ.receiveMessageWaitTimeSeconds
: default: 20 (MainQueue and DelayQueue) or 5 (DLQ).visibilityTimeout
: default: 60 (MainQueue and DelayQueue) or 20 (DLQ).messageRetentionPeriod
: default: 864000 (only for DLQ).delaySeconds
: default: 300 (only for DelayQueue).addTags
: object array: To add Tags for queues. The AWS tag format is [{ Key: 'myTag', Value: 'theTagValue' }]
.generateEnvVars
: boolean | If set to true, the environment variables with the SQS url will be generated. The default will be true
only for SQS queues.
FIFO properties (since 9.6.0)
fifoQueue
: boolean | If set to true
, creates a FIFO queue.contentBasedDeduplication
: boolean | Specifies whether to enable content-based deduplication.fifoThroughputLimit
: string | Valid values are perQueue
and perMessageGroupId
.deduplicationScope
: string | Valid values are queue
and messageGroup
.
Source SNS Topic
The sourceSnsTopic
parameter has the following structure:
name
REQUIRED The name of the SNS TopicfilterPolicy
OPTIONAL An object defining a SNS Subscription Policy to apply. The policy will be applied to the messages attributes scope, not to the event payload.
For cross-account topics (ie, topics pubished in another AWS Accounts / Janis Services), you must add the following properties:
scope
: string With the fixed value of remote
serviceCode
: string With the service code of the remote service that will publish the topic
Returns: array of Hooks
Consumer Files
If handler's location don't change (uses default values), the files must be located in
src/sqs-consumer/[name-in-kebab-case]-consumer.js
for main queue consumersrc/sqs-consumer/[name-in-kebab-case]-delay-consumer.js
for delay consumersrc/sqs-consumer/[name-in-kebab-case]-dlq-consumer.js
for dlq consumer
If prefixPath
received the location will be
src/sqs-consumer/[prefixPath]/[name-in-kebab-case]-consumer.js
for main queue consumersrc/sqs-consumer/[prefixPath]/[name-in-kebab-case]-delay-consumer.js
for delay consumersrc/sqs-consumer/[prefixPath]/[name-in-kebab-case]-dlq-consumer.js
for dlq consumer
SQS URL Env Vars
Environment Variables will be created for SQS URL if the property generateEnvVars
of each queue is set as true (for main queues, generateEnvVars
defaults to true
):
[NAME_IN_SNAKE_CASE]_SQS_QUEUE_URL
for main queue[NAME_IN_SNAKE_CASE]_DELAY_QUEUE_URL
for delay queue (when delayQueueProperties
received)[NAME_IN_SNAKE_CASE]_DLQ_SQS_QUEUE_URL
for dlq
FIFO queues uses the same Environment Variables as Standard queues.
Disable Global env vars
As service grows, the environment variable size quota is reached and breaks service deployments. To avoid it, global env vars can be disabled, and Queue URL env vars can be set in a per-function basis.
To do so, you can use SQSHelper.shouldSetGlobalEnvVars(false)
method (by default, global env vars are enabled).
Once disabled, you MUST set the variables to each Lambda function that needs them using SQSHelper.getEnvVar(queueName)
, for example:
const { helper } = require('sls-helper');
const { SQSHelper } = require('sls-helper-plugin-janis');
SQSHelper.shouldSetGlobalEnvVars(false);
module.exports = helper({
hooks: [
SQSHelper.sqsPermissions
...SQSHelper.buildHooks({ name: 'SessionEnded' }),
['function', {
functionName: 'EndSession',
handler: 'src/lambda/Session/End.handler',
rawProperties: {
environment: {
...SQSHelper.getEnvVar('SessionEnded')
}
}
}],
[['janis.api',
{
path: '/session/{id}/end',
method: 'post',
cors: true,
functionRawProps: {
environment: {
...SQSHelper.getEnvVar('SessionEnded')
}
}
}
]]
]
});
Quick hook example
const { helper } = require('sls-helper');
const { SQSHelper } = require('sls-helper-plugin-janis');
module.exports = helper({
hooks: [
SQSHelper.sqsPermissions
...SQSHelper.buildHooks({ name: 'SessionEnded' })
]
});
With DLQ Consumer example
const { helper } = require('sls-helper');
const { SQSHelper } = require('sls-helper-plugin-janis');
module.exports = helper({
hooks: [
SQSHelper.sqsPermissions
...SQSHelper.buildHooks({
name: 'SessionEnded',
dlqConsumerProperties: {
timeout: 30,
batchSize: 10,
maximumBatchingWindow: 100
}
})
]
});
Delay Queue using main consumer example
const { helper } = require('sls-helper');
const { SQSHelper } = require('sls-helper-plugin-janis');
module.exports = helper({
hooks: [
SQSHelper.sqsPermissions
...SQSHelper.buildHooks({
name: 'ProcessStock',
consumerProperties: {
batchSize: 100,
maximumBatchingWindow: 60,
eventProperties: { maximumConcurrency: 5 }
}
delayQueueProperties: {
visibilityTimeout: 600
},
delayConsumerProperties: {
useMainHandler: true,
batchSize: 50,
maximumBatchingWindow: 30,
eventProperties: { maximumConcurrency: 1 }
}
})
]
});
SNS Helper
This helpers must be used to create SNS resources and subscribers with minimal data to a full customization.
Require Helpers
Unlike to normal Hooks, they must be explicitly required from the package.
const { SNSHelper } = require('sls-helper-plugin-janis');
Permissions
SNS Permissions are automatically created when creating a topic, you don't need to do nothing else :sparkles:
Build Hook
To create a new SNS Topic, you just have to call the SNS.buildHooks(config: SNSConfig)
method with the proper configuration object.
Types
You can see SNSConfig
and their properties in the types definition
Only with a topic name you are ready to go
SNS ARN Env Vars
Environment Variables can be generated using the SNSHelper.getEnvVar(topicName)
method, that will return the following env var:
[TOPIC_NAME_IN_UPPERCASE_SNAKE_CASE]_SNS_TOPIC_ARN
for the topic ARN
For example, for a topic with the name userCreated
, the USER_CREATED_SNS_TOPIC_ARN
env var will be returned.
You MUST add it to the lambda function that uses it.
SQS Connection
See SQSHelper (sourceSnsTopic
property) to know how to link a topic to an SQS Queue.
Quick hook example
const { helper } = require('sls-helper');
const { SNSHelper } = require('sls-helper-plugin-janis');
module.exports = helper({
hooks: [
...SNSHelper.buildHooks({
topic: {
name: 'userCreated'
}
}),
['function', {
functionName: 'CreateUser',
handler: 'src/lambda/User/Create.handler',
rawProperties: {
environment: {
...SNSHelper.getEnvVar('userCreated')
}
}
}],
]
});
Full example
'use strict';
const { helper } = require('sls-helper');
const { SQSHelper } = require('sls-helper-plugin-janis');
module.exports = helper({
hooks: [
['janis.base', {
serviceCode: 'my-service',
servicePort: 5000,
apiSecrets: {
beta: 'foo',
qa: 'bar',
prod: 'baz'
}
}],
'janis.templates',
['janis.authorizers', {
accountId: '012345678910'
}],
'janis.cors',
['janis.api', {
path: '/hello-world',
authorizer: 'NoClientAuthorizer',
cors: true
}],
['janis.apiList', {
entityName: 'product',
authorizer: 'FullAuthorizer',
cors: true
}],
['janis.apiGet', {
entityName: 'product',
authorizer: 'FullAuthorizer',
cors: true
}]
['janis.apiPost', {
entityName: 'product',
authorizer: 'FullAuthorizer',
cors: true
}],
['janis.apiPut', {
entityName: 'product',
authorizer: 'FullAuthorizer',
cors: true
}],
['janis.apiList', {
entityName: 'otherEntity',
authorizer: 'FullAuthorizer',
cors: {
origins: ['*'],
allowCredentials: false
}
}],
['janis.eventListener', {
serviceName: 'catalog',
entityName: 'product',
eventName: 'created',
mustHaveClient: true
}],
['janis.stateMachine', {
name: 'StateMachineName',
definition: {
Comment: 'State Machine Comment',
StartAt: 'WaitForCall',
States: {
WaitForCall: {
Type: 'Wait',
SecondsPath: '$.body.wait',
Next: 'Finish'
}
}
}
}],
['janis.functionsVpc', {
vpcId: 'vpc-11111111',
subnetIds: [
'subnet-111111111',
'subnet-222222222'
]
}],
['janis.functionUrl', {
subdomainName: 'subSubdomain.subdomain',
acmCertificate: '${param:acmCertificateId}',
functions: [
{
functionName: 'CustomUrlLambda',
path: '/customUrl/*'
}
]
}],
...SNSHelper.buildHooks({
topic: {
name: 'productUpdated'
}
}),
SQSHelper.sqsPermissions,
...SQSHelper.buildHooks({
name: 'ProductToUpdate',
sourceSnsTopic: {
name: 'productUpdated',
filterPolicy: {
'platform': ['fullcommerce']
}
}
})
]
}, {});