-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprint-service-stack.ts
More file actions
150 lines (135 loc) · 5.29 KB
/
Copy pathprint-service-stack.ts
File metadata and controls
150 lines (135 loc) · 5.29 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2025-Present Datadog, Inc.
*/
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import { SharedProps } from "../../../../shared/lib/shared-constructs/lib/shared-props";
import { SharedResources } from "../../../../shared/lib/shared-constructs/lib/shared-resources";
import { Api } from "./api";
import { Cluster } from "aws-cdk-lib/aws-ecs";
import { StringParameter } from "aws-cdk-lib/aws-ssm";
import { AWSMessagingProps, ServiceProps } from "./service-props";
import { StreamViewType, Table } from "aws-cdk-lib/aws-dynamodb";
import { BackgroundWorkers } from "./background-workers";
export class PrintServiceStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const serviceName = "print-service";
const environment = process.env.ENV || "dev";
const sharedResources = new SharedResources(this, "SharedResources", {
networkName: `${serviceName}-${environment}-vpc`,
environment: environment,
});
const ddSite = process.env.DD_SITE || "datadoghq.com";
const ddApiKey = process.env.DD_API_KEY || "";
const ddApiKeyParam = new StringParameter(this, "DDApiKeyParam", {
parameterName: `/stickerlandia/${environment}/print/dd-api-key`,
stringValue: ddApiKey,
simpleName: false,
});
const cluster = new Cluster(this, "ApiCluster", {
vpc: sharedResources.vpc,
clusterName: `${serviceName}-${environment}`,
});
cluster.enableFargateCapacityProviders();
const sharedProps = new SharedProps(
this,
"print",
serviceName,
cluster,
ddApiKey,
ddApiKeyParam,
ddSite,
true,
{
DD_APM_IGNORE_RESOURCES:
"(GET|HEAD) .*/health$,POST localhost:4317/opentelemetry.proto.collector.trace.v1.TraceService/Export,POST localhost:4317/opentelemetry.proto.collector.metrics.v1.MetricsService/Export",
},
);
const serviceProps: ServiceProps = {
cloudfrontDistribution: sharedResources.cloudfrontDistribution,
messagingConfiguration: new AWSMessagingProps(
this,
"MessagingProps",
sharedResources
),
serviceDependencies: [],
};
// The data model of the Print Service supports the constraints that DynamoDB imposes on an application
// Using DynamoDB instead of Postgres
const isProduction = environment === "prod";
const printerTable = new Table(this, "PrinterTable", {
tableName: `Printers-${environment}`,
partitionKey: { name: "PK", type: cdk.aws_dynamodb.AttributeType.STRING },
sortKey: { name: "SK", type: cdk.aws_dynamodb.AttributeType.STRING },
removalPolicy: isProduction ? cdk.RemovalPolicy.RETAIN : cdk.RemovalPolicy.DESTROY,
billingMode: cdk.aws_dynamodb.BillingMode.PAY_PER_REQUEST,
pointInTimeRecovery: true,
deletionProtection: isProduction,
stream: StreamViewType.NEW_IMAGE,
timeToLiveAttribute: "TTL",
});
printerTable.addGlobalSecondaryIndex({
indexName: "GSI1",
partitionKey: {
name: "GSI1PK",
type: cdk.aws_dynamodb.AttributeType.STRING,
},
projectionType: cdk.aws_dynamodb.ProjectionType.ALL,
});
const printJobTable = new Table(this, "PrintJobTable", {
tableName: `PrintJobs-${environment}`,
partitionKey: {
name: "PK",
type: cdk.aws_dynamodb.AttributeType.STRING,
},
sortKey: { name: "SK", type: cdk.aws_dynamodb.AttributeType.STRING },
removalPolicy: isProduction ? cdk.RemovalPolicy.RETAIN : cdk.RemovalPolicy.DESTROY,
billingMode: cdk.aws_dynamodb.BillingMode.PAY_PER_REQUEST,
pointInTimeRecovery: true,
deletionProtection: isProduction,
stream: StreamViewType.NEW_IMAGE,
timeToLiveAttribute: "TTL",
});
printJobTable.addGlobalSecondaryIndex({
indexName: "GSI1",
partitionKey: {
name: "GSI1PK",
type: cdk.aws_dynamodb.AttributeType.STRING,
},
sortKey: {
name: "GSI1SK",
type: cdk.aws_dynamodb.AttributeType.STRING,
},
projectionType: cdk.aws_dynamodb.ProjectionType.ALL,
});
const api = new Api(this, "Api", {
sharedProps: sharedProps,
serviceProps,
vpc: sharedResources.vpc,
vpcLink: sharedResources.vpcLink,
vpcLinkSecurityGroupId: sharedResources.vpcLinkSecurityGroupId,
httpApi: sharedResources.httpApi,
serviceDiscoveryName: "print.api",
serviceDiscoveryNamespace: sharedResources.serviceDiscoveryNamespace,
cluster: cluster,
deployInPrivateSubnet: true,
printerTable: printerTable,
printJobTable: printJobTable,
});
const backgroundWorkers = new BackgroundWorkers(this, "BackgroundWorkers", {
sharedProps,
serviceProps,
sharedEventBus: sharedResources.sharedEventBus,
printerTable: printerTable,
printJobTable: printJobTable,
});
// CDK Outputs
new cdk.CfnOutput(this, "ServiceApiUrl", {
value: `https://${sharedResources.cloudfrontDistribution.distributionDomainName}/api/print/v1`,
description: "Print Service API URL",
});
}
}