-
Notifications
You must be signed in to change notification settings - Fork 6
/
order-stack.ts
87 lines (79 loc) · 3.18 KB
/
order-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
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 * as apigw from 'aws-cdk-lib/aws-apigateway'
import { BaseStack, BaseStackProps } from './base-stack';
import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { CfnOutput } from 'aws-cdk-lib';
import { POWERTOOLS_METRICS_NAMESPACE } from '../src/lambda-common';
/**
* Application to manage customer order
*
* An HTTP endpoint is created to receive orders from customers.
* When an order is created, an Order.Created event is sent.
* When a delivery update is received, the order is updated and an Order.Updated event is sent.
*/
export class OrderServiceStack extends BaseStack {
localBus: events.EventBus
identifier: string
constructor(scope: Construct, id: string, props: BaseStackProps) {
super(scope, id, props);
this.identifier = props.identifier
this.createOrderCreateFunction()
this.createDeliveryUpdateFunction()
}
createOrderCreateFunction() {
const createOrderFunction = new nodeLambda.NodejsFunction(this, 'CreateOrderFunction', {
entry: './src/order-handler.ts',
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'handleOrderCreate',
environment: {
BUS_ARN: this.globalBus.eventBusArn,
SERVICE_IDENTIFIER: this.identifier,
POWERTOOLS_SERVICE_NAME: 'OrderCreate',
POWERTOOLS_METRICS_NAMESPACE
},
logRetention: RetentionDays.ONE_WEEK,
tracing: lambda.Tracing.ACTIVE,
})
createOrderFunction.addToRolePolicy(this.globalBusPutEventsStatement)
const api = new apigw.RestApi(this, 'OrderApi', { restApiName: 'order' })
api.root.addMethod('POST', new apigw.LambdaIntegration(createOrderFunction))
new CfnOutput(this, 'apiEndpoint', {
value: `https://${api.restApiId}.execute-api.${this.region}.${this.urlSuffix}/${api.deploymentStage.stageName}`
})
}
createDeliveryUpdateFunction() {
const deliveryUpdateFunction = new nodeLambda.NodejsFunction(this, 'DeliveryUpdateFunction', {
entry: './src/order-handler.ts',
runtime: lambda.Runtime.NODEJS_18_X,
handler: 'handleDeliveryUpdate',
environment: {
BUS_ARN: this.globalBus.eventBusArn,
SERVICE_IDENTIFIER: this.identifier,
POWERTOOLS_SERVICE_NAME: 'DeliveryUpdate',
POWERTOOLS_METRICS_NAMESPACE
},
logRetention: RetentionDays.ONE_WEEK,
tracing: lambda.Tracing.ACTIVE,
})
deliveryUpdateFunction.addToRolePolicy(this.globalBusPutEventsStatement)
// React to delivery events
const deliveryEventsRule = new events.Rule(this, 'DeliveryHandlingRule', {
eventBus: this.localBus,
ruleName: 'order-service-rule',
eventPattern: {
detailType: ['Delivery.Updated'],
}
})
deliveryEventsRule.addTarget(new LambdaFunctionTarget(deliveryUpdateFunction))
new CfnOutput(this, 'deliveryEventsRule', {
value: deliveryEventsRule.ruleName
})
new CfnOutput(this, 'deliveryEventsRuleTarget', {
value: 'Target0'
})
}
}