-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-publishment-stack.ts
190 lines (177 loc) · 6.21 KB
/
api-publishment-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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import * as cdk from "aws-cdk-lib";
import { CfnOutput, Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as apigateway from "aws-cdk-lib/aws-apigateway";
import { DockerImageCode, DockerImageFunction } from "aws-cdk-lib/aws-lambda";
import * as iam from "aws-cdk-lib/aws-iam";
import * as lambdaEventSources from "aws-cdk-lib/aws-lambda-event-sources";
import * as path from "path";
import { Platform } from "aws-cdk-lib/aws-ecr-assets";
import * as wafv2 from "aws-cdk-lib/aws-wafv2";
import * as sqs from "aws-cdk-lib/aws-sqs";
import * as s3 from "aws-cdk-lib/aws-s3";
import * as logs from "aws-cdk-lib/aws-logs";
import { excludeDockerImage } from "./constants/docker";
interface ApiPublishmentStackProps extends StackProps {
readonly bedrockRegion: string;
readonly conversationTableName: string;
readonly tableAccessRoleArn: string;
readonly webAclArn: string;
readonly usagePlan: apigateway.UsagePlanProps;
readonly deploymentStage?: string;
readonly largeMessageBucketName: string;
readonly corsOptions?: apigateway.CorsOptions;
}
export class ApiPublishmentStack extends Stack {
public readonly chatQueue: sqs.Queue;
constructor(scope: Construct, id: string, props: ApiPublishmentStackProps) {
super(scope, id, props);
console.log(`usagePlan: ${JSON.stringify(props.usagePlan)}`); // DEBUG
const deploymentStage = props.deploymentStage ?? "dev";
const chatQueueDLQ = new sqs.Queue(this, "ChatQueueDlq", {
retentionPeriod: cdk.Duration.days(14),
});
const chatQueue = new sqs.Queue(this, "ChatQueue", {
visibilityTimeout: cdk.Duration.minutes(30),
deadLetterQueue: {
maxReceiveCount: 2, // one retry
queue: chatQueueDLQ,
},
});
const handlerRole = new iam.Role(this, "HandlerRole", {
assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
});
handlerRole.addManagedPolicy(
iam.ManagedPolicy.fromAwsManagedPolicyName(
"service-role/AWSLambdaBasicExecutionRole"
)
);
handlerRole.addToPolicy(
// Assume the table access role for row-level access control.
new iam.PolicyStatement({
actions: ["sts:AssumeRole"],
resources: [props.tableAccessRoleArn],
})
);
handlerRole.addToPolicy(
new iam.PolicyStatement({
actions: ["bedrock:*"],
resources: ["*"],
})
);
const largeMessageBucket = s3.Bucket.fromBucketName(
this,
"LargeMessageBucket",
props.largeMessageBucketName
);
largeMessageBucket.grantReadWrite(handlerRole);
// Handler for FastAPI
const apiHandler = new DockerImageFunction(this, "ApiHandler", {
code: DockerImageCode.fromImageAsset(
path.join(__dirname, "../../backend"),
{
platform: Platform.LINUX_AMD64,
file: "Dockerfile",
exclude: [...excludeDockerImage],
}
),
memorySize: 1024,
timeout: cdk.Duration.minutes(15),
environment: {
PUBLISHED_API_ID: id.replace("ApiPublishmentStack", ""),
QUEUE_URL: chatQueue.queueUrl,
TABLE_NAME: props.conversationTableName,
CORS_ALLOW_ORIGINS: (props.corsOptions?.allowOrigins ?? ["*"]).join(
","
),
ACCOUNT: Stack.of(this).account,
REGION: Stack.of(this).region,
BEDROCK_REGION: props.bedrockRegion,
LARGE_MESSAGE_BUCKET: props.largeMessageBucketName,
TABLE_ACCESS_ROLE_ARN: props.tableAccessRoleArn,
},
role: handlerRole,
logRetention: logs.RetentionDays.THREE_MONTHS,
});
// Handler for SQS consumer
const sqsConsumeHandler = new DockerImageFunction(
this,
"SqsConsumeHandler",
{
code: DockerImageCode.fromImageAsset(
path.join(__dirname, "../../backend"),
{
platform: Platform.LINUX_AMD64,
file: "lambda.Dockerfile",
cmd: ["app.sqs_consumer.handler"],
exclude: [...excludeDockerImage],
}
),
memorySize: 1024,
timeout: cdk.Duration.minutes(15),
environment: {
PUBLISHED_API_ID: id.replace("ApiPublishmentStack", ""),
QUEUE_URL: chatQueue.queueUrl,
TABLE_NAME: props.conversationTableName,
CORS_ALLOW_ORIGINS: (props.corsOptions?.allowOrigins ?? ["*"]).join(
","
),
ACCOUNT: Stack.of(this).account,
REGION: Stack.of(this).region,
BEDROCK_REGION: props.bedrockRegion,
TABLE_ACCESS_ROLE_ARN: props.tableAccessRoleArn,
},
role: handlerRole,
logRetention: logs.RetentionDays.THREE_MONTHS,
}
);
sqsConsumeHandler.addEventSource(
new lambdaEventSources.SqsEventSource(chatQueue)
);
chatQueue.grantSendMessages(apiHandler);
chatQueue.grantConsumeMessages(sqsConsumeHandler);
const api = new apigateway.LambdaRestApi(this, "Api", {
restApiName: id,
handler: apiHandler,
proxy: true,
deployOptions: {
stageName: deploymentStage,
},
defaultMethodOptions: { apiKeyRequired: true },
defaultCorsPreflightOptions: props.corsOptions,
});
const apiKey = api.addApiKey("ApiKey", {
description: "Default api key (Auto generated by CDK)",
});
const usagePlan = api.addUsagePlan("UsagePlan", {
...props.usagePlan,
});
usagePlan.addApiKey(apiKey);
usagePlan.addApiStage({ stage: api.deploymentStage });
const association = new wafv2.CfnWebACLAssociation(
this,
"WebAclAssociation",
{
resourceArn: `arn:aws:apigateway:${this.region}::/restapis/${api.restApiId}/stages/${api.deploymentStage.stageName}`,
webAclArn: props.webAclArn,
}
);
association.addDependency(api.node.defaultChild as cdk.CfnResource);
this.chatQueue = chatQueue;
new CfnOutput(this, "ApiId", {
value: api.restApiId,
});
new CfnOutput(this, "ApiName", {
value: api.restApiName,
});
new CfnOutput(this, "ApiUsagePlanId", {
value: usagePlan.usagePlanId,
});
new CfnOutput(this, "AllowedOrigins", {
value: props.corsOptions?.allowOrigins?.join(",") ?? "*",
});
new CfnOutput(this, "DeploymentStage", {
value: deploymentStage,
});
}
}