-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.ts
108 lines (91 loc) · 3.47 KB
/
stack.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cdk from 'aws-cdk-lib';
import { join } from "path";
import { orderExecutedHtmlTemplate } from './orderExecutedHtmlTemplate';
export class Part08SQSStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const api = new cdk.aws_apigateway.RestApi(this, 'api', {
restApiName: 'Part08Service',
});
// Use your own domain name
const DOMAIN_NAME = 'pchol.fr';
// I already created the SES identity in part 06
const identity = cdk.aws_ses.EmailIdentity.fromEmailIdentityName(this, 'sesIdentity', DOMAIN_NAME);
const ordersQueue = new cdk.aws_sqs.Queue(this, 'ordersQueue', {
visibilityTimeout: cdk.Duration.seconds(120),
fifo: true,
});
const eventSource = new cdk.aws_lambda_event_sources.SqsEventSource(ordersQueue, {
batchSize: 1,
});
const ordersEventBus = new cdk.aws_events.EventBus(this, 'ordersEventBus');
const notifyOrderExecutedRule = new cdk.aws_events.Rule(this, 'notifyOrderExecutedRule', {
eventBus: ordersEventBus,
eventPattern: {
source: ['notifyOrderExecuted'],
detailType: ['orderExecuted'],
},
});
const orderExecutedTemplate = new cdk.aws_ses.CfnTemplate(this, 'orderExecutedTemplate', {
template: {
htmlPart: orderExecutedHtmlTemplate,
subjectPart: 'Your order was passed to our provider!',
templateName: 'orderExecutedTemplate',
}
});
const requestOrder = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'requestOrder', {
entry: join(__dirname, 'requestOrder', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
QUEUE_URL: ordersQueue.queueUrl,
},
});
ordersQueue.grantSendMessages(requestOrder);
api.root.addResource('request-order').addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(requestOrder));
const executeOrder = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'executeOrder', {
entry: join(__dirname, 'executeOrder', 'handler.ts'),
handler: 'handler',
environment: {
EVENT_BUS_NAME: ordersEventBus.eventBusName,
},
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
reservedConcurrentExecutions: 1,
timeout: cdk.Duration.seconds(30),
});
executeOrder.addEventSource(eventSource);
executeOrder.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['events:PutEvents'],
resources: [ordersEventBus.eventBusArn],
})
);
const notifyOrderExecuted = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'notifyOrderExecuted', {
entry: join(__dirname, 'notifyOrderExecuted', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
SENDER_EMAIL: `contact@${identity.emailIdentityName}`,
TEMPLATE_NAME: orderExecutedTemplate.ref,
},
});
notifyOrderExecuted.addToRolePolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['ses:SendTemplatedEmail'],
resources: ['*'],
}),
);
notifyOrderExecutedRule.addTarget(new cdk.aws_events_targets.LambdaFunction(notifyOrderExecuted));
}
}