Skip to content

Instantly share code, notes, and snippets.

@abury
Created May 15, 2022 10:18
Show Gist options
  • Save abury/29a6c020d6ca0bbed262d9429c9aa2e6 to your computer and use it in GitHub Desktop.
Save abury/29a6c020d6ca0bbed262d9429c9aa2e6 to your computer and use it in GitHub Desktop.
Reference an SQS across stacks
// At the top of the file define your queue name as an export
export const QUEUE_NAME = `membership-events-queue`;
// Create your SQS queue as normal:
export const composeMembershipEventsHandler = (
stack: Stack,
props: ComposeBackgroundEventProps
) => {
const { env, tables } = props;
/**
* Create queues to handle the event data and trigger events
*/
const dlq = new sqs.Queue(stack, `${env}-membership-events-dlq`, {
queueName: `${env}-membership-events-dlq`,
encryption: sqs.QueueEncryption.KMS_MANAGED,
retentionPeriod: Duration.days(14),
});
tagComponent(dlq, "private");
const queue = new sqs.Queue(stack, `${env}-${QUEUE_NAME}`, {
queueName: `${env}-membership-events-queue`,
encryption: sqs.QueueEncryption.KMS_MANAGED,
deadLetterQueue: {
maxReceiveCount: 2,
queue: dlq,
},
});
...
};
// Now in your other stack, compose the ARN and import it:
import { QUEUE_NAME } from "~/background-events-stack/membershipEvents";
export const composeMembershipHandlerFunction = (
stack: Stack,
props: ComposeHandlerFunction
) => {
const { env, tables } = props;
/**
* Find the queue responsible for handling class based events
*/
const queueArn = `arn:aws:sqs:${stack.region}:${stack.account}:${env}-${QUEUE_NAME}`;
const queue = sqs.Queue.fromQueueArn(
stack,
`${env}-class-handler-${QUEUE_NAME}`,
queueArn
);
...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment