-
Notifications
You must be signed in to change notification settings - Fork 2
02 Bucket Queue Event Bus
JP Barbosa edited this page Sep 24, 2022
·
11 revisions
code ./stacks/resources/bucket.ts
import { Bucket, Stack } from "@serverless-stack/resources";
export const createBucket = (stack: Stack) =>
new Bucket(stack, "bucket", {
cors: true,
cdk: {
bucket: {
eventBridgeEnabled: true,
},
},
});
code ./stacks/resources/queue.ts
import { Queue, Stack, Bucket } from "@serverless-stack/resources";
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
type CreateQueueOptions = { bucket: Bucket };
type CreateQueue = (stack: Stack, options: CreateQueueOptions) => Queue;
export const createQueue: CreateQueue = (stack, { bucket }) => {
const queuePolicy = new PolicyStatement({
actions: ["rekognition:*"],
resources: ["*"],
});
const queue = new Queue(stack, "queue", {
consumer: {
function: {
handler: "functions/process.handler",
initialPolicy: [queuePolicy],
},
},
});
queue.attachPermissions([bucket]);
return queue;
};
code ./stacks/resources/bus.ts
import { Bucket, EventBus, Queue, Stack } from "@serverless-stack/resources";
import * as events from "aws-cdk-lib/aws-events";
type CreateBusOptions = { bucket: Bucket; queue: Queue };
type CreateBus = (stack: Stack, options: CreateBusOptions) => EventBus;
export const createBus: CreateBus = (stack, { bucket, queue }) =>
new EventBus(stack, "bus", {
cdk: {
eventBus: events.EventBus.fromEventBusName(
stack,
"ImportedBus",
"default"
),
},
rules: {
rule1: {
pattern: {
source: ["aws.s3"],
detailType: ["Object Created"],
detail: {
bucket: {
name: [bucket.bucketName],
},
},
},
targets: {
process: queue,
},
},
},
});
code ./stacks/resources/index.ts
export * from "./bucket";
export * from "./queue";
export * from "./bus";
code ./stacks/MyStack.ts
import { StackContext } from "@serverless-stack/resources";
import {
createBucket,
createQueue,
createBus,
} from "./resources";
export function MyStack({ stack }: StackContext) {
const bucket = createBucket(stack);
const queue = createQueue(stack, { bucket });
const bus = createBus(stack, { bucket, queue });
}
git add .
git commit -m "Bucket, Queue, and Event Bus"