Skip to content

Commit 3c85a81

Browse files
committed
addressed review 2
1 parent 9b6723d commit 3c85a81

File tree

8 files changed

+35
-97
lines changed

8 files changed

+35
-97
lines changed

appsync-events-bedrock-cdk/lib/appsync-events-bedrock-cdk-stack.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,47 @@
11
import * as cdk from 'aws-cdk-lib';
2-
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';
32
import { Construct } from 'constructs';
43
import { CfnApiKey } from 'aws-cdk-lib/aws-appsync';
54
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
65
import { Runtime } from 'aws-cdk-lib/aws-lambda';
76
import { PolicyDocument, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
7+
import { CfnApi, CfnChannelNamespace } from "aws-cdk-lib/aws-appsync"
88
import { CorsHttpMethod, HttpApi, HttpMethod } from 'aws-cdk-lib/aws-apigatewayv2';
99
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
10+
import { Duration, Expiration } from 'aws-cdk-lib';
1011

1112
export class AppsyncEventsBedrockCdkStack extends cdk.Stack {
1213
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
1314
super(scope, id, props);
1415

15-
// L1 constructs not available yet
16-
const template = new CfnInclude(this, "EventsApiCfnTemplate", {
17-
templateFile: './lib/events-api-cfn.yaml'
18-
})
19-
20-
const eventsApi = template.getResource('EventsApi') as cdk.CfnResource
16+
const eventsApi = new CfnApi(this, 'Events', {
17+
name: 'bedrock-failover',
18+
19+
eventConfig: {
20+
authProviders: [{
21+
authType: 'API_KEY',
22+
}],
23+
connectionAuthModes: [{
24+
authType: 'API_KEY',
25+
}],
26+
defaultPublishAuthModes: [{
27+
authType: 'API_KEY'
28+
}],
29+
defaultSubscribeAuthModes: [{
30+
authType: 'API_KEY',
31+
}],
32+
},
33+
});
2134

22-
const channel = template.getResource('Channel') as cdk.CfnResource
23-
const channelName = "BedrockChat"
35+
const channel = new CfnChannelNamespace(this, 'DefaultChannel', {
36+
name: 'BedrockChat',
37+
apiId: eventsApi.attrApiId
38+
})
2439

25-
const apiKey = template.getResource('ApiKey') as CfnApiKey
40+
const apiKey = new CfnApiKey(this, "AppsyncApiKey", {
41+
apiId: eventsApi.attrApiId,
42+
description: "Default API Key",
43+
expires: Expiration.after(Duration.days(30)).toEpoch(),
44+
});
2645

2746
const lambda = new NodejsFunction(this, 'Chat', {
2847
entry: "src/chat.ts",
@@ -33,7 +52,7 @@ export class AppsyncEventsBedrockCdkStack extends cdk.Stack {
3352
REGION: this.region,
3453
EVENTS_API_URL: eventsApi.getAtt('Dns.Http').toString(),
3554
API_KEY: apiKey.attrApiKey,
36-
CHANNEL_NAME: channelName
55+
CHANNEL_NAME: channel.name
3756
},
3857
role: new Role(this, 'ChatRole', {
3958
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
@@ -69,7 +88,6 @@ export class AppsyncEventsBedrockCdkStack extends cdk.Stack {
6988
)
7089
})
7190

72-
7391
const api = new HttpApi(this, 'ChatApi', {
7492
apiName: "chat-api",
7593
corsPreflight: {
@@ -86,19 +104,16 @@ export class AppsyncEventsBedrockCdkStack extends cdk.Stack {
86104
},
87105
})
88106

89-
90107
api.addRoutes({
91108
path: '/chat',
92109
methods: [HttpMethod.POST],
93110
integration: new HttpLambdaIntegration('LambdaIntegration', lambda)
94111
})
95112

96-
97113
new cdk.CfnOutput(this, "EventsApiHttp", {
98114
value: eventsApi.getAtt('Dns.Http').toString()
99115
})
100116

101-
102117
new cdk.CfnOutput(this, "EventsApiRealtime", {
103118
value: eventsApi.getAtt('Dns.Realtime').toString()
104119
})
@@ -108,7 +123,7 @@ export class AppsyncEventsBedrockCdkStack extends cdk.Stack {
108123
})
109124

110125
new cdk.CfnOutput(this, "ChannelName", {
111-
value: channelName
126+
value: channel.name
112127
})
113128

114129
new cdk.CfnOutput(this, "Region", {

appsync-events-bedrock-cdk/lib/events-api-cfn.yaml

Lines changed: 0 additions & 40 deletions
This file was deleted.

appsync-events-bedrock-cdk/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121
"@types/jest": "^29.5.14",
2222
"@types/node": "22.7.9",
2323
"@types/ws": "^8.5.13",
24-
"aws-cdk": "2.165.0",
24+
"aws-cdk": "^2.175.1",
2525
"esbuild": "^0.24.0",
2626
"jest": "^29.7.0",
27-
"socket.io": "^4.8.1",
2827
"ts-jest": "^29.2.5",
2928
"ts-node": "^10.9.2",
3029
"tsx": "^4.19.2",
3130
"typescript": "~5.6.3",
3231
"ws": "^8.18.0"
3332
},
3433
"dependencies": {
35-
"aws-cdk-lib": "2.165.0",
34+
"aws-cdk-lib": "2.175.1",
3635
"constructs": "^10.0.0",
37-
"socket.io-client": "^4.8.1",
3836
"source-map-support": "^0.5.21"
3937
}
4038
}

appsync-events-bedrock-cdk/src/chat.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ export interface ChatRequest {
1919
prompt: string
2020
}
2121

22-
2322
export const handler = async (event: APIGatewayProxyEventV2) => {
2423

25-
// log event
26-
console.log(JSON.stringify(event))
27-
2824
const body = JSON.parse(event.body ?? "") as ChatRequest
2925

30-
// log env vars
31-
//console.log(process.env)
32-
3326
// filter requests by checking if prompt is present
3427
if (!body.prompt) {
3528
throw Error("Missing prompt")
@@ -43,7 +36,6 @@ export const handler = async (event: APIGatewayProxyEventV2) => {
4336

4437
console.log("Final channel name: " + channelName)
4538

46-
4739
const command = new ConverseStreamCommand({
4840
modelId: modelId,
4941
messages: [{
@@ -97,17 +89,4 @@ export const handler = async (event: APIGatewayProxyEventV2) => {
9789
throw Error("Error in lambda function:" + JSON.stringify(error))
9890
}
9991

100-
}
101-
102-
103-
104-
/*
105-
TEST EVENT
106-
107-
108-
{
109-
"prompt": "what is the capital of paris",
110-
"userId": "gbiagini"
111-
}
112-
113-
*/
92+
}

appsync-events-bedrock-cdk/test/appsync-events-bedrock-cdk.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@ import { AppSyncEventsRequestApiKey } from "../utils/appsyncRequest"
22
import { readFileSync } from "fs"
33
import { makeRequest } from "../utils/apigwRequest";
44

5-
65
describe("AppSync Event API with API_KEY auth mode", () => {
76

87
const file = readFileSync("cdk-outputs.json");
98
const data = JSON.parse(file.toString())
109
const stackName = "AppsyncEventsBedrockCdkStack"
1110

12-
1311
process.env.API_KEY = data[stackName].ApiKey
1412
process.env.EVENTS_API_HTTP = data[stackName].EventsApiHttp
1513
process.env.EVENTS_API_REALTIME = data[stackName].EventsApiRealtime
1614
process.env.REGION = data[stackName].Region
1715
process.env.CHANNEL_NAME = data[stackName].ChannelName
1816
process.env.CHAT_API_URL = data[stackName].ChatApiUrl
1917

20-
21-
22-
2318
test("The user is able to publish a message", async () => {
2419

2520
if (!process.env.EVENTS_API_HTTP) throw new Error("EVENTS_API_HTTP is not defined")

appsync-events-bedrock-cdk/utils/apigwRequest.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import https from 'https'
22

3-
43
export interface RequestParams {
54
url: string;
65
method: string;
@@ -17,7 +16,6 @@ export interface RequestParams {
1716
userId?: string;
1817
}
1918

20-
2119
export const makeRequest = async (params: RequestParams, body: ChatRequest): Promise<ApiResponse> => {
2220
const endpoint = new URL(params.url);
2321
const options = {

appsync-events-bedrock-cdk/utils/appsyncRequest.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { HttpRequest } from '@aws-sdk/protocol-http'
22
import * as https from 'https'
33

4-
54
export interface RequestParamsEvent {
65
config: Config
76
channelName: String
@@ -14,7 +13,6 @@ export interface Config {
1413
region: string,
1514
}
1615

17-
1816
export const AppSyncEventsRequestApiKey = async (params: RequestParamsEvent) => {
1917
let endpoint: URL
2018

@@ -25,7 +23,6 @@ export const AppSyncEventsRequestApiKey = async (params: RequestParamsEvent) =>
2523
endpoint = new URL(params.config.url)
2624
}
2725

28-
2926
const request = new HttpRequest({
3027
hostname: endpoint.host,
3128
port: 443,
@@ -41,7 +38,6 @@ export const AppSyncEventsRequestApiKey = async (params: RequestParamsEvent) =>
4138
})
4239
})
4340

44-
4541
return new Promise<any>((resolve, reject) => {
4642
const httpRequest = https.request({ ...request, host: endpoint.hostname }, (result) => {
4743
let responseBody = '';
@@ -64,5 +60,4 @@ export const AppSyncEventsRequestApiKey = async (params: RequestParamsEvent) =>
6460
httpRequest.end()
6561
})
6662

67-
6863
}

appsync-events-bedrock-cdk/utils/outputs.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,4 @@ VITE_EVENTS_API_HTTP=${cdkOutputs[stackName].EventsApiHttp}
1919
VITE_EVENTS_API_REAL_TIME=${cdkOutputs[stackName].EventsApiRealtime}
2020
VITE_CHAT_API_URL=${cdkOutputs[stackName].ChatApiUrl}`,
2121
'utf8',
22-
);
23-
24-
22+
);

0 commit comments

Comments
 (0)