-
Notifications
You must be signed in to change notification settings - Fork 6
/
delivery-stack.ts
62 lines (55 loc) · 2.11 KB
/
delivery-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
import { Construct } from 'constructs';
import * as events from 'aws-cdk-lib/aws-events'
import { LambdaFunction as LambdaFunctionTarget } from 'aws-cdk-lib/aws-events-targets'
import * as nodeLambda from 'aws-cdk-lib/aws-lambda-nodejs'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import { BaseStack, BaseStackProps } from './base-stack'
import { CfnOutput, Duration } from 'aws-cdk-lib';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { POWERTOOLS_METRICS_NAMESPACE } from '../src/lambda-common';
/**
* Application to handle deliveries for orders
*
* When an Order.Created event is received, this application "delivers" the order
*/
export class DeliveryServiceStack extends BaseStack {
localBus: events.EventBus
identifier: string
constructor(scope: Construct, id: string, props: BaseStackProps) {
super(scope, id, props);
this.identifier = props.identifier
this.createOrderDeliveryFunction()
}
createOrderDeliveryFunction() {
const orderDeliveryFunction = new nodeLambda.NodejsFunction(this, 'OrderDeliveryFunction', {
entry: './src/delivery-handler.ts',
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'handleOrderCreated',
environment: {
BUS_ARN: this.globalBus.eventBusArn,
SERVICE_IDENTIFIER: this.identifier,
POWERTOOLS_SERVICE_NAME: 'OrderDelivery',
POWERTOOLS_METRICS_NAMESPACE
},
timeout: Duration.seconds(10),
logRetention: RetentionDays.ONE_WEEK,
tracing: lambda.Tracing.ACTIVE,
})
orderDeliveryFunction.addToRolePolicy(this.globalBusPutEventsStatement)
// The delivery function reacts to orders being created
const orderDeliveryRule = new events.Rule(this, 'OrderDeliveryRule', {
eventBus: this.localBus,
ruleName: 'order-delivery-rule',
eventPattern: {
detailType: ['Order.Created'],
},
})
orderDeliveryRule.addTarget(new LambdaFunctionTarget(orderDeliveryFunction))
new CfnOutput(this, 'orderDeliveryRule', {
value: orderDeliveryRule.ruleName
})
new CfnOutput(this, 'orderDeliveryRuleTarget', {
value: 'Target0'
})
}
}