@node-ts/bus-sqs
![CircleCI](https://circleci.com/gh/node-ts/bus/tree/master.svg?style=svg)
![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)
An AWS SQS transport adapter for @node-ts/bus-core
.
Installation
Install all packages and their dependencies
npm i reflect-metadata inversify @node-ts/bus-sqs @node-ts/bus-core
Once installed, load the BusSqsModule
to your inversify container alongside the other modules it depends on:
import { Container } from 'inversify'
import { LoggerModule } from '@node-ts/logger-core'
import { BusModule } from '@node-ts/bus-core'
import { BUS_SQS_SYMBOLS, BusSqsModule, SqsConfiguration } from '@node-ts/bus-sqs'
const container = new Container()
container.load(new LoggerModule())
container.load(new BusModule())
container.load(new BusSqsModule())
const resourcePrefix = 'integration'
const invalidSqsSnsCharacters = new RegExp('[^a-zA-Z0-9_-]', 'g')
const normalizeMessageName = (messageName: string) => messageName.replace(invalidSqsSnsCharacters, '-')
const AWS_REGION = process.env.AWS_REGION
const AWS_ACCOUNT_ID = process.env.AWS_ACCOUNT_ID
const sqsConfiguration: SqsTransportConfiguration = {
queueName: `${resourcePrefix}-test`,
queueUrl: `https://sqs.${AWS_REGION}.amazonaws.com/${AWS_ACCOUNT_ID}/${resourcePrefix}-test`,
queueArn: `arn:aws:sqs:${AWS_REGION}:${AWS_ACCOUNT_ID}:${resourcePrefix}-test`,
deadLetterQueueName: `${resourcePrefix}-dead-letter`,
deadLetterQueueArn: `arn:aws:sqs:${AWS_REGION}:${AWS_ACCOUNT_ID}:${resourcePrefix}-dead-letter`,
resolveTopicName: (messageName: string) =>
`${resourcePrefix}-${normalizeMessageName(messageName)}`,
resolveTopicArn: (topicName: string) =>
`arn:aws:sns:${AWS_REGION}:${AWS_ACCOUNT_ID}:${topicName}`,
queuePolicy: `
{
"Version": "2012-10-17",
"Statement": [
{
"Principal": "*",
"Effect": "Allow",
"Action": [
"sqs:SendMessage"
],
"Resource": [
"arn:aws:sqs:${AWS_REGION}:${AWS_ACCOUNT_ID}:${resourcePrefix}-*"
],
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:sns:${AWS_REGION}:${AWS_ACCOUNT_ID}:${resourcePrefix}-*"
}
}
}
]
}
`
}
container.bind(BUS_SQS_SYMBOLS.SqsConfiguration).toConstantValue(sqsConfiguration)
Development
Local development can be done with the aid of docker to run the required infrastructure. To do so, run:
docker run -e SERVICES=sqs,sns -p 4575:4575 -p 4576:4576 localstack/localstack
This will create a localstack instance running and exposing a mock sqs/sns that's compatible with the AWS-SDK. This same environment is used when running integration tests for the SqsTransport
.